readFile 给出未捕获( promise 中)类型错误:无法读取未定义的属性(读取“F_OK”)
ExcelJS:^4.3.0
Node JS:v14.18.2
重现步骤
const wb = new ExcelJS.Workbook();
const fullPath = path.resolve("excelName.xlsx");
await wb.xlsx.readFile(fullPath );
const buffer = await wb.xlsx.writeBuffer();
最佳答案
对我来说这是这样的:
upload(fileToUpload: File) {
this.readFile(fileToUpload, (data) =>this.readDataFromFile(data));
}
private readFile(fileToUpload: File, callback) {
const reader = new FileReader();
let data;
reader.onload = function () {
data = reader.result;
callback(data);
}
reader.readAsArrayBuffer(fileToUpload);
}
private readDataFromFile(data: any) {
var workbook = new Excel.Workbook();
workbook.xlsx.load(data)
.then(workbook => {
console.log(workbook, 'workbook instance');
workbook.eachSheet((sheet, id) => {
sheet.eachRow((row, rowIndex) => {
console.log(row.values, rowIndex);
});
});
});
}
将 Excel 文件作为数组缓冲区读取,并使用 exceljs 不使用方法 readFile,而是使用 load。
关于node.js - 无法读取未定义的属性(读取 'F_OK' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74016333/