Search code examples
node.jsexceljs

How to change msexcell cell format in Node.js by using exceljs


const Excel = require('exceljs');
let workbook = new Excel.Workbook();
let workSheet = workbook.getWorksheet(1);
workSheet.getCell('W2').font = {color: {argb: "004e47cc"}};

this code sets font color for entire row, not just W2 cell. Same happens if I do:

let row2 = workSheet.getRow(2);
row2.getCell(23).font = {color: {argb: "004e47cc"}}; //W2

So how do I set cell style just for certain cell?


Solution

  • It worked for me as well, but your code doesn't work as-is. workbook.getWorksheet(1) is failing, since you haven't created any worksheets in the new workbook you created.

    This code generates a valid .xlsx file where cell W2 has the color you want:

    const Excel = require('exceljs');
    const workbook = new Excel.Workbook();
    const workSheet = workbook.addWorksheet('Sheet');
    workSheet.getCell('W2').font = {color: {argb: "004e47cc"}};
    workbook.xlsx.writeFile('foo.xlsx');