Node.js 如何将大型JSON数据导出到excel文件
我正试图使用Node.js 如何将大型JSON数据导出到excel文件,node.js,excel,excel4node,Node.js,Excel,Excel4node,我正试图使用node.js将250000个对象的数组导出到excel文件,但服务器每次都会失败,原因是: 致命错误:调用和重试上次分配失败-JavaScript堆内存不足 我总是用npm--max old space size=8000 start启动服务器 这是我试图使用excel4node模块生成excel文件的代码部分: // Create a new instance of a Workbook class var wb = new xl.Workbook(); // Add Work
node.js
将250000个对象的数组导出到excel文件,但服务器每次都会失败,原因是:
致命错误:调用和重试上次分配失败-JavaScript堆内存不足
我总是用npm--max old space size=8000 start启动服务器
这是我试图使用excel4node
模块生成excel文件的代码部分:
// Create a new instance of a Workbook class
var wb = new xl.Workbook();
// Add Worksheets to the workbook
var ws = wb.addWorksheet('Sheet 1');
for(const [idx, request] of report.entries()) {
let counter = 1;
for(const [_, val] of Object.keys(request)) {
ws.cell(idx + 1, counter).string(val);
counter++;
}
}
wb.write('Excel.xlsx');
这是报告
数组的内容:
[
{
"Page URL": "http://www.example.com",
"Request URL": "http://use.typekit.net/yse3oeo.js",
"Domain": "typekit.net",
"OP Tag Category": "Data Management",
"OP Tag Name": "Adobe TypeKit",
"Registrar Name": "Adobe Systems Incorporated",
"Method": "GET",
"Type": "Script",
"mimeType": "text/javascript",
"Status": 200,
"Content Encoding": "gzip",
"Encoded Data Length": 8028,
"Action": null,
"IP Address": "92.123.20.219",
"Geolocation": "FR",
"Match Regex": null,
"Not Match Regex": null,
"Error": null,
"Chrome Initiator": "http://example.com",
"Final Page URL": null,
"Initial Page Status": null
}
...250000 more objects
]
我还尝试使用mongoxlsx
模块,但失败了,出现了相同的错误
有没有办法提高代码效率以减少内存使用?或者有更好的方法可以做到这一点?您可以使用exceljs节点模块并流式传输数据
var options = {
filename: './streamed-workbook.xlsx',
useStyles: true,
useSharedStrings: true
};
var workbook = new Excel.stream.xlsx.WorkbookWriter(options);
worksheet.addRow({
id: i,
name: theName,
etc: someOtherDetail
}).commit();
请参阅:python中有一个库名panda,它是为大量数据而构建的。您可以知道xl.Workbook
必须支持以块形式生成xlsx文件(并将其保存在磁盘上)。然后,您可以使用该文件或将该文件发送给download@nitishk72谢谢,但我使用的是node.js…它看起来像是excel4node
库总是在内存中构建XLSX XML树。对于如此大的数据,您可能需要另一个库。如果您只需要数据可以通过Excel打开(而不是严格意义上的XLSX文件),那么可以将其编写为纯XML(与原始JSON结构类似,但使用XML)。Excel在解析这方面出人意料地出色。当然,CSV和TSV也可以是您的选择。