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";
  // console.log(fileName + "_" + parseTime(new Date(), "{y}-{m}-{d}"));
  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
    });
  }
}
