'Node.js append a row to xlsx file

I'm just trying to figure out how to append a single row to an xlsx file. For example, append array = [1 2 3 4 5] to the first empty row using columns 1,2,3,4,5. Ideally I'd start with an empty .xlsx file, and repeatedly rerun this program, which appends a new row to the file each time it is ran.

I'm attempting to use exceljs, but any test writes I try to do to a file say corrupted when I attempt to open them.

edited code (still not working):

var Excel = require('exceljs');
var workbook = new Excel.Workbook();
var sheet = workbook.addWorksheet('rssi');                                                                                                                                                                                                  
file = 'testfile.xlsx'          
var array = [1,2,3,4,5]
sheet.addRow(array)
workbook.xlsx.writeFile(file)
.then(function() {
    console.log('Array added and file saved.')
});

Output is zero bytes and cannot be opened by Microsoft Excel. Says, "The file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."



Solution 1:[1]

Basically, you was trying to add the row in method which was called after file write. Try this example:

var workbook = new Excel.Workbook();                                                                                                                                                                                                      
file = 'testfile.xlsx'
var array = [1,2,3,4,5]
var sheet = workbook.addWorksheet('rssi');
sheet.addRow(array)
workbook.xlsx.writeFile(file)
       .then(function() {
           console.log('Array added and then file saved.')
       });

Solution 2:[2]

But it add only one row at a time

var spread_sheet = require('spread_sheet');

var row = "1,2,Jack,Pirate";
var filePath = '/home/Pranjal/Desktop/test.xlsx';
var sheetName = "Sheet1";

spread_sheet.addRow(row,filePath,sheetName,function(err,result){
console.log(err,result)
})

Solution 3:[3]

Add row to spreadsheet:-

I also stuck with the same functionality issue, so I used 'spread_sheet' module.

It worked for me

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 aring
Solution 2 Daniel F
Solution 3 naresh kaktwan