123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- import { openWindow } from '..';
- import { dataURLtoBlob, urlToBase64 } from './base64Conver';
- import * as XLSX from 'xlsx' // Vue3 版本
- import fs from 'file-saver'
- /**
- * Download online pictures
- * @param data
- * @param filename
- */
- export function exportElsxFile(json, fields, filename = '表格.xlsx') {
- json.forEach(item => {
- for (let i in item) {
- if (fields.hasOwnProperty(i)) {
- item[fields[i]] = item[i];
- }
- delete item[i]; //删除原先的对象属性
- }
- })
- let sheetName = filename //excel的文件名称
- let wb = XLSX.utils.book_new() //工作簿对象包含一SheetNames数组,以及一个表对象映射表名称到表对象。XLSX.utils.book_new实用函数创建一个新的工作簿对象。
- let ws = XLSX.utils.json_to_sheet(json, { header: Object.values(fields) }) //将JS对象数组转换为工作表。
- wb.SheetNames.push(sheetName)
- wb.Sheets[sheetName] = ws
- const defaultCellStyle = { font: { name: "Verdana", sz: 13, color: "FF00FF88" }, fill: { fgColor: { rgb: "FFFFAA00" } } };//设置表格的样式
- let wopts = { bookType: 'xlsx', bookSST: false, type: 'binary', cellStyles: true, defaultCellStyle: defaultCellStyle, showGridLines: false } //写入的样式
- let wbout = XLSX.write(wb, wopts)
- let blob = new Blob([s2ab(wbout)], { type: 'application/octet-stream' })
- fs.saveAs(blob, filename + '.xlsx')
- }
- const s2ab = s => {
- if (typeof ArrayBuffer !== 'undefined') {
- var buf = new ArrayBuffer(s.length)
- var view = new Uint8Array(buf)
- for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xff
- return buf
- } else {
- var buf = new Array(s.length);
- for (var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;
- return buf;
- }
- }
- /**
- * Download online pictures
- * @param url
- * @param filename
- * @param mime
- * @param bom
- */
- export function downloadByOnlineUrl(url: string, filename: string, mime?: string, bom?: BlobPart) {
- urlToBase64(url).then((base64) => {
- downloadByBase64(base64, filename, mime, bom);
- });
- }
- /**
- * Download pictures based on base64
- * @param buf
- * @param filename
- * @param mime
- * @param bom
- */
- export function downloadByBase64(buf: string, filename: string, mime?: string, bom?: BlobPart) {
- const base64Buf = dataURLtoBlob(buf);
- downloadByData(base64Buf, filename, mime, bom);
- }
- /**
- * Download according to the background interface file stream
- * @param {*} data
- * @param {*} filename
- * @param {*} mime
- * @param {*} bom
- */
- export function downloadByData(data: BlobPart, filename: string, mime?: string, bom?: BlobPart) {
- const blobData = typeof bom !== 'undefined' ? [bom, data] : [data];
- const blob = new Blob(blobData, { type: mime || 'application/octet-stream' });
- const blobURL = window.URL.createObjectURL(blob);
- const tempLink = document.createElement('a');
- tempLink.style.display = 'none';
- tempLink.href = blobURL;
- tempLink.setAttribute('download', filename);
- if (typeof tempLink.download === 'undefined') {
- tempLink.setAttribute('target', '_blank');
- }
- document.body.appendChild(tempLink);
- tempLink.click();
- document.body.removeChild(tempLink);
- window.URL.revokeObjectURL(blobURL);
- }
- /**
- * Download file according to file address
- * @param {*} sUrl
- */
- export function downloadByUrl({
- url,
- target = '_blank',
- fileName,
- }: {
- url: string;
- target?: TargetContext;
- fileName?: string;
- }): boolean {
- const isChrome = window.navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
- const isSafari = window.navigator.userAgent.toLowerCase().indexOf('safari') > -1;
- if (/(iP)/g.test(window.navigator.userAgent)) {
- console.error('Your browser does not support download!');
- return false;
- }
- if (isChrome || isSafari) {
- const link = document.createElement('a');
- link.href = url;
- link.target = target;
- if (link.download !== undefined) {
- link.download = fileName || url.substring(url.lastIndexOf('/') + 1, url.length);
- }
- if (document.createEvent) {
- const e = document.createEvent('MouseEvents');
- e.initEvent('click', true, true);
- link.dispatchEvent(e);
- return true;
- }
- }
- if (url.indexOf('?') === -1) {
- url += '?download';
- }
- openWindow(url, { target });
- return true;
- }
|