'How to read excel file in angularjs?

I have tried to read excel file to follow the following tutorial. http://code.psjinx.com/xlsx.js/ But I have failed to read excel file for undefine situation in the following highlighted line.... I have tried it in IE11.

var reader = new FileReader();

            reader.onload = function(e) {
                var data = e.target.result;
                var workbook = XLSX.read(data, {
                    type: 'binary'
                });

                obj.sheets = XLSXReader.utils.parseWorkbook(workbook, readCells, toJSON);
                handler(obj);
            }

            **reader.readAsBinaryString(file)**;


Solution 1:[1]

The following answer describe, if you are going to load xlsx file from server. For uploading there is another code.

OPTION 1: This is a procedure, which works in Alasql library:

See files: 15utility.js and 84from.js for example

readBinaryFile(filename,true,function(a){
    var workbook = X.read(data,{type:'binary'});
    // do what you need with parsed xlsx
});

// Read Binary reading procedure
// path - path to the file
// asy - true - async / false - sync

var readBinaryFile = utils.loadBinaryFile = function(path, asy, success, error) {
    if(typeof exports == 'object') {
        // For Node.js
        var fs = require('fs');
        var data = fs.readFileSync(path);
        var arr = new Array();
        for(var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
        success(arr.join(""));
    } else {
        // For browser
        var xhr = new XMLHttpRequest();
        xhr.open("GET", path, asy); // Async
        xhr.responseType = "arraybuffer";
        xhr.onload = function() {
            var data = new Uint8Array(xhr.response);
            var arr = new Array();
            for(var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
            success(arr.join(""));
        };
        xhr.send();
    };
};

OPTION 2: you can use Alasql library itself, which, probably, can be easier option.

alasql('SELECT * FROM XLSX("myfile.xlsx",{headers:true,sheetid:"Sheet2",range:"A1:D100"})',
     [],function(data) {
     console.log(res);
});

See the example here (simple Excel reading demo) or here (d3.js demo from Excel).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1