有倆種方法可以用
第一種方法:
解析:直接解析並且下載后端的亂碼
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(()=>{ })