javascript 如何在angularjs中读取excel文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27523406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:34:54  来源:igfitidea点击:

How to read excel file in angularjs?

javascriptangularjsexcel

提问by Amin Uddin

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.

我尝试阅读 excel 文件以遵循以下教程。 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)**;

回答by agershun

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

以下答案描述了是否要从服务器加载 xlsx 文件。对于上传,还有另一个代码。

OPTION 1:This is a procedure, which works in Alasqllibrary:

选项 1:这是一个程序,适用于Alasql库:

See files: 15utility.jsand 84from.jsfor 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 演示)。