8

How do you edit a specific cell in an excel file using JavaScript?

I am trying to do it using ExcelJS but having a hard time in writing to my excel file.

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
Prashant Dewan
  • 291
  • 1
  • 3
  • 11

2 Answers2

13

Thanks for the reply I got it working and here is my code.

var Excel = require('exceljs');
var workbook = new Excel.Workbook();
workbook.xlsx.readFile('file.xlsx')//Change file name here or give file path
.then(function() {
    var worksheet = workbook.getWorksheet('sheet');
    var i=1;
    worksheet.eachRow({ includeEmpty: false }, function(row, rowNumber) {
      r=worksheet.getRow(i).values;
      r1=r[2];// Indexing a column
      console.log(r1);
      i++;
    }); 
    worksheet.getCell('B3').value = "abc";//Change the cell number here
return workbook.xlsx.writeFile('file.xlsx')//Change file name here or give     file path
   });

I was missing this return statement that saves my edited file.

Prashant Dewan
  • 291
  • 1
  • 3
  • 11
3

Here are a couple examples found in the exceljs documentation that may be helpful. https://www.npmjs.com/package/exceljs

// Modify/Add individual cell 
worksheet.getCell('C3').value = new Date(1968, 5, 1);

// write to a file 
var workbook = createAndFillWorkbook();
workbook.xlsx.writeFile(filename)
    .then(function() {
        // done 
    });
cannr
  • 31
  • 2