3

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.

Xcodian Solangi
  • 2,342
  • 5
  • 24
  • 52
Vikas Gupta
  • 1,183
  • 1
  • 10
  • 25
  • may be you don't have permissions rights? what is your environment? –  Oct 31 '17 at 13:05
  • Sorry, I have updated my question. I think I wrong with providing right relative path. – Vikas Gupta Oct 31 '17 at 13:06
  • 1
    No problem, so try a path starting width your public directory name : `public/data/Data.xls` or a relative way `../data/Data.xls` may be accessible doing a `data/Data.xls` depends or your rights too. If you are on linux way go to the root project and do a `pwd` to known wich absolute path using. –  Oct 31 '17 at 15:53
  • Thanks [headmax](https://stackoverflow.com/users/8556290/headmax), that worked – Vikas Gupta Nov 01 '17 at 06:24

1 Answers1

1

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));
        });
    });
Vikas Gupta
  • 1,183
  • 1
  • 10
  • 25