how to append to a excel file in nodejs

10,431

Have you tried Exceljs. It helps in Read, manipulate and write spreadsheet data and styles to XLSX and JSON.

check on the below link for details

https://www.npmjs.com/package/exceljs

Your problem of adding rows or append rows is solved here:

worksheet.addRow({id: 1, name: 'John Doe', dob: new Date(1970,1,1)});

var rowValues = [];
rowValues[1] = 4;
rowValues[5] = 'Kyle';
rowValues[9] = new Date();
worksheet.addRow(rowValues);

// Add an array of rows 
var rows = [
    [5,'Bob',new Date()], // row by array 
    {id:6, name: 'Barbara', dob: new Date()}
];
worksheet.addRows(rows);
Share:
10,431
Syed Faizan
Author by

Syed Faizan

Updated on June 17, 2022

Comments

  • Syed Faizan
    Syed Faizan about 2 years

    i am currently working on a form which will take user input and add it to one row of an excel sheet, as of now i have managed to make excel sheet using 3rd party plugins(node-xls to be specific). issue arises if i wanna add another row to the excel , it deletes the old entry and adds the new instead of appending the data to the next row.

    tried reading the excel and then concatenating the buffers and writing the file again, but turns out it is corrupting the file and is rendering it unusable.

    How do i append data to the end of the excel sheet? i am new to nodejs and buffers

    var fs = require('fs');
    var NodeXls = require('node-xls');
    var tool = new NodeXls();
    var xls = tool.json2xls({firstName: "arjun", lastName: "u", dob:"12/3/2008"}, {order:["firstName", "lastName", "dob"]});
    fs.appendFile('output.xlsx', xls, 'binary', function(err){
    if(err)
      alert("File is readOnly or is being used by another application,  please close it and continue!");
    });
    
  • Admin
    Admin almost 7 years
    Use proper formatting and, how do you know it must solve the issue?
  • Admin
    Admin almost 7 years
    But why does it must solve it? Would it not work on some spreadsheets?
  • thinkmore
    thinkmore almost 4 years
    If there are millions of data, storing to worksheet variable would cause heap out of memory error. So if much data, it would be good to write file as junk. How can I implement that using exceljs?
  • hu7sy
    hu7sy over 2 years
    @thinkmore do you find solution for heap out of memory?