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.

4
  • may be you don't have permissions rights? what is your environment? Commented Oct 31, 2017 at 13:05
  • Sorry, I have updated my question. I think I wrong with providing right relative path. Commented Oct 31, 2017 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. Commented Oct 31, 2017 at 15:53
  • Thanks headmax, that worked Commented Nov 1, 2017 at 6:24

1 Answer 1

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));
        });
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.