DiegoZoracKy / convert-excel-to-json

Convert Excel to JSON, mapping sheet columns to object keys.
MIT License
290 stars 92 forks source link

excelToJson doesn't work for file with huge amount of rows #69

Closed simyara closed 1 year ago

simyara commented 1 year ago

When file has 461k of rows (single sheet) excelToJson works well, but for file with 469k+ of rows excelToJson returns empty array.

DiegoZoracKy commented 1 year ago

Hi @simyara, take a look at this thread from SheetJS, which is the module in use under the hood here.

https://github.com/SheetJS/sheetjs/issues/61

bloodykheeng commented 5 months ago

hei i think the best soution is to use workers m using react but when i tried workers browser no longer freezez @simyara

 const createWorker = () => {
        const blob = new Blob(
            [
                `
                importScripts('https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.0/xlsx.full.min.js');
                self.onmessage = async function(e) {
                    try {
                        const file = e.data;
                        const data = await file.arrayBuffer();
                        const workbook = XLSX.read(data, { type: 'array' });
                        const worksheet = workbook.Sheets[workbook.SheetNames[0]];
                        const jsonData = XLSX.utils.sheet_to_json(worksheet, {
                            header: 1,
                            defval: "",
                        });
                        self.postMessage({ status: 'success', data: jsonData });
                    } catch (error) {
                        self.postMessage({ status: 'error', error: error.message });
                    }
                };
            `,
            ],
            { type: "application/javascript" }
        );

        return new Worker(URL.createObjectURL(blob));
    };

read excel file

const readExcel = (file) => {
        return new Promise((resolve, reject) => {
            const worker = createWorker();

            worker.onmessage = function (e) {
                if (e.data.status === "success") {
                    resolve(e.data.data);
                } else {
                    reject(new Error(e.data.error));
                }
                worker.terminate();
            };

            worker.onerror = function (e) {
                reject(new Error(e.message));
                worker.terminate();
            };

            worker.postMessage(file);
        });
    };