This post was most recently updated on July 31st, 2024
To read excel file in nodejs application we required exceljs to be installed.
In protractor to read excel file, we need exceljs library along with node and protractor.
Here are the steps to install exceljs library.
- Go to the folder location where you want to install exceljs library.
- Type npm install exceljsand press Enter.
Once library installed on your machine you will see:
Once exceljs library installed, we can read excel file using below code snippet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//Excel filePath="D:\\DataFiles\\SampleExcel.xlsx" readExcelFile(filePath) { var Excel = require("exceljs"); var workbook = new Excel.Workbook(); //Use then function to executed code that need to perform immediately after readFile workbook.xlsx.readFile(filePath).then(function () { //Use sheetName in getWorksheet function var worksheet = workbook.getWorksheet("SheetInfo"); //Use nested iterator to read cell in rows //First iterator for rows in sheet worksheet.eachRow({ includeEmpty: false }, function (row, rowNumber) { console.log("Current Row:" + rowNumber); //Second iterator for cells in row row.eachCell({ includeEmpty: false }, function (cell, colNumber) { //print row number, column number and cell value at[row][col] console.log("Cell Value=" + cell.value + " for cell [" + rowNumber + "]" + "[" + colNumber + "]"); /* write code */ }); }); }); } |
Input file used:
Output Generated:
What if our excel file cells contain images? How to save them into DB