'Trying to create excel file from ExcelJs but its give me corrupted file
Below code give me the corrupted file please help.
exports.testExcelCreation = async function () {
// construct a streaming XLSX workbook writer with styles and shared strings
const options = {
filename: 'assets/Uploads/Reports/TEST/streamed-workbook.xlsx',
useStyles: true,
useSharedStrings: true
};
const workBook = new ExcelJs.stream.xlsx.WorkbookWriter(options);
const workSheet = workBook.addWorksheet("sheet 1");
console.log("Success");
}
Solution 1:[1]
I think you forget add await workbook.commit();
before console.log("Success");
Solution 2:[2]
I face the similar problem. I tried to update excel file asynchronously from multiple places in my code simultaneously. If we try to open the file in read mode when it was in write mode already, it'll make the file corrupted.
I was stuck in the below error.
Error Error: Corrupted zip or bug: expected 16 records in central dir, got 0
at ZipEntries.readCentralDir (/node_modules/jszip/lib/zipEntries.js:146:23)
at ZipEntries.load (/node_modules/jszip/lib/zipEntries.js:257:14)
at /node_modules/jszip/lib/load.js:48:24
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async XLSX.load (/node_modules/exceljs/lib/xlsx/xlsx.js:279:17)
at async XLSX.readFile (/node_modules/exceljs/lib/xlsx/xlsx.js:55:24)
I carefully gone through my code and found that I've been asynchronously calling update excel method multiple times simultaneously. I made it to be synchronous and removed unwanted code calling update excel method. This fixes my problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Piotr Stefa?ski |
Solution 2 | Arunachalam B |