XLSX实现分页导出json数据

1.使用XLSX前先安装,在vue文件引用


<div class="dialog-footer" style="text-align: center;padding: 4px 0;">
    <el-button @click="excelData" type="primary">导出</el-button>
</div>


import * as XLSX from 'xlsx'

 methods: {
    // 导出excel
    excelData() {
      try {
        const nowTime = formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss')
        const filename = `智能测试数据${nowTime}.xlsx`

        const workbook = XLSX.utils.book_new(); // 创建工作簿

        for (let i = 0; i < 4; i++) {
          const data = {
            sheetName: 'CH' + (i + 1),// 分页名称
            // 导出excel的表头
            tHeader: ['序号', '工步名称', '电流(A)', '电压(V)', '协议', '模式', '测试电流(A)', '测试电压(V)', '测试范围(V)', '测试结果(V)', '测试时间'],
            // filterVal 与中文表头对应对象属性名称
            filterVal: ['num', 'name', 'writeElectricity', 'writeVoltage', 'protocol', 'mode', 'electricity', 'voltage', 'scope', 'results', 'testDate'],
            list: this.testData[i].printData
          };

          const formattedData = this.formatJson(data.filterVal, data.list);
          const worksheetData = [data.tHeader, ...formattedData]; // 添加标题行和数据行
          const worksheet = XLSX.utils.aoa_to_sheet(worksheetData); // 创建工作表
          XLSX.utils.book_append_sheet(workbook, worksheet, data.sheetName) // 将工作表添加到工作簿
        }

        XLSX.writeFile(workbook, filename); // 导出Excel文件
      } catch (error) {
        console.log(error)
      }
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    }
}

2.导出数据为

