react使用(react-html-table-to-excel)把table导出为Excel以及配合antd使用

项目里有个把表格导出为Excel的功能,使用的是react-html-table-to-excel实现的也很简单,记录一下。

1.实现过程

1.下载依赖:npm install --save react-html-table-to-excel

npm install --save react-html-table-to-excel

2.导入:import ReactHTMLTableToExcel from 'react-html-table-to-excel' //导表

import ReactHTMLTableToExcel from 'react-html-table-to-excel' //导表

 3.具体实现过程

import React from 'react'
import ReactDOM from 'react-dom'
import ReactHTMLTableToExcel from 'react-html-table-to-excel' //导表

class TableExport extends React.Component {
 render(
 <div>
    <ReactHTMLTableToExcel
                    id="test-table-xls-button"
                    className="download-table-xls-button"
                    table="table-to-xls"
                    filename="tablexls"
                    sheet="tablexls"
                    buttonText="Download as XLS"/>
                <table id="table-to-xls">
                    <tr>
                        <th>Firstname</th>
                        <th>Lastname</th>
                        <th>Age</th>
                    </tr>
                    <tr>
                        <td>Jill</td>
                        <td>Smith</td>
                        <td>50</td>
                    </tr>
                    <tr>
                        <td>Eve</td>
                        <td>Jackson</td>
                        <td>94</td>
                    </tr>
                </table>
 </div>
)
}

 

PropertyTypeDescription
tablestring 表格名字
filenamestring文件名字
sheetstringexcel的sheet名
idstringid
classNamestring类名
buttonTextstringButton text.

4. antd Table导出:因为antd对table进行了封装所以需要借助ref属性来获取然后再设置

import React from 'react'
import ReactDOM from 'react-dom'
import ReactHTMLTableToExcel from 'react-html-table-to-excel' //导表
import { Table } from 'antd'//导入Table组件
class TableExport extends React.Component {
exportTable(){
    const tableCon = ReactDOM.findDOMNode(this.refs['table'])  通过ref属性找到该table
    const table = tableCon.querySelector('table') 获取table
    table.setAttribute('id', 'table-to-xls') //给该table设置属性
}
 render(){
   <div>
   <button onClick={this.exportTable}>导出</button>
     <ReactHTMLTableToExcel
                  id="test-table-xls-button"
                  table="table-to-xls"
                  filename="tablexls"
                  sheet="tablexls"
                 />
               <Table
                  ref="table"
                  columns={this.columns}      //替换成自己的
                  dataSource={this.state.data} //替换成自己的
                />
 </div>
}
}

npm react-html-table-to-excel的官网网址API

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

崽崽的谷雨

漫漫前端路,摸爬滚打

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值