exceljs: [BUG] readFile error

readFile gives TypeError: Cannot read property 'F_OK' of undefined

I have

import { Workbook } from "exceljs";
let workbook = new Workbook();
files.forEach((file) => {
const data = workbook.csv.readFile(file.name)
})

Tried with workbook.xlsx.readFile and this gives same error.

I have version 3.8.1 installed.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 21 (4 by maintainers)

Most upvoted comments

Exceljs v4.1.0 : import { Workbook } from 'exceljs/excel' fix the problem with Electron.

I’m using from an Electron window with nodeIntegration: true and still getting this.

I’m able to use other libraries that access the file system (and even fs itself), but I can’t get around this error with exceljs.

image

Workaround if using this in electron

        fs.promises.readFile(path)
            .then(data => {
                workbook.xlsx.load(data.buffer)
                    .then()
                    .catch();
            }).catch();

Hello guys, this exceljs uses different modules in the browser and node environment, you can refer to the following code.

Demo

index.vue

<template>
  <div class="excel">
    <div @click="generatorXlsx">generatorXlsx</div>
    <input type="file" @change="readXlsx" />
  </div>
</template>

<script>
import exceljs from "exceljs/dist/es5/exceljs.browser";
import { saveAs } from "file-saver";

export default {
  name: "ExcelPage",
  data() {
    return {};
  },
  methods: {
    downloadFile(wb, fileName) {
       wb.xlsx
        .writeBuffer()
        .then(buffer => {
          const blob = new Blob([buffer]);
          if (navigator.msSaveBlob) {
            navigator.msSaveBlob(blob, fileName);
          } else {
            console.log("writeFile ok");
            saveAs(blob, fileName);
          }
        })
        .catch(err => {
          console.log("writeFile Fail");
        });
    },
    generatorXlsx() {
      const workbook = new exceljs.Workbook();
      const worksheet = workbook.addWorksheet("sheet 1");
      worksheet.addRow(["a", "b", "c"]);

      // download the file
     this.downloadFile(workbook, 'abc.xlsx')
    },
    async readXlsx(obj) {
      if (!obj.target.files) {
        return;
      }
      const f = obj.target.files[0];
      const workbook = new exceljs.Workbook();
      var fileReader = new FileReader();
      fileReader.onload = (e) => {
        const buffer = e.target.result;
        workbook.xlsx.load(buffer).then(async (wb)=> {
          console.log("readFile success");
         this.downloadFile(wb, 'abcd.xlsx')
        }).catch((error)=> {
          console.log("readFile fail", error);
        })
      };
      fileReader.readAsArrayBuffer(f);
    }
  }
};
</script>

main.js

Related to issue #1213 .

import 'core-js/modules/es.promise';
import 'core-js/modules/es.object.assign';
import 'core-js/modules/es.object.keys';
import 'regenerator-runtime/runtime';
// @ts-ignore
import rewritePattern from 'regexpu-core';
// @ts-ignore
import { generateRegexpuOptions } from '@babel/helper-create-regexp-features-plugin/lib/util';
try {
  new RegExp('a', 'u');
} catch (err) {
  // @ts-ignore
  global.RegExp = function (pattern, flags) {
    if (flags && flags.includes('u')) {
      return new RegExp(
        rewritePattern(
          pattern,
          flags,
          generateRegexpuOptions({ flags, pattern }),
        ),
      );
    }
    return new RegExp(pattern, flags);
  };
  // @ts-ignore
  global.RegExp.prototype = RegExp;
}

If you works on browser environment, fs doesn’t exist. fs exist only on nodejs environment. If you works with Electron, be sure to import exceljs like this : import { Workbook } from 'exceljs/excel'