Syed Faizan Syed Faizan - 1 year ago 160
Node.js Question

how to append to a excel file in nodejs

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){
alert("File is readOnly or is being used by another application, please close it and continue!");

Answer Source

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

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();

// Add an array of rows 
var rows = [
    [5,'Bob',new Date()], // row by array 
    {id:6, name: 'Barbara', dob: new Date()}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download