后端创文件流前端浏览器进行下载Excel(springboot+Vue)

转载 https://blog.csdn.net/qq_34940644/article/details/99638156

    // 导出
    exportFun() {
      if (this.planListData.length === 0) {
        this.$message({
          message: "列表为空,无法导出",
          type: "error"
        });
        return;
      }
      this.isExport = false;
      this.exportList(this.searchData).then(res => {
          let blob = new Blob([res], {
            type:
              "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
          });
          let objectUrl = URL.createObjectURL(blob);
          let a = document.createElement("a");
          a.href = objectUrl;
          a.download = "补货计划报表";
          // a.click();
          //下面这个写法兼容火狐
          a.dispatchEvent(
            new MouseEvent("click", {
              bubbles: true,
              cancelable: true,
              view: window
            })
          );
          window.URL.revokeObjectURL(blob);
      });
    },
    /**
    * @description 补货计划--列表-导出
    */
    async exportList({ dispatch }, params) {
        const res = await Axios.post(API.REPLENISH_PLAN_EXPORT.URL, params, { responseType: "blob" }).then(response => response)
            .catch(error => Promise.reject(error))
        return res;
    },
async exportReport() {
      Axios.get("/sqdb/gwlog/v1/export", {
        params: {},
        responseType: "blob"
      }).then(res => {
        let blob = new Blob([res], {
          type:
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        });
        // console.log(blob);
        let objectUrl = URL.createObjectURL(blob);

        let a = document.createElement("a");
        a.href = objectUrl;
        a.download = "报表";
        // a.click();
        //下面这个写法兼容火狐
        a.dispatchEvent(
          new MouseEvent("click", {
            bubbles: true,
            cancelable: true,
            view: window
          })
        );
        window.URL.revokeObjectURL(blob);
      });
    },
原文地址:https://www.cnblogs.com/MR-cui/p/12454913.html