0

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");
    }

3 Answers3

1

I think you forget add await workbook.commit(); before console.log("Success");

  • exports.testExcelCreation = async function () { const fileName = "assets/Uploads/Reports/TEST/test_report_" + (new Date()).getTime() + ".xlsx"; const options = { filename: fileName, useStyles: true, useSharedStrings: true }; const workBook = new ExcelJs.stream.xlsx.WorkbookWriter(options); const workSheet = workBook.addWorksheet("sheet 1"); await workSheet.commit(); console.log("Success"); } – Gopal Dutt Aug 28 '20 at 09:34
  • Still facing issue – Gopal Dutt Aug 28 '20 at 09:35
  • guy, `workBook.commit()` not `workSheet.commit()`, replace `workSheet` bor `workBook`. – Piotr Stefański Aug 29 '20 at 09:34
  • `const ExcelJs = require('exceljs'); const fileName = './' + (new Date()).getTime() + '.xlsx'; const options = { filename: fileName, useStyles: true, useSharedStrings: true }; const workBook = new ExcelJs.stream.xlsx.WorkbookWriter(options); workBook.addWorksheet("sheet 1"); workBook.commit().then(() => console.log("Success")); ` – Piotr Stefański Aug 29 '20 at 09:34
0

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.

0

for me it is axios responseType issue. I am not passing the responseType in axios params

Wrong:

const res = await axios.get(`${process.env.url}/excel`, { params: { type: "excelFormat"} })

Correct:

const res = await axios.get(`${process.env.url}/marketplace/seller/excel`, { params: { type: "excelFormat"}, responseType: "arraybuffer" })
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129