公司有这样的需求:要将前端的表格数据导出为Excel。看了大佬的文章很受用原文地址
应用了插件
先安装js-export-excel
yarn add js-export-excel
npm install js-export-excel
引入js-export-excel
import ExportJsonExcel from 'js-export-excel';
使用
let data = res.body.content || []; // json数据
let option = {};
let dataTable = [];
if (data.length > 0) {
if (data) {
for (let i in data) {
let obj = {
'考勤图片': _.get(data[i].attendancImageInfo,'resource_url',''),
'考勤人员': data[i].name,
'证件号': data[i].idNoMarked,
'单位': data[i].laborCompanyName,
'工种': data[i].workerTypeName,
'班组': data[i].workerGroupName,
'考勤时间': moment(data[i].attendanceTime).format('YYYY-MM-DD HH:mm:ss'),
'考勤方式': data[i].attendanceType == '1' ? '门禁' : data[i].attendanceType == '2' ? '人脸识别' : '',
'进出场': data[i].entry == '1' ? '进场' : data[i].entry == '-1' ? '出场' : '',
}
dataTable.push(obj);
}
}
}
option.fileName = '考勤数据'
option.datas = [
{
sheetData: dataTable,
sheetName: 'sheet',
sheetFilter: ['考勤图片', '考勤人员', '证件号', '单位', '工种', '班组', '考勤时间', '考勤方式', '进出场'],
sheetHeader: ['考勤图片', '考勤人员', '证件号', '单位', '工种', '班组', '考勤时间', '考勤方式', '进出场'],
}
];
let toExcel = new ExportJsonExcel(option);
toExcel.saveExcel();