我曾尝试创建一个 excel,但收到提交错误。我的代码有什么问题。如果我删除sheet.commit()
和workbook.commit()
程序工作正常。
const Excel = require('exceljs');
const fs = require('fs');
const workbook = new Excel.Workbook();
const sheet = workbook.addWorksheet("MySheet");
const writeToExcel = fs.createWriteStream("./test/testfile.xlsx");
sheet.columns = [
{ header: 'Id', key: 'id', width: 10 },
{ header: 'Name', key: 'name', width: 40 },
{ header: 'DOB', key: 'dob', width: 10, outlineLevel: 1}
];
let names = ['Windows', 'Mac Os', 'Ubuntu', 'B OS'];
let i = 2;
names.forEach( (singleName) => {
let row = sheet.getRow(i);
row.values = {
id: i-1,
name: singleName,
dob: new Date()
};
row.commit();
i++;
});
sheet.commit();
workbook.commit();
workbook.xlsx.write(writeToExcel)
.then( (response) => {
console.log("Excel file is created with data.");
})
.catch( (error) => {
console.log("Some problem in creating an excel file. Please check for errors...");
});
下面是错误。
λ node excel.js
c:\Personal\node_projects\node-js-playlist-master\excel.js:29
sheet.commit();
^
TypeError: sheet.commit is not a function
at Object.<anonymous> (c:\Personal\node_projects\node-js-playlist-master\excel.js:29:7)
at Module._compile (module.js:624:30)
如果我删除工作表提交,则会在工作簿提交行上引发错误。任何帮助在这里都有用..