vue导出接口

async exportExcel() {
    try {
      const list: any = [];
      this.multipleSelection.map((v: any) => {
        list.push(v.id);
      });
      list.sort((a: any, b: any) => a - b);
      let allApi: any = null;
      if (this.showTabs == 'one') allApi = hisTruck;
      if (this.showTabs == 'two') allApi = internalExport;
      if (this.showTabs == 'three') allApi = procurementExport;
      if (this.showTabs == 'four') allApi = downExport;
      let startTime = '';
      let endTime = '';
      if (this.showTabs == 'one') {
        if (this.infoForm.tabPosition == 'day') {
          startTime = this.infoForm.day[0];
          endTime = this.infoForm.day[1];
        } else if (this.infoForm.tabPosition == 'week') {
          startTime = this.getMonDayAndSunDay(1, this.infoForm.week) + ' 00:00:00';
          endTime = this.getMonDayAndSunDay(7, this.infoForm.week) + ' 23:59:59';
        } else if (this.infoForm.tabPosition == 'month') {
          const date = new Date(this.infoForm.month);
          startTime = yearMonth(this.infoForm.month) + ' 00:00:00';
          endTime =
            dayStampToTime(new Date(date.getFullYear(), date.getMonth() + 1, 0)) + ' 23:59:59'; //本月最后一天;
        }
      } else {
        startTime = this.infoForm.time[0];
        endTime = this.infoForm.time[1];
      }

      const res = await allApi({
        startTime: startTime,
        endTime: endTime,
        idList: list,
        materialName: this.infoForm.materialName,
        statusType: this.infoForm.tabPosition,
        uniqueKey: this.infoForm.only,
      }).catch((error: any) => {
        this.$message.error(error);
      });
      const fileName = decodeURIComponent(res.headers['content-disposition'].split('=')[1]).split(
        "utf-8''"
      )[1];
      const filenameExtension: any = 'xlsx';
      //封装读取文件并下载
      this.readFileDownload(res, fileName, filenameExtension);
    } catch (error) {
      this.$message.error('导出失败');
    }
  }
  /** 导出模板 */
  readFileDownload(data: any, fileName: any, filenameExtension: any) {
    const res = data.data;
    //res.type 后端返回的类型
    let blob: any = '';
    //根据excel类型解析对应文件
    if (filenameExtension === 'xlsx') {
      blob = new Blob([res], {
        type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8',
      });
    } else if (filenameExtension === 'xls') {
      blob = new Blob([res], {
        type: 'application/vnd.ms-excel',
      });
    }
    if (window.navigator && (window.navigator as any).msSaveOrOpenBlob) {
      (window.navigator as any).msSaveOrOpenBlob(blob, fileName);
    } else {
      const elink = document.createElement('a'); // 通过elink得到一个<a>标签(有点类似于ref)
      elink.download = fileName; // 规定被下载的超链接目标
      elink.style.display = 'none'; // 设置样式(照抄就行了)
      elink.href = URL.createObjectURL(blob); // 为<a>标签绑定href属性
      document.body.appendChild(elink); // 创建一个<a>标签
      elink.click();
      URL.revokeObjectURL(elink.href); // 释放URL对象
      document.body.removeChild(elink);
    }

    this.$message.success('导出成功');
    // if (
    //   res.type === 'application/msexcel' ||
    //   res.type === 'application/x-download' ||
    //   res.type === 'text/xml' ||
    //   res.type === 'application/vnd.ms-excel'
    // ) {
    // } else {
    //   this.$message.warning('导出失败!');
    // }
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天气晚来秋~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值