exceljs: Corrupt xlsx after updating

Hello, great lib, I have an issue with the following code whereby the updated file become corrupt. The val you see in code is a string version of a bool so “true” or “false”, I did hardcode a string as val (“hello”), just to see if it was the bool that caused the issue, but the file still got corrupted. Can you please take a look and see what I may be doing wrong, thanks. If I am able to recover the file, it has the updated value, but my app is no longer able to read the file for subsequent parsing

function updateExcelEntry(inputPage,sheet,colKeyIndex,colIndexToUpdate,rowKey,val){
    //console.log("in excel " + inputPage);
    var workbook = new Excel.Workbook();
    workbook.xlsx.readFile(inputPage).then(function() {
            // use workbook
        //console.log("getting sheet " + sheet);
        var worksheet = workbook.getWorksheet(sheet);
        //console.log("getting keyCol " + colKeyIndex);
        var keyCol = worksheet.getColumn(colKeyIndex);
        keyCol.key = "key";
        var col = worksheet.getColumn(colIndexToUpdate);
        col.key = "colToUpdate";

        worksheet.eachRow(function(row, rowNumber) {

            //console.log("checking key val " + row.getCell("key").value);
            //console.log("vs rowKey " + rowKey);
            if(rowNumber !== 1 && row.getCell("key").value === rowKey){
                //console.log("Row " + rowNumber + " = " + JSON.stringify(row.values));
                row.getCell("colToUpdate").value = val;

            }
        });


    }).then(function() {
        return workbook.xlsx.writeFile(inputPage)
    }).then(function() {
        console.log("workbook updated " + inputPage);
    });
}

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Reactions: 5
  • Comments: 19

Most upvoted comments

I solved the problem by using:

https://www.npmjs.com/package/xlsx-populate

All sorts of small formatting problems simply disappeared. I cannot recommend it enough.

I have same issue. After read the latest comments i make a test. Make a simple script that only read and write on different file.

First test:

  1. Create new file with LibreOffice
  2. run the script
  3. file corrupted

Second test:

  1. Create new file with Google Docs and download as xlsx
  2. run the script
  3. works!!

So something is different in LibreOffice/OpenOffice files.

Nothing here worked as an intended solution for me. Any updates?

Same issue here, using the last version:

"use strict";

const Excel = require("exceljs")
    , unstream = require("unstream")
    ;


// read from a file
var workbook = new Excel.Workbook();
//var workbook = new Excel.stream.xlsx.WorkbookWriter(options);
workbook.xlsx.readFile("input.xlsx").then(function(x) {
    let worksheet = workbook.getWorksheet("My Sheet");
    worksheet.addRow([3, 'Michael']).commit();
    return workbook.xlsx.writeFile("output.xlsx")

    //workbook.xlsx.write(unstream({}, function(data, cb) {
    //    console.log(data.toString("base64"));
    //    cb(null, data.toString('base64'));
    //}).pipe(require("fs").createWriteStream("foooo.xlsx")));
}).then(function () {
    console.log("Saved");
});

I am using it to take an Excel sheet, use it as a template, and change some of the cells programmatically.

I can sadly confirm the problem. Wasted 6 hours on this…

I can confirm that this is happening for any libreoffice-generated xlsx I read and then write (even if no changes are made)

Libreoffice info:

Version: 5.4.2.2
Build ID: 1:5.4.2-0ubuntu0.17.10.1
CPU threads: 8; OS: Linux 4.13; UI render: default; VCL: kde4; 
Locale: en-US (en_US.UTF-8); Calc: group