如何利用react将Table下载成excel

import React from 'react'
import ReactDOM from 'react-dom'
import ReactHTMLTableToExcel from 'react-html-table-to-excel'//主要是这个插件!!
import { Table } from 'antd';
import reqwest from 'reqwest';//这个插件是用于请求数据的

const columns = [{
  title: 'Name',
  dataIndex: 'name',
  sorter: true,
  render: name => `${name.first} ${name.last}`,
  width: '20%',
}, {
  title: 'Gender',
  dataIndex: 'gender',
  filters: [
    { text: 'Male', value: 'male' },
    { text: 'Female', value: 'female' },
  ],
  width: '20%',
}, {
  title: 'Email',
  dataIndex: 'email',
}];

export default class WalletInfo extends React.Component {
  state = {
    data: [],
    pagination: {},
    loading: false,
  };

  handleTableChange = (pagination, filters, sorter) => {
    const pager = { ...this.state.pagination };
    pager.current = pagination.current;
    this.setState({
      pagination: pager,
    });
    this.fetch({
      results: pagination.pageSize,
      page: pagination.current,
      sortField: sorter.field,
      sortOrder: sorter.order,
      ...filters,
    });
  }

  fetch = (params = {}) => {//ajax请求api
    console.log('params:', params);
    this.setState({ loading: true });
    reqwest({
      url: 'https://randomuser.me/api',
      method: 'get',
      data: {
        results: 10,
        ...params,
      },
      type: 'json',
    }).then((data) => {
      const pagination = { ...this.state.pagination };
      // Read total count from server
      // pagination.total = data.totalCount;
      pagination.total = 200;
      this.setState({
        loading: false,
        data: data.results,
        pagination,
      });
    });
  }

  componentDidMount() {
    this.fetch();
    const tableCon = ReactDOM.findDOMNode(this.refs['tables'])//利用reactdom.finddomnode()来获取真实DOM节点
    const table = tableCon.querySelector('table')
    table.setAttribute('id','table-to-xls')//为table设置id属性
  }

  render() {
    return (
        

  //应用该下载按钮插件
                               className="downloadButton"
                      table="table-to-xls"
                      filename="钱包信息"
                      sheet="钱包信息"
                      buttonText="导出"/>
                ref='tables'
        columns={columns}
        rowKey={record => record.login.uuid}
        dataSource={this.state.data}
        pagination={this.state.pagination}
        loading={this.state.loading}
        onChange={this.handleTableChange}
      />
     

    );
  }
}

你可能感兴趣的:(前端,ES6,前端)