前端react和andt导出表格,复杂表头

12 篇文章 0 订阅 ¥9.90 ¥99.00
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ant Design Pro + React 可以使用 `antd` 的 `Table` 组件来实现多级表头表格导出。 首先,需要在表格组件的 `columns` 属性中定义多级表头的结构,例如: ```jsx const columns = [ { title: '姓名', dataIndex: 'name', width: 100, fixed: 'left', children: [ { title: '年龄', dataIndex: 'age', width: 80, sorter: (a, b) => a.age - b.age, }, { title: '地址', dataIndex: 'address', children: [ { title: '街道', dataIndex: 'street', width: 120, }, { title: '城市', dataIndex: 'city', width: 80, }, { title: '邮编', dataIndex: 'zip', width: 80, }, ], }, ], }, { title: '公司', dataIndex: 'company', children: [ { title: '职位', dataIndex: 'position', width: 120, }, { title: '部门', dataIndex: 'department', width: 120, }, ], }, ]; ``` 然后,在表格组件中添加一个按钮,用于触发导出操作。在导出操作中,需要使用 `xlsx` 库将表格数据转换为 Excel 文件并下载,例如: ```jsx import React, { useState } from 'react'; import { Button, Table } from 'antd'; import XLSX from 'xlsx'; const data = [ { key: '1', name: 'John Brown', age: 32, street: 'New York No. 1 Lake Park', city: 'New York', zip: '10001', company: 'ABC Company', position: 'Manager', department: 'Sales', }, // ... ]; const columns = [ // ... ]; const ExportTable = () => { const [loading, setLoading] = useState(false); const handleExport = () => { setLoading(true); setTimeout(() => { const worksheet = XLSX.utils.json_to_sheet(data); const workbook = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1'); XLSX.writeFile(workbook, 'table.xlsx'); setLoading(false); }, 1000); }; return ( <> <Button onClick={handleExport} loading={loading}> 导出表格 </Button> <Table columns={columns} dataSource={data} /> </> ); }; export default ExportTable; ``` 在上面的代码中,使用 `XLSX.utils.json_to_sheet` 将表格数据转换为 Excel 工作表,并使用 `XLSX.utils.book_new` 创建一个新的 Excel 工作簿。然后,使用 `XLSX.utils.book_append_sheet` 将工作表添加到工作簿中,并使用 `XLSX.writeFile` 将工作簿保存为 Excel 文件并下载。最后,使用 `loading` 状态来控制导出操作的加载状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值