Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
20
rated 0 times [  24] [ 4]  / answers: 1 / hits: 6492  / 4 Years ago, sat, july 11, 2020, 12:00:00

I'm trying to write data into an Excel file using ExcelJS library. I was successfully able to create worksheet and add column data.


However, while trying to implement addRow() or addRows() method, the data is not added into the Excel worksheet.


Here is the code I tried:


 const ExcelJS = require('exceljs');
var workbook = new ExcelJS.Workbook();
var worksheet = workbook.addWorksheet('Payment Data');
worksheet.columns = reportHeaders; //reportHeaders is an array of header objects

I'm able to see the columns created successfully in the excel sheet. The trouble starts from below, where I'm trying to add data (into rows):


1st method :


  worksheet.addRows(excelData);//excelData is an array of data objects

2nd method:


  for(var rowItem in excelData){
worksheet.addRow(excelData[rowItem]);}

However, it seems either of these methods aren't working for me.
Finally, the file is saved:


  workbook.xlsx.writeFile('PaymentData.xlsx')

Is there anything I'm missing? Any help will be appreciated.


More From » node.js

 Answers
3

So the issue with your code was, you were trying to add the data to the columns without specifying the key property in the columns array and hence it was unable to add the data.


I modified the worksheet.columns array to look something like the following:


worksheet.columns = [
{ header: "A", key: "a" },
{ header: "B", key: "b" },
];

This will solve your problem


[#3214] Thursday, July 9, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jocelynkarsynr

Total Points: 472
Total Questions: 98
Total Answers: 96

Location: Macau
Member since Mon, Nov 16, 2020
4 Years ago
jocelynkarsynr questions
Tue, Feb 8, 22, 00:00, 2 Years ago
Sun, May 10, 20, 00:00, 4 Years ago
Sat, Jan 18, 20, 00:00, 4 Years ago
Thu, Mar 14, 19, 00:00, 5 Years ago
;