import Excel from "exceljs" throwing error on browser TypeError: Cannot read property 'prototype' of undefined

import Excel from "exceljs" throwing error on browser TypeError: Cannot read property 'prototype' of undefined

我正在使用 React 构建我的网页。我想将我的 reducer 的存储数据下载到一个 xlxs 文件中。为此,我正在使用 exceljs 包。 我已经使用 npm i exceljs.

安装了 exceljs

但是当我尝试将 exceljs 导入我的组件时,我在屏幕上看到 TypeError: Cannot read property 'prototype' of undefined

我正在使用命令 import Excel from "exceljs"; 导入。

请查看屏幕截图,以便更好地查看我在屏幕上看到的错误。

我的package.json是:

{
  "name": "xxxxxxx_xxxxx",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "exceljs": "^1.6.0",
    "react": "^16.4.1",
    "react-dom": "^16.4.1",
    "react-intl": "^2.4.0",
    "react-redux": "^5.0.7",
    "react-router": "^4.3.1",
    "react-router-dom": "^4.3.1",
    "react-scripts": "1.1.4",
    "redux": "^4.0.0",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.3.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build:langs": "NODE_ENV='production' ./node_modules/.bin/babel src --out-dir lib",
    "build:locale": "NODE_ENV='production' babel-node scripts/translator.js",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-plugin-react-intl": "^2.4.0"
  }
}

似乎 ExcelJS 的导出对于浏览器来说有点奇怪。我通过这样做让它工作:

import Excel from "exceljs/dist/es5/exceljs.browser";

然后您可以使用 Excel 随心所欲。即:

console.log('excel', Excel);  // Check that it exists
var workBook = new Excel.Workbook(); // Make a new Workbook

我使用以下方法让它工作:

从 "exceljs/dist/exceljs.min.js" 导入 * 作为 Excel;

而不是

从 "exceljs/dist/es5/exceljs.browser" 导入 Excel;