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

/**
 * 导出Excel
 * @param {*} data 导出Excel文件流数据
 * @param {*} fileName 导出Excel文件名称
 * @author Rockery(1113269755@qq.com)
 */
export function exportExcel(data, fileName) {
  const contentByExportExcel = data;
  const blobByExportExcel = new Blob([contentByExportExcel], { type: "application/vnd.ms-excel;charset=utf-8" });
  const filenameByExportExcel = fileName + "_" + parseTime(new Date(), "{y}-{m}-{d}") + "_" + new Date().getTime() + ".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
    });
  }
}
