使用文件流 導出 excel表 (兼容IE)


大多數excel表格的導出,直接一個a標簽跳轉就行了

但是為了安全考慮,有些公司前端需要用post方式獲取后端返回的文件流,前端使用node將文件流轉譯后再導出

下面就是代碼

exportBtn(){
  //導出修改為讀取二進制文件流
      let parm = {
        report_type: this.sheetType,
        set_id: this.searchName,
        selectDate: selectDate,
        startTime: startTime,
        endTime: endTime,
      };
      api_tws.excelReport(qs.stringify(parm)).then((res) => {
        if (res == "apiError") {
          return;
        }
        const blob = new Blob([res]);
     if(window.navigator.msSaveBlob){
      //兼容ie
       window.navigator.msSaveBlob(blob,“報表.xls”)
     }else{
      //主流瀏覽器
       const elink = document.createElement("a");
          elink.download = `報表.xls`;
          elink.style.display = "none";
          elink.href = URL.createObjectURL(blob);
          document.body.appendChild(elink);
          elink.click();
          URL.revokeObjectURL(elink.href); // 釋放URL 對象
          document.body.removeChild(elink);
     }
        
      });  
}


//上面是方法,下面是定義的api請求


export async function excelReport(body) {
  const res = await axios({
    method: "post",
    responseType: "blob",
    url: serverUrl + '/excelReport/export?' + body
  });
  return res;
}

 

 

ps ,日期有一個Data.parse()方法 ie不會識別2021-11-11;只能識別2020/11/11

  所以需要轉換一下 let _startTime = Date.parse(("2021-11-11 00:00:00").replace(/-/g,"/"));


免責聲明!

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



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