Search code examples
node.jsexceljs

How to Append Existing Excel File in NodeJS using ExcelJS


How can I append to the last open row of an already existing Excel file in NodeJS using ExcelJS?.


Solution

  • You can use lastRow Attribute to get the last row of the current sheet. Then increment that value and add new records to the new Row.

    let nameFileExcel = 'YourExcelFile.xlsx'
    var workbook = new exceljs.Workbook();
    workbook.xlsx.readFile(nameFileExcel)
    .then(function()  {
        var worksheet = workbook.getWorksheet(1);
        var lastRow = worksheet.lastRow;
        var getRowInsert = worksheet.getRow(++(lastRow.number));
        getRowInsert.getCell('A').value = 'New Value';
        getRowInsert.commit();
        return workbook.xlsx.writeFile(nameFileExcel);
    });
    

    Reference: Click Here