1import WriteXlsxFile from "write-excel-file";
2
3/**
4 * @description Json数据导出Xlsx
5 * @param {Array} data JSON数据
6 * @param {Array} header xlsx表头 [{key:'',name:'',width:''}]
7 * @param {String} key column的key
8 * @param {String} name column的name
9 * @param {Number} width column的width
10 * @param {String} fileName xlsx文件名
11 **/
12export const DataToXlsx = async (data, header, fileName) => {
13 let stmp = {
14 column: "",
15 with: 20,
16 value: null
17 };
18
19 let SCHEMA = header.map(({ key, name, width, type }) => {
20 let column = Object.assign({}, stmp, {
21 type: type ?? String,
22 column: name,
23 width: width || 20,
24 value: v => v?.[key] ?? ""
25 });
26 return column;
27 });
28
29 let FILE_NAME = fileName.lastIndexOf(".xlsx") != -1 ? fileName : fileName + ".xlsx";
30
31 await WriteXlsxFile(data, {
32 schema: SCHEMA,
33 fileName: FILE_NAME,
34 headerStyle: {
35 backgroundColor: "#f2f6fc",
36 fontWeight: "bold",
37 align: "center",
38 height: 20
39 }
40 });
41};