0

So I'm trying to read an Excel file (.xlsx specifically) using the js-xlsx library shown here. I must have messed up somewhere with the installation, though, because I can't get access to the "XLSX" object that is necessary to do anything with the library.

Specifically, I tried adding a script with src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.10.5/xlsx.full.min.js" to my HTML page. When that didn't work, I installed the library manually with node and used src="js/node_modules/xlsx/dist/xlsx.full.min.js". Neither works; I'm still getting "The global variable 'XLSX' is not defined" whenever I try to use the library in my javascript.

I have a feeling I'm missing something basic, but I can't figure out what. What am I doing wrong?

Edit: Here's the code that I use to open the Excel file.

function handleDrop(e) {
e.stopPropagation();
e.preventDefault();
var files = e.dataTransfer.files;
var i,f;
alert("a");
for (i = 0; i != files.length; ++i) {
f = files[i];
var reader = new FileReader();
var name = f.name;
reader.onload = function(e) {
  var data = e.target.result;
  alert("b");
  var workbook;
  if(rABS) {
    /* if binary string, read with type 'binary' */
    workbook = XLSX.read(data, {type: 'binary'});
  } else {
    /* if array buffer, convert to base64 */
    var arr = fixdata(data);
    workbook = XLSX.read(btoa(arr), {type: 'base64'});
  }

  /* 
   * console.log(XLSX.utils.sheet_to_json(ws, {header:1}));
   */
  alert("henlo world");
};
if(rABS) reader.readAsBinaryString(f);
else reader.readAsArrayBuffer(f);
}
}

None of the alerts go off, and the "XLSX" variable is marked as undeclared.

anon
  • 35
  • 5
  • Can you please past the code that you used to open the excel? – Karpak Jun 23 '17 at 16:20
  • Sure. It's been added to the main post. – anon Jun 23 '17 at 18:13
  • Have you associated the dragover event in addition to the drop event for that HTML element? If not, can you associate the dragover event and try? Just by including the library, I tried in all browser, it works perfectly fine. Only if I remove the dragover even I am not getting any of the alerts. – Karpak Jun 24 '17 at 15:36
  • Associating the dragover works insofar as it makes the handleDrop() function run. However, e.dataTransfer.files (and e.target.files) are both undefined. I suspect this is because I'm only dragging the file over the element, not actually dropping it, so no data is actually being transferred. Do you know anything that might help that? – anon Jun 26 '17 at 15:43

0 Answers0