In this post we will understand how we can read/write excel sheet and styles using ExcelJS [javaScript/TypeScript].
We are using node.js / npm package “ExcelJS” to achieve excel operations.
Installation :
Open terminal [Ctrl + `] and enter
npm install exceljs
or
make an entry to the package.json under dependency and run command npm install
Open any JS editor [let’s say VScode], on your local drive, create a file “ExcelJSTest.js” and open the file in VSCode.
Implementation :
Once installed, create an excel sheet “test.xlsx” under your project folder and follow the below basic code for excel operations
var Excel = require('exceljs'); var wb = new Excel.Workbook(); wb.xlsx.readFile("./test.xlsx").then(function(){ var sh = wb.getWorksheet("Sheet1"); sh.getRow(3).getCell(2).value = 32; wb.xlsx.writeFile("./test.xlsx"); console.log("Row-3 | Cell-2 - "+sh.getRow(3).getCell(2).value); console.log(sh.rowCount); //Get all the rows data [1st and 2nd column] for (i = 1; i <= sh.rowCount; i++) { console.log(sh.getRow(i).getCell(1).value); console.log(sh.getRow(i).getCell(2).value); } })
To run the code, on terminal enter
node ExcelJSTest.js
Output
Row-3 | Cell-2 - 32 3 11 12 21 22 31 32
Reference – ExcelJS
Hi can we use this code in cypress?