表格导出功能 支持render方法 支持DOM渲染 (初级)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import ExportJsonExcel from "js-export-excel";
import {message} from "antd";
/**
* @param {Array} dataSource
* @param {Array} columns
* @param {String} title
* */
export const excelExporter = ({dataSource, columns, title = '记录导出文件'}) => {
return new Promise((resolve, reject) => {
const hide = message.loading('导出中')
const option = {};
const getReactElementLoop = (element) => {
const propChilds = element.props.children
let result = ''
switch (typeof propChilds) {
case "object":
if (isArr(propChilds)) {
for (const child of propChilds) {
result += getReactElementLoop(child) + ' '
}
} else {
result = getReactElementLoop(propChilds)
}
break;
default:
result = propChilds;
break;
}
return result
}
option.fileName = title;
option.datas = [
{
sheetData: dataSource.map(item => {
const result = {};
columns.forEach(c => {
result[c.dataIndex] = c.render ? c.render(item[c.dataIndex], item) : item[c.dataIndex];
if (typeof result[c.dataIndex] === 'object') {
result[c.dataIndex] = getReactElementLoop(result[c.dataIndex])
}
});
return result;
}),
sheetName: title, // Excel文件名称
sheetFilter: columns.map(item => item.dataIndex),
sheetHeader: columns.map(item => item.title),
columnWidths: columns.map(() => 10),
},
];
const toExcel = new ExportJsonExcel(option);
toExcel.saveExcel();
setTimeout(hide, 500)
resolve(1)
})
}