我正在从数据库获取数据并尝试填充 excel 文件。无法填充 Excel 文件。
写入数据的代码如下。
await users();
console.log("data " + JSON.stringify(test));
workbook1.xlsx.writeFile("testing.xlsx").then(function () {
worksheet.addRow(test);
console.log(test);
});
用户功能如下
function users() {
return new Promise(async (resolve, reject) => {
constants.sequelize.query(`SELECT collectionName FROM DB.[collection] WHERE id = '1234567'`).then(user => {
console.log(user);
test = user;
resolve(user);
console.log("out");
}).catch(error => {
reject(error);
});
});
}
“测试”中的数据如下
[[{"collectionName":"some name"}],[{"collectionName":"another name"}]]
编辑:
它应该是
worksheet.addRows(test);
并且应该在“writeFile”之外
最佳答案
我认为你应该使用 addRows
而不是 addRow
:
worksheet.addRows(test); // < --------- as 'test' is array of multiple rows
代替
worksheet.addRow(test);
关于node.js - Exceljs 写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51759527/