Crash Override
Crash Override

Reputation: 461

Convert XLS to JSON using Sheetjs and FileReader.js

I am trying to convert an uploaded XLSX file to JSON using https://github.com/bgrins/filereader.js to handle the upload and https://github.com/SheetJS for converting the file to JSON. This is my code:

var opts = {
  dragClass : 'drag',
  on: {
    load: function(e, file) {
        var result      = e.target.result;
        var xlsread     = XLSX.read(result, {type: 'binary'});
        var xlsjson     = XLSX.utils.sheet_to_json(xlsread.Sheets.Sheet1);
        console.log(xlsread,xlsjson);
    }
  }
};

$("#file-input, #dropzone").fileReaderJS(opts);

All I get is an empty array

Any suggestion?

Upvotes: 1

Views: 3573

Answers (1)

Crash Override
Crash Override

Reputation: 461

Ok, I solved it setting the output as Array Buffer. Working code:

var opts = {
  readAsDefault: 'ArrayBuffer',
    dragClass : 'drag',
    on: {
    load: function(e, file) {
        var result      = new Uint8Array(e.target.result);
        var xlsread     = XLSX.read(result, {type: 'array'});
        var xlsjson     = XLSX.utils.sheet_to_json(xlsread.Sheets.Sheet1);
        console.log(xlsread,xlsjson);
    }
  }
};

$("#file-input, #dropzone").fileReaderJS(opts);

Upvotes: 2

Related Questions