后端創文件流前端瀏覽器進行下載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);
      });
    },

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM