admin管理员组

文章数量:1432001

I have tried to read excel file to follow the following tutorial. .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)**;

I have tried to read excel file to follow the following tutorial. http://code.psjinx./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)**;
Share Improve this question asked Dec 17, 2014 at 10:27 Amin UddinAmin Uddin 1,0066 gold badges17 silver badges33 bronze badges 1
  • Could you specify: are you going to upload your Excel file from desktop or load from server? – agershun Commented Dec 22, 2014 at 15:11
Add a ment  | 

1 Answer 1

Reset to default 0

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).

本文标签: javascriptHow to read excel file in angularjsStack Overflow