NodeJS 无法读取未定义的属性(正在阅读“F_OK”)

9rygscc1  于 2023-02-12  发布在  Node.js
关注(0)|答案(1)|浏览(425)

读取文件得到Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'F_OK')
Excel表格:^4.3.0
NodeJS :版本14.18.2
重现步骤

const wb = new ExcelJS.Workbook();

const fullPath = path.resolve("excelName.xlsx");
await wb.xlsx.readFile(fullPath );

const buffer = await wb.xlsx.writeBuffer();

yc0p9oo0

yc0p9oo01#

对我来说是这样的:

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文件作为数组缓冲区,并使用excel不使用方法readFile,但使用load

相关问题