-5

I need to read excel file in Javascript dynamically. want to iterate cell values using row counts and column count. Is it possible to do so? without converting it into JSON object. However, I revisited many posts in order to resolve the issue @ stackoverflow. but not succeeded.Please suggest some satisfactory solution.

thanks you

Pankaj
  • 17
  • 1
  • 6
  • 2
    Lazy question gives you lazy answer: http://www.databison.com/read-write-to-file-excel-workbook-access-using-javascript-sql/ – Aschab Oct 02 '16 at 14:43
  • Possible duplicate of [Reading Excel file using node.js](http://stackoverflow.com/questions/28860728/reading-excel-file-using-node-js) – AdityaReddy Oct 02 '16 at 15:04
  • Duplicate .. has been answered at http://stackoverflow.com/questions/28860728/reading-excel-file-using-node-js http://stackoverflow.com/questions/31667726/is-there-any-way-to-read-excel-file-in-nodejs – AdityaReddy Oct 02 '16 at 15:04

3 Answers3

0

There's a npm package which would perfectly match with your requirements : xlsx. If you need exemple of code, feel free to ask, I often use it

Here is a basic exemple of parsing a xlsx file : I read the value of the cell 'B1' of the first sheet of the file test.xlsx

'use strict';
var XLSX = require('xlsx');
var workbook = XLSX.readFile('test.xlsx');
var sheetName = workbook.SheetNames[0];
var worksheet = workbook.Sheets[sheetName];
var redValue = worksheet['B1'];
console.log(redValue); 
kevin ternet
  • 4,514
  • 2
  • 19
  • 27
  • most of npm package read excel file and convert it into json file.Can we directly read excel file in Javascript like Apache POI – Pankaj Oct 02 '16 at 14:56
  • most of npm package read excel file and convert it into json file.Can we directly read excel file in Javascript like Apache POI – Pankaj Oct 02 '16 at 14:56
  • Yes with xlsx you can both parse (read) and write excel file as you would do with apache poi. The main difference remains the language (JavaScript instead of Java) – kevin ternet Oct 02 '16 at 19:12
  • By using xlsx package can I use the excel sheet data directly in javascript?. If so, it would be great if you could share code. – Pankaj Oct 02 '16 at 19:22
  • Thanks @kevin, I was having a query regarding how to read the excel values row wise with respect to columns. It would be great if you share some examples. – Pankaj Oct 03 '16 at 05:16
  • I don't understand what you expect when you intend to read the excel values row wise with respect to columns ? – kevin ternet Oct 03 '16 at 18:20
0

I found one solution for reading excel use 'kexcel' npm package.I can do my work with it.But I am stuck at point,how can we get last row number from excel using it.

Pankaj
  • 17
  • 1
  • 6
0

To read from an excel file you can use 2 different methods,

The first one is using the package xlsx, Inside the project folder run the command 'npm i xlsx' on the terminal which will install the necessary packages

var XLSX = require('xlsx');
var UserCred = XLSX.readFile('ExcelFile/UserLogin.xlsx');
var UserLoginWorksheet= UserCred.Sheets['Sheet1'];
var BaseUrl=UserLoginWorksheet['B1'].v;
console.log(BaseUrl);

Here 'BaseUrl' holds the value at column B1 in the excel file

The second one is using the package exceljs, Inside the project folder run the command 'npm i excel' on the terminal which will install the necessary packages.

var Excel = require("exceljs");
 var filePath = 'ExcelFiles/StaffDetails.xlsx';
 var workbook = new Excel.Workbook();
 await workbook.xlsx.readFile(filePath).then(function () {
 //Use sheetName in getWorksheet function
 worksheet = workbook.getWorksheet("Sheet1");
 let cellValue1 = worksheet.getRow(1).getCell(1).value;
 console.log(emptyCell1+"4");
 })
let  cellValue2 = worksheet.getRow(2).getCell(2).value;

Here cellvalue1 holds A1 columns value and cellvalue2 holds B2 column value

sarath
  • 1
  • 1