如何在 JavaScript 中创建 250MB 到 750 MB 的 xlsx 文件?
我正在尝试从 json 创建 XLSX 文件,但是当我尝试此代码时:
const convertJsonToExcel = () => {
//var objeto=JSON.parse(JSON.stringify(jsonData))
console.log(typeof(bigJson))
const workSheet = XLSX.utils.json_to_sheet(bigJson['titulo']);
const workBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workBook, workSheet, "students")
// Generate buffer
XLSX.write(workBook, { bookType: 'xlsx', type: "buffer" })
// Binary string
XLSX.write(workBook, { bookType: "xlsx", type: "binary" })
XLSX.writeFile(workBook, "studentsData.xlsx")
}
我得到:致命错误:达到堆限制分配失败 - JavaScript 堆内存不足
此 json 文件为 132MB ,但我的文件最多可达 250MB。我寻找了 ExcelJS 库,但它也有一些问题: https://github.com/exceljs /exceljs/issues/709
什么是好的策略?
编辑:
const convertJsonToExcel = () => {
var arr=bigJson['titulo']
// arr2=arr.slice(arr.length/2, arr.length-1)
// arr=arr.slice(0, arr.length/2)
// arr3=arr.slice(0, 1)
console.log(typeof(bigJson))
const workSheet = XLSX.utils.json_to_sheet(arr);
//XLSX.utils.sheet_add_json(workSheet,arr2,{origin: -1});
const workBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workBook, workSheet, "students")
XLSX.writeFile(workBook, "studentsData.xlsx")
}
我试图从 500MB 的 json 创建一个 xlsx 文件,但它只能继续工作而没有响应。这适用于 250MB 的文件。
我尝试将注释部分分成两个块 json 文件,但没有再次得到响应
I'm trying to create a XLSX file from json, but when I'm trying this code:
const convertJsonToExcel = () => {
//var objeto=JSON.parse(JSON.stringify(jsonData))
console.log(typeof(bigJson))
const workSheet = XLSX.utils.json_to_sheet(bigJson['titulo']);
const workBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workBook, workSheet, "students")
// Generate buffer
XLSX.write(workBook, { bookType: 'xlsx', type: "buffer" })
// Binary string
XLSX.write(workBook, { bookType: "xlsx", type: "binary" })
XLSX.writeFile(workBook, "studentsData.xlsx")
}
I get: FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
This json file is 132MBs, but I'll have files until 250MBs. I looked for ExcelJS library, but it has some issues too : https://github.com/exceljs/exceljs/issues/709
What would be a good strategy?
EDIT:
const convertJsonToExcel = () => {
var arr=bigJson['titulo']
// arr2=arr.slice(arr.length/2, arr.length-1)
// arr=arr.slice(0, arr.length/2)
// arr3=arr.slice(0, 1)
console.log(typeof(bigJson))
const workSheet = XLSX.utils.json_to_sheet(arr);
//XLSX.utils.sheet_add_json(workSheet,arr2,{origin: -1});
const workBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workBook, workSheet, "students")
XLSX.writeFile(workBook, "studentsData.xlsx")
}
I was trying to create a xlsx file from a json of 500MBs, but it only keeps working without response. This works for 250MBs file.
I tried with the commented part dividing in two blocks the json file, and not getting response again
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试使用参数运行脚本来增加最大堆大小?您可能会达到节点的堆限制。
参考: https://nodejs.org/ api/cli.html#--max-old-space-size大小-以兆字节
Did you try to run your script with params to increase your max heap size? You might reach the heap limit of node.
reference: https://nodejs.org/api/cli.html#--max-old-space-sizesize-in-megabytes