gpt4 book ai didi

node.js - 如何将大型 JSON 数据导出到 Excel 文件

转载 作者:行者123 更新时间:2023-12-04 20:10:32 27 4
gpt4 key购买 nike

我正在尝试使用 node.js 将 250000 个对象的数组导出到 excel 文件中。但服务器每次都会失败:

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory



我总是用 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');

这是 report的内容大批:
[
{
"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
]

我也尝试使用 mongo-xlsx模块,但它失败并出现同样的错误......

有什么方法可以提高我的代码效率以减少内存使用量?或者也许有更好的方法来做到这一点?

最佳答案

您可以使用 exceljs Node 模块并流式传输数据

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();

引用: http://wiki.workassis.com/node-js-write-large-data-to-excel/

关于node.js - 如何将大型 JSON 数据导出到 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50229263/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com