我将数据导出到Excel,但我会收到此错误:无法读取未定义的属性(Reading'编辑')

发布于 2025-01-23 18:34:18 字数 899 浏览 2 评论 0 原文

我想将我的材料表数据导出到Excel,但是当我单击下载时,我会在控制台中获得错误: 无法读取未定义的属性(读取')

(我使用“ xlsx”:“^0.18.0”),

<div onClick={downloadExcel} className="downloadExcel">
      <img src="/assets/excel-svgrepo-com.svg" />
</div>

而我的downloadexcel函数是:

  const downloadExcel = () => {
    const newData = Apiary.map((row) => {
      delete row.tableData;
      return row;
    });
    const workSheet = XLSX.utils.json_to_sheet(newData);
    const workBook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(workBook, workSheet, "students");
    //Buffer
    let buf = XLSX.write(workBook, { bookType: "xlsx", type: "buffer" });
    //Binary string
    XLSX.write(workBook, { bookType: "xlsx", type: "binary" });
    //Download
    XLSX.writeFile(workBook, "لیست زنبورستان.xlsx");
  };

当我onclick img img时,我的反应是冻结的,并停止 如何解决此错误?

I Want to export my material table data to Excel but when I click to download I get error in console:
Cannot read properties of undefined (reading 'editing')

(I use "xlsx": "^0.18.0")

<div onClick={downloadExcel} className="downloadExcel">
      <img src="/assets/excel-svgrepo-com.svg" />
</div>

and my downloadExcel function is:

  const downloadExcel = () => {
    const newData = Apiary.map((row) => {
      delete row.tableData;
      return row;
    });
    const workSheet = XLSX.utils.json_to_sheet(newData);
    const workBook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(workBook, workSheet, "students");
    //Buffer
    let buf = XLSX.write(workBook, { bookType: "xlsx", type: "buffer" });
    //Binary string
    XLSX.write(workBook, { bookType: "xlsx", type: "binary" });
    //Download
    XLSX.writeFile(workBook, "لیست زنبورستان.xlsx");
  };

when I onClick img my react is freeze and stop
How can I fix this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2025-01-30 18:34:18

tl; dr:确保您将XLSX库类似: import *从'xlsx'


我与vue.js上的xlsx相似的问题。

我正在使用 import xlsx从'xlsx'导入lib,并在控制台上获取此错误:

[Vue warn]: Error in v-on handler: "TypeError: Cannot read properties of undefined (reading 'utils')"

然后我在终端上注意到(正在运行vue.js应用程序)消息: 在'xlsx'中找不到“导出'默认值'(导入为'xlsx'),

因此我通过将导入更改为: import * as xlsx从'xlsx'<

TL;DR: Make sure you import the XLSX library like this: import * as XLSX from 'xlsx'


I had a similar issue with the XLSX on Vue.js.

I was importing the lib on a component with import XLSX from 'xlsx' and getting this error on the console:

[Vue warn]: Error in v-on handler: "TypeError: Cannot read properties of undefined (reading 'utils')"

And then I noticed on the terminal (which was running the vue.js app) the message: "export 'default' (imported as 'XLSX') was not found in 'xlsx'

So I've resolved it by changing the import to: import * as XLSX from 'xlsx'

手长情犹 2025-01-30 18:34:18

问题似乎是因为XLSX版本。 0.18.0和0.15.1之间有一些破裂的变化。
我能够通过三种方式解决问题:

  1. 通过以文件扩展为XSLX依赖关系(XLSX的较新版本)如下:

import * as xlsx as xlsx as xlsx/xlsx.js;

实际上,实际上,在XLSX的较新版本,有两个可用的文件,即,XLSX/XLSX.JS和
XLSX/XLSX.MJS。默认情况下,App尝试加载.mjs文件,而.mjs文件默认不可导入。

  1. 通过明确定义配置以允许在应用中导入.mjs文件。

  2. 通过在整个项目中使用较低版本的XLSX。 XLSX版本0.15.1将起作用。

Problem seems to be because of xlsx version. There are some breaking changes between 0.18.0 and 0.15.1.
I was able to solve the issue in three ways:

  1. By importing xslx dependency with file extension(with newer version of xlsx) like below:

import * as XLSX from 'xlsx/xlsx.js;

Actually, in newer version of xlsx, there are two files available, i.e, xlsx/xlsx.js and
xlsx/xlsx.mjs. By default, app tries to load .mjs file while .mjs files are not importable by default.

  1. By explicitly defining config to allow import of .mjs files in the app.

  2. By using lower version of xlsx across whole project. xlsx version 0.15.1 will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文