在浏览器上导入Excel时出现错误,错误类型为TypeError: Cannot read property 'prototype' of undefined。

4

我正在使用React构建我的网页。我想将我的Reducer存储数据下载到一个xlxs文件中,所以我使用了exceljs包。 我已经使用npm i exceljs命令来安装exceljs

但是当我尝试将exceljs导入到我的组件中时,屏幕上会显示TypeError: Cannot read property 'prototype' of undefined错误信息。

我使用命令import Excel from "exceljs";进行导入。

请查看屏幕截图以更好地查看我在屏幕上得到的错误信息: enter image description here enter image description here

我的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"
  }
}
2个回答

3
我通过使用以下代码使其工作起来:

import * as Excel from "exceljs/dist/exceljs.min.js";

而不是

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


那个文件不再存在。 - tamj0rd2

2
似乎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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接