/*
备注:直接传入表格的 columns 和 data即可
header: [{dataIndex: 'site', title: '地点'}] string Array 标题
body: [{time: '3点', site: '青岛'}] object Array 数据
name: '数据导出' string 文件名
*/
export function expoerExcel(header = [], body = [], name = '') {
console.log('header==', header);
console.log('body==', body);
console.log('name==', name);
// 处理标题
const dataSource = [], r1 = [];
header.forEach(item => {
r1.push(item.title)
})
dataSource.push(r1)
// 处理数据
body.forEach(b => {
let r = [];
header.forEach(h => {
r.push(b[h.dataIndex])
})
dataSource.push(r)
})
console.log('dataSource==', JSON.stringify(dataSource)); // [["工厂名称","物料编码","物料名称","BOM层级","版本号","数量"],["华为","3Z668A","钣金门",2,"N167",30],["华为","WS2452","钣金门2",1,"N167",230],["华为","74GER4","冷凝器",3,"N167",16]]
const worksheet = XLSX.utils.aoa_to_sheet(dataSource);
const new_workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(new_workbook, worksheet, 'sheet1');
XLSX.writeFile(new_workbook, `${name || '导出报表'}.xlsx`);
}