从“ exceljs”导入Excel在浏览器上引发错误TypeError:无法读取未定义的属性“ prototype”

时间:2018-08-14 07:48:39

标签: reactjs exceljs

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

但是,当我尝试将exceljs导入我的componenet时,屏幕上出现了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 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我使用以下方法使它起作用:

从“ exceljs / dist / exceljs.min.js”将*作为Excel导入;

而不是

从“ exceljs / dist / es5 / exceljs.browser”导入Excel;