zoukankan      html  css  js  c++  java
  • react 前端导出Excel

    1.首先下载 js-export-excel

       npm install js-export-excel;

    2.下载 xlsx

       npm install xlsx;

    3.引入

       import * as XLSX from 'xlsx'
       import ExportJsonExcel from 'js-export-excel'
     
    4.定义方法
       
       downloadFileToExcel = () => {
            let option = {};  //option代表的就是excel文件
            option.fileName = 'demo表';  //excel文件名称
            option.datas = [
                {
                    sheetData: this.state.dataTable,  //excel文件中的数据源
                    sheetName: 'demo',  //excel文件中sheet页名称
                    sheetFilter: ['你的名称', '我的名称', '你的编号', '我的方案'],  //excel文件中需显示的列数据
                    sheetHeader:['你的名称', '我的名称', '你的编号', '我的方案']  //excel文件中每列的表头名称
                }
            ]
            let toExcel = new ExportJsonExcel(option);  //生成excel文件
            toExcel.saveExcel();  //下载excel文件
        }
     
    5.调用后台接口获取数据源
       
       axios.post('后台接口地址').then(res=>{
             this.state.dataTable = [];
             data.map(item=>{
                  // 表头一一对应
                  this.state.dataTable.push(
                            {
                                '你的名称':item.YourName,
                                '我的名称':item.MyName, 
                                '你的编号':item.YourNo, 
                                '我的方案':item.MyNo
                            }
                        )
                  })
                 // 调用导出excel方法
                 this.downloadFileToExcel()
       })
     
    这样就完完整整的实现了前端导出excel的方法了
     
     
  • 相关阅读:
    第二周作业
    第一周作业
    抓老鼠啊~亏了还是赚了?
    打印沙漏
    寒假作业2
    我与老师
    自我介绍
    第九周课程总结&实验报告(七)
    第八周课程总结&实验报告(六)
    第七周实验报告(五)&周总结
  • 原文地址:https://www.cnblogs.com/ranyonsue/p/15726429.html
Copyright © 2011-2022 走看看