简体   繁体   English

如何在angularjs中读取Excel文件?

[英]How to read excel file in angularjs?

I have tried to read excel file to follow the following tutorial. 我试图阅读excel文件以遵循以下教程。 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. http://code.psjinx.com/xlsx.js/但在以下突出显示的行中,由于未定义的情况,我无法读取excel文件。...我已在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)**;

The following answer describe, if you are going to load xlsx file from server. 以下答案描述了是否要从服务器加载xlsx文件。 For uploading there is another code. 对于上传,还有另一个代码。

OPTION 1: This is a procedure, which works in Alasql library: 选项1:这是一个过程,可在Alasql库中使用:

See files: 15utility.js and 84from.js for example 查看文件:例如15utility.js84from.js

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. 选项2:您可以使用Alasql库本身,这可能更简单。

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 ). 请参见此处的示例( 简单的Excel读取演示 )或此处( Excel的d3.js演示 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM