导出excel表格,并且兼容IE浏览器

346 阅读1分钟
// 点击导出
    exportExcel(){
    //入参
      let params = {
        searchConditionList:this.form.tableData
      };
      excelExportApi(params)
      .then(res=>{
        // console.log(res);
        const blob = new Blob([res],{type:"application/vnd.ms-excel"});//处理文档流
          const fileName = '文件名.xlsx'; //导出后的文件名
          if (!!window.ActiveXObject || "ActiveXObject" in window || window.navigator.userAgent.indexOf('Edge/') > 0) {//判断是不是ie的浏览器
            window.navigator.msSaveOrOpenBlob(blob, fileName);
            console.log('ieeeeee')
          } else {
            const elink = document.createElement('a');
            elink.download = fileName;
            elink.style.display = 'none';
            elink.href = URL.createObjectURL(blob);
            document.body.appendChild(elink);
            elink.click();
            URL.revokeObjectURL(elink.href); // 释放URL 对象
            document.body.removeChild(elink);
            console.log('noieeeee')
          }
      }) 
      .catch((err) => {
          this.$message({ message: err || "导出失败", type: "warning", showClose: true });
        });
    },
    
   注意接口处要添加responseType: 'blob',
   export function excelExportApi (data) {
    return httpRequest({
    url:'',
    method: 'post',
    responseType: 'blob',
    data: data
  })
}
    

不兼容IE浏览器,谷歌浏览器可用

// if(res){
     //   let link = document.createElement('a')
     //   let blob = new Blob([res],{type:"application/vnd.ms-excel"})
     //   link.style.display = 'none'
     //   link.href = URL.createObjectURL(blob)
     //   link.setAttribute('download','出险率维护'+'.xlsx')
     //   document.body.appendChild(link)
     //   link.click()
     // }