import * as XLSX from "xlsx"; import { saveAs } from "file-saver"; import * as XLSXD from "xlsx-js-style"; // 导出的方法 tHeader => 设置Excel的表格第一行的标题 filterVal => 对象的属性key值 被导出listData => 导出数据 => 导出(文件)名称 import { export_json_to_excel } from "@/tools/excel/Export2Excel"; //引入文件 export function outExportExcel( tHeader = [], filterVal = [], listData = [], exportName = new Date().getTime() ) { // 注意这个Export2Excel路径 const data = formatJson(filterVal, listData); export_json_to_excel(tHeader, data, exportName); } function formatJson(filterVal, jsonData) { return jsonData.map((v) => filterVal.map((j) => v[j])); } const exportExcel = (el, header, title) => { let $e = el; try { let $table = $e.querySelector(".el-table__fixed"); if (!$table) { $table = $e; } const wb = XLSX.utils.table_to_book($table, { raw: true }); if (wb && wb.Sheets.Sheet1) { let as = ["!cols", "!fullref", "!merges", "!ref", "!rows"]; let bs = header; for (let i in wb.Sheets.Sheet1) { if (as.indexOf(i) < 0) { if (bs.indexOf(wb.Sheets.Sheet1[i].v) >= 0) { wb.Sheets.Sheet1[i].s = { font: { name: "微软雅黑", sz: 11, bold: true, color: { rgb: "ffffff" }, }, alignment: { horizontal: "center", vertical: "center", }, fill: { fgColor: { rgb: "4f81bd" } }, }; if (i === "A1") { wb.Sheets.Sheet1["!cols"].push({ wch: 30 }); } else if (i === "B1") { wb.Sheets.Sheet1["!cols"].push({ wch: 20 }); } else { wb.Sheets.Sheet1["!cols"].push({ wch: 15 }); } } else { wb.Sheets.Sheet1[i].s = { font: { name: "微软雅黑", sz: 10, bold: false, color: { rgb: "000000" }, }, alignment: { horizontal: "center", vertical: "center", }, // fill:{fgColor: {rgb: '4f81bd'}} }; } } } } const wbout = XLSXD.write(wb, { bookType: "xlsx", bookSST: true, type: "array", }); saveAs( new Blob([wbout], { type: "application/octet-stream" }), `${title}.xlsx` ); } catch (e) { if (typeof console !== "undefined") console.error(e); } }; export default { exportExcel, };