我必须使用 excel4node 以 excel 格式打印一些数据。在数据中,我有包含要传输为 excel 格式的信息的对象。但由于某种原因,它显示错误:TypeError:无法读取未定义的属性“用户名”。在对象内部,我有属性用户名及其值。这里的代码如下:
const wb = new xl.Workbook();
const data = {};
data = {
username: 'name',
department: 'department',
title: 'title',
percentage: 23,
correct: 27,
date: 2021-09-03T16:38:05.107Z
}
const fileName = "Excel.xlsx";
const filePath = path.resolve(__dirname, "../.././public/files/", fileName);
const ws = wb.addWorksheet("Sheet 1");
const style = wb.createStyle({
font: {
color: "FFFFFFFF",
size: 12,
},
});
const form = [
"name",
"name",
"name",
"name",
"name",
"name",
];
for (let i = 0; i < form.length; i++) {
ws.cell(1, i + 1)
.string(form[i])
.style(style);
switch(i) {
case 1:
ws.column(2).setWidth(30);
break;
case 3:
ws.column(4).setWidth(30);
break;
case 4:
ws.column(5).setWidth(30);
break;
case 5:
ws.column(6).setWidth(30);
break;
}
}
for(let i = 1; i <= data.length; i++) {
ws.cell(i + 1, 1).number(i);
ws.cell(i + 1, 2).string(data[i].username);
ws.cell(i + 1, 3).date(data[i].date.toString());
ws.cell(i + 1, 4).string(data[i].department);
ws.cell(i + 1, 5).number(data[i].percentage);
ws.cell(i + 1, 6).number(data[i].correct);
}
wb.write(filePath);