chList: [
          {
              label: "CH1",
              name: "first",
              tableDatas: [
                  {
                      name: "PD5V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "5.0",
                      decoyVoltage: "5.0",
                      electricity: "1.0",
                      upLimit: "5.5",
                      lowerLimit: "4.5",
                      isStart: false,
                      testval: "0",
                      scope: "4.5~5.5",
                      time: "0",
                      timeout: "0"
                  },
                  {
                      name: "PD9V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "9.0",
                      decoyVoltage: "9.0",
                      electricity: "1.0",
                      upLimit: "9.5",
                      lowerLimit: "8.5",
                      isStart: false,
                      testval: "0",
                      scope: "8.5~9.5",
                      time: "0",
                      timeout: "0"
                  },
                  {
                      name: "PD12V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "12.0",
                      decoyVoltage: "12.0",
                      electricity: "1.0",
                      upLimit: "12.5",
                      lowerLimit: "11.5",
                      isStart: false,
                      testval: "0",
                      scope: "11.5~12.5",
                      time: "0",
                      timeout: "0"
                  },
                  {
                      name: "PD15V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "15.0",
                      decoyVoltage: "15.0",
                      electricity: "1.0",
                      upLimit: "15.5",
                      lowerLimit: "14.5",
                      isStart: false,
                      testval: "0",
                      scope: "14.5~15.5",
                      time: "0",
                      timeout: "0"
                  },
                  {
                      name: "PD20V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "20.0",
                      decoyVoltage: "20.0",
                      electricity: "1.0",
                      upLimit: "20.5",
                      lowerLimit: "19.5",
                      isStart: false,
                      testval: "0",
                      scope: "19.5~20.5",
                      time: "0",
                      timeout: "0"
                  }
              ]
          },
          {
              label: "CH2",
              name: "second",
              tableDatas: [
                  {
                      name: "PD5V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "5.0",
                      decoyVoltage: "5.0",
                      electricity: "1.0",
                      upLimit: "5.5",
                      lowerLimit: "4.5",
                      isStart: false,
                      testval: "0",
                      scope: "4.5~5.5",
                      time: "0",
                      timeout: "0"
                  },
                  {
                      name: "PD9V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "9.0",
                      decoyVoltage: "9.0",
                      electricity: "1.0",
                      upLimit: "9.5",
                      lowerLimit: "8.5",
                      isStart: false,
                      testval: "0",
                      scope: "8.5~9.5",
                      time: "0",
                      timeout: "0"
                  },
                  {
                      name: "PD12V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "12.0",
                      decoyVoltage: "12.0",
                      electricity: "1.0",
                      upLimit: "12.5",
                      lowerLimit: "11.5",
                      isStart: false,
                      testval: "0",
                      scope: "11.5~12.5",
                      time: "0",
                      timeout: "0"
                  },
                  {
                      name: "PD15V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "15.0",
                      decoyVoltage: "15.0",
                      electricity: "1.0",
                      upLimit: "15.5",
                      lowerLimit: "14.5",
                      isStart: false,
                      testval: "0",
                      scope: "14.5~15.5",
                      time: "0",
                      timeout: "0"
                  },
                  {
                      name: "PD20V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "20.0",
                      decoyVoltage: "20.0",
                      electricity: "1.0",
                      upLimit: "20.5",
                      lowerLimit: "19.5",
                      isStart: false,
                      testval: "0",
                      scope: "19.5~20.5",
                      time: "0",
                      timeout: "0"
                  }
              ]
          },
          {
              label: "CH3",
              name: "third",
              tableDatas: [
                  {
                      name: "PD5V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "5.0",
                      decoyVoltage: "5.0",
                      electricity: "1.0",
                      upLimit: "5.5",
                      lowerLimit: "4.5",
                      isStart: false,
                      testval: "0",
                      scope: "4.5~5.5",
                      time: "0",
                      timeout: "0"
                  },
                  {
                      name: "PD9V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "9.0",
                      decoyVoltage: "9.0",
                      electricity: "1.0",
                      upLimit: "9.5",
                      lowerLimit: "8.5",
                      isStart: false,
                      testval: "0",
                      scope: "8.5~9.5",
                      time: "0",
                      timeout: "0"
                  },
                  {
                      name: "PD12V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "12.0",
                      decoyVoltage: "12.0",
                      electricity: "1.0",
                      upLimit: "12.5",
                      lowerLimit: "11.5",
                      isStart: false,
                      testval: "0",
                      scope: "11.5~12.5",
                      time: "0",
                      timeout: "0"
                  },
                  {
                      name: "PD15V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "15.0",
                      decoyVoltage: "15.0",
                      electricity: "1.0",
                      upLimit: "15.5",
                      lowerLimit: "14.5",
                      isStart: false,
                      testval: "0",
                      scope: "14.5~15.5",
                      time: "0",
                      timeout: "0"
                  },
                  {
                      name: "PD20V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "20.0",
                      decoyVoltage: "20.0",
                      electricity: "1.0",
                      upLimit: "20.5",
                      lowerLimit: "19.5",
                      isStart: false,
                      testval: "0",
                      scope: "19.5~20.5",
                      time: "0",
                      timeout: "0"
                  }
              ]
          },
          {
              label: "CH4",
              name: "fourth",
              tableDatas: [
                  {
                      name: "PD5V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "5.0",
                      decoyVoltage: "5.0",
                      electricity: "1.0",
                      upLimit: "5.5",
                      lowerLimit: "4.5",
                      isStart: false,
                      testval: "0",
                      scope: "4.5~5.5",
                      time: "0",
                      timeout: "0"
                  },
                  {
                      name: "PD9V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "9.0",
                      decoyVoltage: "9.0",
                      electricity: "1.0",
                      upLimit: "9.5",
                      lowerLimit: "8.5",
                      isStart: false,
                      testval: "0",
                      scope: "8.5~9.5",
                      time: "0",
                      timeout: "0"
                  },
                  {
                      name: "PD12V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "12.0",
                      decoyVoltage: "12.0",
                      electricity: "1.0",
                      upLimit: "12.5",
                      lowerLimit: "11.5",
                      isStart: false,
                      testval: "0",
                      scope: "11.5~12.5",
                      time: "0",
                      timeout: "0"
                  },
                  {
                      name: "PD15V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "15.0",
                      decoyVoltage: "15.0",
                      electricity: "1.0",
                      upLimit: "15.5",
                      lowerLimit: "14.5",
                      isStart: false,
                      testval: "0",
                      scope: "14.5~15.5",
                      time: "0",
                      timeout: "0"
                  },
                  {
                      name: "PD20V1A",
                      protocol: 3,
                      currentPro: 3,
                      tabPosition: "CC",
                      voltage: "20.0",
                      decoyVoltage: "20.0",
                      electricity: "1.0",
                      upLimit: "20.5",
                      lowerLimit: "19.5",
                      isStart: false,
                      testval: "0",
                      scope: "19.5~20.5",
                      time: "0",
                      timeout: "0"
                  }
              ]
          }
        ],


3.导出后的表格为

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值