Vikas Gupta
Vikas Gupta

Reputation: 1231

Reading Excel through exceljs module

I am reading excel by using exceljs module. When my excel file in same folder. It works fine.

var workbook = new Excel.Workbook(); 
workbook.xlsx.readFile('Data.xls')
    .then(function() {
        var worksheet = workbook.getWorksheet(sheet);
        worksheet.eachRow({ includeEmpty: true }, function(row, rowNumber) {
          console.log("Row " + rowNumber + " = " + JSON.stringify(row.values));
        });
    });

But When my excel file in some other folder and I try to give fileName with path, It throws console error, that file not found. my file structure is like below:

TestFolder

|------nodemodules/

|------example/js/e2e/fileServer.js

|------data/Data.xls

My Question is How to provide relative path of excel file in readFile(). I want to provide path for Data.xls in fileServer.js file.

Upvotes: 3

Views: 4427

Answers (1)

Vikas Gupta
Vikas Gupta

Reputation: 1231

Well, I was wrong with taking Relative Path for excel file. As you can see the file-structure, I used wrong Relative path.

Relative path for this exceljs module should be taken from its root folder. In my case, the correct path is: 'data/Data.xls'. No matter in which js file, you are going to read. Well In my case, I was reading this Data.xls file from fileServer.js.

var workbook = new Excel.Workbook(); 
workbook.xlsx.readFile('data/Data.xls')
    .then(function() {
        var worksheet = workbook.getWorksheet(sheet);
        worksheet.eachRow({ includeEmpty: true }, function(row, rowNumber) {
          console.log("Row " + rowNumber + " = " + JSON.stringify(row.values));
        });
    });

Upvotes: 1

Related Questions