美文网首页
导出excel表格

导出excel表格

作者: 命题_1f6e | 来源:发表于2019-12-09 09:50 被阅读0次

    安装插件js-export-excel

    npm install js-export-excel
    

    示例

    1. 引入
    import ExportJsonExcel from 'js-export-excel';
    
    1. 使用
    downloadExcel = () => {
    const data = this.state.data ? this.state.data : '';//表格数据
      var option={};
      let dataTable = [];
      if (data) {
        for (let i in data) {
          if(data){
            let obj = {
              '组织ID': data[i].id,
              '组织代码': data[i].organization_code,
              '组织名称': data[i].organization_name,
            }
            dataTable.push(obj);
          }
        }
      }
      option.fileName = '组织信息'
      option.datas=[
        {
          sheetData:dataTable,
          sheetName:'sheet',
          sheetFilter:['组织ID','组织代码','组织名称'],  
          sheetHeader:['组织ID','组织代码','组织名称'],
        }
      ];
    
      var toExcel = new ExportJsonExcel(option); 
      toExcel.saveExcel();        
    }
    

    导出csv

    export const createCsvFormat = (header, data) => {
      if(!data || data.length < 1 || !header) return false
      let  headerCol = ""
      let  mainCol = ""
      header.forEach(item => {
        headerCol += item.strTitle + ","
      })
      headerCol += "\r\n"
    
      data.forEach((item) => {
        let col = ""
        item.forEach(t => {
          col += t.strValue + ","
        })
        mainCol += col + "\r\n"
      })
      return headerCol + mainCol
    }
    
    
    export const exportCsvFile = (data, fileName) => {
      data = "\ufeff" + data;
      let blob = new Blob([data], { type: 'data:text/csv;charset=utf-8;\uFEFF' });
      let url = window.URL.createObjectURL(blob);
      let link = document.createElement("a");
      link.href = url;
      link.download = fileName + ".csv";
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link)
      URL.revokeObjectURL(url);
    }
    
    let data = createCsvFormat (表格头部信息, 内容)
    exportCsvFile (data , 'xxxx')
    
    

    相关文章

      网友评论

          本文标题:导出excel表格

          本文链接:https://www.haomeiwen.com/subject/fwfbgctx.html