导出Excel

//import { parseTime } from "@/utils/index";
import { Message } from 'element-ui';

/**
 * 导出Excel
 * @param {*} data 导出Excel文件流数据
 * @param {*} fileName 导出Excel文件名称
 */
export function exportExcel(data, fileName) {
  const contentByExportExcel = data;
  const blobByExportExcel = new Blob([contentByExportExcel], { type: "application/vnd.ms-excel;charset=utf-8" });
  console.log('blobByExportExcel',blobByExportExcel);
  // const filenameByExportExcel = fileName + "_" + parseTime(new Date(), "{y}-{m}-{d}") + "_" + new Date().getTime() + ".xlsx";
  const filenameByExportExcel = fileName + Date.parse(new Date()) + ".xlsx";
  if ('download' in document.createElement('a')) {
    const elink = document.createElement('a');
    elink.download = filenameByExportExcel;
    elink.style.display = 'none';
    elink.href = URL.createObjectURL(blobByExportExcel);
    document.body.appendChild(elink);
    elink.click();
    URL.revokeObjectURL(elink.href);
    document.body.removeChild(elink);
    Message({
      message: "导出Excel成功!",
      type: 'success',
      showClose: true,
      center: true
    });
  } else {
    navigator.msSaveBlob(blobByExportExcel, filenameByExportExcel);
    Message({
      message: "导出Excel成功!",
      type: 'success',
      showClose: true,
      center: true
    });
  }
}

THE END
分享
二维码
打赏
< <上一篇
下一篇>>