Modify existing Excel File using node.js

jengfad picture jengfad · Oct 5, 2016 · Viewed 29.8k times · Source

Is there any way to modify existing excel file in node.js? I've looked into exceljs but it does not provide any functionality that will just modify the existing data. It seems like it will write to a new stream.

Or did I miss something on exceljs?

Thanks in advance.

Answer

Clarence Leung picture Clarence Leung · Oct 5, 2016

exceljs does let you modify Excel spreadsheets.

Here's an example of reading in an existing spreadsheet and writing it back out to a different file:

var Excel = require('exceljs');
var workbook = new Excel.Workbook();

workbook.xlsx.readFile('old.xlsx')
    .then(function() {
        var worksheet = workbook.getWorksheet(1);
        var row = worksheet.getRow(5);
        row.getCell(1).value = 5; // A5's value set to 5
        row.commit();
        return workbook.xlsx.writeFile('new.xlsx');
    })

If you're using the Streams API with exceljs, you can also pipe your stream into fs.createWriteStream to write to a file as well.