vue 導出excel后端返回亂碼下載不了的解析問題


有倆種方法可以用

 

第一種方法:

解析:直接解析並且下載后端的亂碼

 this.download('后端給的導出excel的方法', {
       ...this.queryParams
 }, `job_${new Date().getTime()}.xlsx`)
 
 
 
第二種方法:
 
封裝一個api,里面寫你的方法,在請求的方法里加入:
responseType: 'blob',
 
例如:
//導出表格數據
export function exportTable() {
    return request({
        url: '/portal/course/export',
        method: 'post',
        responseType: 'blob',
    })
}

 

在 .vue 文件引入了這個方法之后,方法里寫上這些代碼。

例如:

exportTable().then(res=>{
        const fileName = '表格數據.xls'; //表格名字
            if ('download' in document.createElement('a')) { // 非IE下載
          const blob = new Blob([res], {type: 'application/ms-excel'}); // 解析后端返回的亂碼
          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);
        }
      }).catch(()=>{
        
      })

 

 

 

 

 


免責聲明!

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



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