3

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"

2 Answers 2

8

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

1 Comment

this didn't work for me - specially type: "array" gives me null.. if i change it to type: "buffer"- i get one sheet.. but the data is random numbers and not something my excel has - stackoverflow.com/questions/59214951/…
1

I had many issues with reading the file server-side, with a number of errors including type error, charCodeAt. So this provides a client and server-side solution using a reader. The excel file comes from a file upload button, and uses node.js. Client-side:

let excelInput = document.getElementById("fileToUpload");
let excelFile = excelInput.files[0];
let reader = new FileReader();

So you get the file using files[0] from that element and create a fileReader.

You can see Aymkdn's solution on Github. https://github.com/SheetJS/sheetjs/issues/532. It uses the Uint8Array to work.

reader.readAsArrayBuffer(excelFile);
reader.onload = function() {
  excelArray = new Uint8Array(reader.result); //returns Uint8Array using the result of reader

  let binary = "";
  var length = excelArray.byteLength;
  for (var i = 0; i < length; i++) {
    binary += String.fromCharCode(excelArray[i]); 
    //uses a for loop to alter excelArray to binary
  }

  let formData = new FormData(); //create form data
  formData.append("excel", binary); //append binary to it

  fetch('/excel', {method: "POST", body: formData}) //post as normal
  .then((data) => {
    console.log('Success:', data);
  })
  .catch((error) => {
    console.error('Error:', error);
  });
}

Server-side:

app.post('/excel', function(req, res) {
    let data = req.body;
    var workbook = sheetJS.read(data, {type: 'buffer'});
    console.log("workbook is", workbook);
    res.send();
}

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.