首页 > 解决方案 > 'ReferenceError: cptable is not defined' 运行 Jest 测试时,生成 excel 工作表的实际代码工作正常

问题描述

堆栈跟踪如下。

Test suite failed to run

ReferenceError: cptable is not defined

  1 | import React, { Component } from 'react';
> 2 | import ReactExport from 'react-export-excel';
    | ^

  at cptable (node_modules/react-export-excel/node_modules/xlsx/xlsx.js:10:37)
  at Object.<anonymous> (node_modules/react-export-excel/node_modules/xlsx/xlsx.js:6:1)
  at Object.require (node_modules/react-export-excel/dist/ExcelPlugin/components/ExcelFile.js:19:13)
  at Object.require (node_modules/react-export-excel/dist/index.js:7:18)
  at Object.<anonymous> (components/CustomReport.jsx:2:1)

如果我跟踪跟踪,它会到达以下代码部分

在 cptable (node_modules/react-export-excel/node_modules/xlsx/xlsx.js:10:37)

if(typeof cptable === 'undefined') cptable = require('./dist/cpexcel');

我已经看到 github 问题,他们建议从组件代码中删除“use strict”,但我什至没有使用它。这是相关问题https://github.com/SheetJS/sheetjs/issues/324

标签: reactjsjestjs

解决方案


好吧,这个图书馆没有帮助,所以用另一个图书馆。

react-html-table-to-excel

在这里,您需要创建一个 html 表格,然后将该表格转换为 excel。我使用 CSS 隐藏了表格,所以我只能得到导出 excel 按钮。


推荐阅读