我想使用 Angular 代码读取 excel 文件。
我的文件存储在我本地系统的某个地方,例如 C:/data/test.xlsx
我已经尝试加载
exceljs 的 readFile() 和 load() 方法。
他们什么也没读,反而给出错误
最佳答案
I found the way to read excel file from Angular code. Convert the file into ArrayBuffer and read it using exceljs
import * as Excel from 'exceljs/dist/exceljs.min.js';
compilerOptions: {
paths": {
"exceljs": [
"node_modules/exceljs/dist/exceljs.min.js"
]
}
}
<input id="uploadBtn" type="file" (change)="readExcel($event)"
class="upload input-common" required />
readExcel(event) {
const workbook = new Excel.Workbook();
const target: DataTransfer = <DataTransfer>(event.target);
if (target.files.length !== 1) {
throw new Error('Cannot use multiple files');
}
/**
* Final Solution For Importing the Excel FILE
*/
const arryBuffer = new Response(target.files[0]).arrayBuffer();
arryBuffer.then(function (data) {
workbook.xlsx.load(data)
.then(function () {
// play with workbook and worksheet now
console.log(workbook);
const worksheet = workbook.getWorksheet(1);
console.log('rowCount: ', worksheet.rowCount);
worksheet.eachRow(function (row, rowNumber) {
console.log('Row: ' + rowNumber + ' Value: ' + row.values);
});
});
});
}
此外,polyfill.ts 必须按此顺序引用..
import 'exceljs/dist/exceljs.min.js';
import 'zone.js/dist/zone';
关于angular - 如何使用exceljs以 Angular 导入EXCEL文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56595103/