node.js如何生成excel大文件呢?
用node-xlsx直接传入二维数组生成excel时,报了内存溢出, 代码:
var xlsx = require('node-xlsx');
var buffer = xlsx.build([{name: "mySheetName", data: data}]);
fs.writeFileSync('test.xlsx', buffer);
报错信息:
请问各位大侠,有没有啥方法用node.js来生成excel大文件?
5 回复
用我的ejsExcel 模板引擎,试试, 要文件实在太大,应该用流来生成CSV文件,这样,就不会有大小的限制了, 比如,数据获得一条数据,就暂停,先写入CSV文件,然后再读取下一条数据,如此反复, 就不会有内存限制
直接写成csv格式的文件? 自豪地采用 CNodeJS ionic
@151263 请问有没有其它node.js的库来方便地操作流导入excel呢?
@pauky 你可以试试我的ejsExcel模板引擎看看能否满足你的需求
@151263 谢谢,因为要处理的数据不通过模板引擎,所以我后来选用了xlsx-writestream这个库来用流写excel大文件。该库文档写的示例得写个监听流结束事件和一个小bug,我在这补充一下,以防后来的人踩坑。
var XLSXWriter = require('xlsx-writestream');
var fs = require('fs');
var writer = new XLSXWriter('mySpreadsheet.xlsx', {} /* options */);
var wirteStream = fs.createWriteStream('mySpreadsheet.xlsx')
// After instantiation, you can grab the readstream at any time.
writer.getReadStream().pipe(wirteStream);
// Optional: Adjust column widths
writer.defineColumns([
{ width: 30 }, // width is in 'characters'
{ width: 10 }
]);
// Add some rows
writer.addRow({
"Name": "Bob",
"Location": "Sweden"
});
writer.addRow({
"Name": "Alice",
"Location": "France"
});
// Add a row with a hyperlink
writer.addRow({
"Name": {value: "Bill", hyperlink: "http://www.thegatesnotes.com"},
"Location": "Seattle, Washington"
});
// Finalize the spreadsheet. If you don't do this, the readstream will not end.
writer.finalize();
wirteStream.on('finish', function () {
// finish
});