react 简单的表格导出

js-export-excel(纯js 支持中文) ES6 module

当页面需要导出简单的excel时,如果需要纯前端实现,可以使用js-export-excel

首先在项目当前目录下安装 npm install js-export-excel
其次在需要导出表格的页面引入该组件 import ExportJsonExcel from “js-export-excel”;
再然后就可以开始写代码了,以下为具体实现内容:

// 点击按钮,在render中
<Button onClick={this.downloadExcel}> 导出excel表格 </Button>

// 调用下载excel方法
const downloadExcel = () => {
  // dataHistory 是列表数据
  var option = {};
  var dataTable = [];
  if (dataHistory) {
    for (let i in dataHistory) {
       if (dataHistory) {
        let obj = {
          价格: dataHistory[i].price,
          数量: dataHistory[i].count,
          累计金额: dataHistory[i].amounts,
          时间: dataHistory[i].times,
        };
          dataTable.push(obj);
        }
      }
    }
    option.fileName = '历史成交';
    option.datas = [
      {
        sheetData: dataTable,
        sheetName: 'sheet',
        sheetFilter: ['价格', '数量', '累计金额', '时间'],
        sheetHeader: ['价格', '数量', '累计金额', '时间'],
      },
    ];

    const toExcel = new ExportJsonExcel(option); // new
    toExcel.saveExcel();
};
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容