导出excel下载

2023-06-29  本文已影响0人  jesse28
    onConfirm(formName) {
      let loading = this.$loading({
        lock: true,
        customClass: "loading-bc",
      });
      caseInfoListExpApi({
        typeEnumCode: this.typeEnumCode,
        startingTime: this.form.startingTime,
        endTime: this.form.endTime,
        ...this.param,
      })
        .then((res) => {
          loading.close();
          console.log("确认", res);
          utils.downloadFile({
            fileName: "简易程序.xls",
            blob: res.data,
          });

          this.$emit("onClose");
        })
        .catch((err) => {
          loading.close();
        });
    },
image.png

封装的请求接口

export const caseInfoListExpApi = (data) => {
    return request({
        url: `http://172.18.1.91:8615/exp/exp/caseInfoList`,
        method: "post",
        data,
        responseType: 'blob',
    })
}

utils.js的封装

//下载文件
const downloadFile = (file) => {
    // let responseData = await downLoadFile({fileId: data.id, fileName: data.name, d: 0}, 'blob');
    // let blob = new Blob([file.blob])
    // console.log("blob => ", blob.text());
    let downloadElement = document.createElement('a')
    let href = window.URL.createObjectURL(file.blob); //创建下载的链接
    downloadElement.href = href;
    downloadElement.download = file.fileName; //下载后文件名
    document.body.appendChild(downloadElement);
    downloadElement.click(); //点击下载
    document.body.removeChild(downloadElement); //下载完成移除元素
    window.URL.revokeObjectURL(href); //释放blob对象
};
上一篇下一篇

猜你喜欢

热点阅读