如何在 JavaScript 中创建 250MB 到 750 MB 的 xlsx 文件?

发布于 2025-01-09 16:28:20 字数 1494 浏览 4 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔纱相望 2025-01-16 16:28:20

您是否尝试使用参数运行脚本来增加最大堆大小?您可能会达到节点的堆限制。

node --max_old_space_size=4096 app.js

参考: 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.

node --max_old_space_size=4096 app.js

reference: https://nodejs.org/api/cli.html#--max-old-space-sizesize-in-megabytes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文