以角度2读取excel文件 [英] Read excel file in angular 2
本文介绍了以角度2读取excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 ng2-File-upload,我想阅读excel 文件及其所有行并获取总数.有什么方法可以通过使用这个模块来实现它,以及如何通过纯 JavaScript 或打字稿来实现它.
I am using ng2-File-upload,i want to read the excel file and all its rows and get the total count. Is there any way i can achieve it by using this module and how to get it done by plain JavaScript or typescript.
推荐答案
我使用了已接受的答案中提到的包.ng2-file-upload 有一个 uploader
,它有一个回调函数 this.uploader.onAfterAddingFile
.我刚刚调用了我的更改函数并读取了如下文件:
I used the package mentioned in the accepted answer.
ng2-file-upload has a uploader
that has a call back function this.uploader.onAfterAddingFile
. I just called my change function and read the file as below:
onFileChange(file: any) {
/* wire up file reader */
//const target: DataTransfer = <DataTransfer>(evt.target);
//if (target.files.length !== 1) throw new Error('Cannot use multiple files');
const reader: FileReader = new FileReader();
reader.onload = (e: any) => {
/* read workbook */
const bstr: string = e.target.result;
const wb: XLSX.WorkBook = XLSX.read(bstr, { type: 'binary' });
/* grab first sheet */
const wsname: string = wb.SheetNames[0];
const ws: XLSX.WorkSheet = wb.Sheets[wsname];
/* save data */
var data = <any>(XLSX.utils.sheet_to_json(ws, { header: 1 }));
this.totalInventories = data.length > 0 ? data.length - 1 : 0;
};
if (file._file)
reader.readAsBinaryString(file._file);
}
希望对大家有帮助:)
这篇关于以角度2读取excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文