xlsx.js导出表格数据

 定义导出方法

export function exportExcel(table,fileName,ws_name) {

        //table ---json数据 fileName---导出的文件名   ws_name---sheet名

        //创建book

        var wb = XLSX.utils.book_new();

        // json转sheet

        var ws = XLSX.utils.json_to_sheet(table, { skipHeader:true});

        var timestamp = (new Date()).getTime();

        //sheet写入book

        XLSX.utils.book_append_sheet(wb, ws, ws_name);

        //输出

        XLSX.writeFile(wb,fileName+".xlsx");

}

引用

// 导出
    Export() {
      if (this.selectData && this.selectData.length) {
        let data = JSON.parse(JSON.stringify(this.selectData))
        let filename = '全部项目';
        let ws_name = '全部项目';
        let headerRow = {
          projectName: '项目名称',
          createTime: '创建时间',
          investorFullName: '投资方全称',
          investorType: '投资方类别',
          totalInvestmentAmount: '总投资金额',
          investmentMethod: '投资方式',
          industryCategory: '产业类别',
          projectFillingUnit: '项目填报单位',
          projectUndertakers: '项目承接单位',
          projectMatchingStatus: '项目对接状态',
          projectPhase: '项目阶段',
          reviewStatus: '审核状态',
          filler: '填报人'
        };
        let table = [];
        table.push(headerRow);
        data.forEach((item) => {
          let row = {
            projectName: item.projectName,
            createTime: item.createTime,
            investorFullName: item.investorFullName,
            investorType: item.investorType,
            totalInvestmentAmount: item.totalInvestmentAmount,
            investmentMethod: item.investmentMethod,
            industryCategory: item.industryCategory,
            projectFillingUnit: item.projectFillingUnit,
            projectUndertakers: item.projectUndertakers,
            projectMatchingStatus: item.projectMatchingStatus,
            projectPhase: item.projectPhase,
            reviewStatus: item.reviewStatus,
            filler: item.filler
          };
          switch (item.investorType) {
            case 0:
              row.investorType = '央企'
              break;
            case 1:
              row.investorType = '国有企业'
              break;
            case 2:
              row.investorType = '民营企业'
              break;
            case 3:
              row.investorType = '外资企业'
              break;
            case 4:
              row.investorType = '集体制所有制企业'
              break;
            case 5:
              row.investorType = '联营企业'
              break;
            case 6:
              row.investorType = '三私企业'
              break;
            case 7:
              row.investorType = '私营企业'
              break;
            case 8:
              row.investorType = '中外合资企业'
              break;
            case 9:
              row.investorType = '其他'
              break;
            default:
              break;
          }
          switch (item.investmentMethod) {
            case 0:
              row.investmentMethod = '租楼'
              break;
            case 1:
              row.investmentMethod = '购房'
              break;
            case 2:
              row.investmentMethod = '租地'
              break;
            case 3:
              row.investmentMethod = '购地'
              break;
            case 4:
              row.investmentMethod = '租楼或购楼或购地'
              break;
            case 5:
              row.investmentMethod = '合作开发'
              break;
            case 6:
              row.investmentMethod = '股权投资'
              break;
            case 7:
              row.investmentMethod = '政府支持'
              break;
            case 8:
              row.investmentMethod = '其他'
              break;
            default:
              break;
          }
          switch (item.projectMatchingStatus) {
            case 0:
              row.projectMatchingStatus = '在谈项目'
              break;
            case 1:
              row.projectMatchingStatus = '项目审核'
              break;
            case 2:
              row.projectMatchingStatus = '落地项目'
              break;
            case 3:
              row.projectMatchingStatus = '中止项目'
              break;
            default:
              break;
          }
          switch (item.projectPhase) {
            case 0:
              row.projectPhase = '初次对接'
              break;
            case 1:
              row.projectPhase = '协助选址'
              break;
            case 2:
              row.projectPhase = '协议洽谈'
              break;
            default:
              break;
          }
          table.push(row)
        });
        exportExcel(table,filename,ws_name);
      } else {
        this.$message({
          message: '请至少选择一条数据!',
          type: 'warning'
        });
      }
    },

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