我的 Angular-CLI 前端开发服务器在本地运行 localhost:4200
I need to get a local Excel file stored on my PC, read its content and make some calls to an API from the client side.
我正在尝试使用 js-xlsx,安装了
npm install xlsx
但我找不到如何获取文件并读取其内容。如何在 Angular 13 中使用 js-xlsx 导入本地 excel 文件?
最佳答案
这是工作Example
onFileChange(ev) {
let workBook = null;
let jsonData = null;
const reader = new FileReader();
const file = ev.target.files[0];
reader.onload = (event) => {
const data = reader.result;
workBook = XLSX.read(data, { type: 'binary' });
jsonData = workBook.SheetNames.reduce((initial, name) => {
const sheet = workBook.Sheets[name];
initial[name] = XLSX.utils.sheet_to_json(sheet);
return initial;
}, {});
const dataString = JSON.stringify(jsonData);
document.getElementById('output').innerHTML = dataString.slice(0, 300).concat("...");
this.setDownload(dataString);
}
reader.readAsBinaryString(file);
}
关于javascript - 使用 Angular 13 使用 js-xlsx 读取本地 Excel 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57818105/