javascriptjqueryexceljs-xlsxsheetjs

Javascript Read Excel file on server with SheetJS


I am using a library called SheetJS and I want to read an excel sheet that resides on the server without using nodejs, only pure javascript. Is this possible?

There is a message in the documentation that says "readFile is only available in server environments. Browsers have no API for reading arbitrary files given a path, so another strategy must be used"

With the above message, I assume the author is referring to a situation where the file is residing on the client side.

This is what I have done so far

var wb = XLSX.readFile("myFile.xlsx"); //my file is in same directory on server

I get error "xlsx.full.min.js:22 Uncaught TypeError: Cannot read property 'readFileSync' of undefined"


Solution

  • This worked for me

       /* set up async GET request */
        var req = new XMLHttpRequest();
        req.open("GET", url, true);
        req.responseType = "arraybuffer";
    
        req.onload = function(e) {
          var data = new Uint8Array(req.response);
          var workbook = XLSX.read(data, {type:"array"});
    
          /* DO SOMETHING WITH workbook HERE */
        }
    
        req.send();