导出excel下载

    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对象
};
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容