javascriptnode.jsexceljs

Why my excel is corrupt when adding data?


This code is supposed to add "ABC" in B3, but when writing the document, its size goes to 0ko (original is ~200ko) and Excel refuse to open it up.

const excel = require("exceljs");

const modele = "./myexcel.xlsx"

var workbook = new excel.Workbook();
workbook.xlsx.readFile(modele)
.then(function() {
    var worksheet = workbook.getWorksheet('Personnel');

    worksheet.getCell('B3').value = "ABC";
    
    return workbook.xlsx.writeFile(modele)

});

I don't want to create a new sheet but to write in an existing one

I tried using stream, writing in a new file and different version of this code but it doesn't seems to work.

I've used ExcelJS before for the same purpose and it worked.

The cell B3 is in a table I want to add a lot of data in.

The sheet looks like this :

the sheet looks like this

Versions :


Solution

  • I found out it was because this was an Excel table.

    There's the documentation to use the table with ExcelJS and should have used table.addRow() to add data in the table

    I just edited my file to remove the table and it also works.