如何将大型JSON数据导出到Excel文件

时间:2018-05-08 08:30:15

标签: node.js excel excel4node

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

  

致命错误:CALL_AND_RETRY_LAST分配失败 - 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');

这是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模块,但它失败并出现相同的错误......

有没有办法提高代码效率以减少内存使用量?或者可能有更好的方法来做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以使用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();

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