i am using exceljs and try to write value in a cell but it does not working. However workbook.xlsx.readFile(filename)
is working but workbook.xlsx.writeFile(filename)
won't.
Here is my code:-
var Excel = require('exceljs');
var fs = require('fs')
module.exports.summary = function(req, res, next) {
try {
var filename = process.cwd() + '/template/report/summary.xlsx';
var workbook = new Excel.Workbook();
workbook.xlsx.writeFile(filename)
.then(function() {
var worksheet = workbook.getWorksheet(1);
console.log('worksheet',worksheet);
var row = worksheet.getRow(5);
row.getCell('C').value = new Date();
row.commit();
worksheet.commit();
workbook.commit().then(function() {
console.log('xls file is written.');
});
res.json({ msg: done })
});
} catch (e) {
next(e);
}
}
Try this code, Hope this will help you
const excel = require('exceljs');
//Creating New Workbook
var workbook = new excel.Workbook();
//Creating Sheet for that particular WorkBook
var sheetName = 'Sheet1';
var sheet = workbook.addWorksheet(sheetName);
//Header must be in below format
sheet.columns = [{key:"name", header:"name"}, {key: "age", header: "age"}];
//Data must be look like below, key of data must be match to header.
var data = [{name:"Kalai", age: 24}, {name:"Vignesh", age:24}];
//adding each in sheet
for(i in data){
sheet.addRow(data[i]);
}
//Finally creating XLSX file
var fileName = "Sample.xlsx";
workbook.xlsx.writeFile(fileName).then(() => {
callback(null);
});