使用文件流 导出 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,"/"));

posted @ 2020-12-02 15:17  我是一名好程序员  阅读(742)  评论(0编辑  收藏  举报