我想使用角度代码读取Excel文件。 我的文件存储在本地系统的某个位置,例如C:/data/test.xlsx
我已经尝试加载 exceljs的readFile()和load()方法。
他们什么也没读,反而给了错误
答案 0 :(得分:0)
我找到了从Angular代码读取Excel文件的方法。转换文件 放入 ArrayBuffer 并使用 exceljs
读取
import * as Excel from 'exceljs/dist/exceljs.min.js';
compilerOptions: {
paths": {
"exceljs": [
"node_modules/exceljs/dist/exceljs.min"
]
}
}
<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);
});
});
});
}