vue導(dǎo)出excel文件流中文亂碼問(wèn)題及解決
導(dǎo)出excel文件流中文亂碼

解決此方法很多網(wǎng)上的差不多都可以。一下提供簡(jiǎn)單的方法
loads(){
let data={
userWord:this.dataList.userWord,
examId:this.$route.query.id,
exportType:this.active,
}
api.exportUserResult(data).then((res) => {
const blob = new Blob([res.data]);
const fileName = '考試成績(jī).xls';
const linkNode = document.createElement('a');
linkNode.download = fileName; //a標(biāo)簽的download屬性規(guī)定下載文件的名稱
linkNode.style.display = 'none';
linkNode.href = URL.createObjectURL(blob); //生成一個(gè)Blob URL
document.body.appendChild(linkNode);
linkNode.click(); //模擬在按鈕上的一次鼠標(biāo)單擊
URL.revokeObjectURL(linkNode.href); // 釋放URL 對(duì)象
document.body.removeChild(linkNode);
});
},注意:

填寫(xiě)
另住攔截器,因?yàn)榕袛鄏esult,沒(méi)在正確里返回,所以我直接返回

導(dǎo)出excel亂碼(錕斤拷唷?錕?;錕斤拷)

我這個(gè)是 post請(qǐng)求亂碼了 ,如果是get,就直接window.open(url,'_blank')就可以了
1.“錕斤拷唷?錕?;錕斤拷”這種亂碼信息導(dǎo)致的原因是:整個(gè)數(shù)據(jù)流的字符集 GBK=>UTF-8=>GBK導(dǎo)致的。
2. 前端代碼:
axios({
method: "post",
url: url,
data: params,
headers: {
// ... 接口需要的請(qǐng)求頭
},
responseType: "blob"
}).then(response => {
const blob = new Blob([res.data],{type: 'application/vnd.ms-excel'});
const fileName = res.headers["content-disposition"].split("=")[1]; //接口響應(yīng)頭定義的文件名
downloadFile(blob, fileName);
});//import { Message } from "element-ui";
/**
* 文件下載, 對(duì)于下載鏈接可直接用 window.open(url, "_blank");
* @param {*} data 二進(jìn)制數(shù)據(jù)或base64編碼 Blob、String
* @param {*} fileName 下載的文件命名,可帶擴(kuò)展名,跨域下無(wú)效
*/
export function downloadFile(data, fileName) {
let url = "";
let isBlob = false;
const errMsg = "下載出錯(cuò),文件數(shù)據(jù)無(wú)法識(shí)別!";
if (data instanceof Blob) {
isBlob = true;
url = window.URL.createObjectURL(data);
} else if (typeof data == "string") {
// base64編碼
url = data;
} else {
Message.error(errMsg);
return;
}
if ("download" in document.createElement("a")) {
// 非IE下載
const tmpLink = document.createElement("a");
tmpLink.download = fileName || "";
tmpLink.style.display = "none";
tmpLink.href = url;
document.body.appendChild(tmpLink);
tmpLink.click();
window.URL.revokeObjectURL(tmpLink.href); // 釋放URL 對(duì)象
document.body.removeChild(tmpLink);
} else {
// IE10+下載
if (isBlob) {
window.navigator.msSaveBlob(data, fileName);
} else {
//Message.error(errMsg);
console.log(errMsg);
return;
}
}
}3. 感覺(jué)完美 但是結(jié)果下載下來(lái)的如一開(kāi)始截圖的亂碼,其實(shí)代碼沒(méi)有問(wèn)題,問(wèn)題在于前端項(xiàng)目啟用了mock.js,把所有 import 或 require @/mock 的地方注釋調(diào)就可以了
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue element實(shí)現(xiàn)將表格單行數(shù)據(jù)導(dǎo)出為excel格式流程詳解
這篇文章主要介紹了vue element實(shí)現(xiàn)將表格單行數(shù)據(jù)導(dǎo)出為excel格式流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-12-12
Avue實(shí)現(xiàn)動(dòng)態(tài)查詢與數(shù)據(jù)展示的示例代碼
Avue是一個(gè)基于Vue.js的前端框架,它是由阿里云開(kāi)發(fā)的一款企業(yè)級(jí)UI組件庫(kù),旨在提供一套全面、易用且高性能的界面解決方案本文介紹了Avue實(shí)現(xiàn)動(dòng)態(tài)查詢與數(shù)據(jù)展示的示例,需要的朋友可以參考下2024-08-08
vue中radio單選框如何實(shí)現(xiàn)取消選中狀態(tài)問(wèn)題
這篇文章主要介紹了vue中radio單選框如何實(shí)現(xiàn)取消選中狀態(tài)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
關(guān)于element?ui中的el-scrollbar橫向滾動(dòng)問(wèn)題
這篇文章主要介紹了關(guān)于element?ui中的el-scrollbar橫向滾動(dòng)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
關(guān)于vue使用ant design vue,打包后a-date-picker控件無(wú)法選擇日期的問(wèn)題
這篇文章主要介紹了關(guān)于vite .env.test環(huán)境使用ant design vue,打包后a-date-picker控件無(wú)法選擇日期的問(wèn)題,本文針對(duì)這個(gè)問(wèn)題提供了解決方法,需要的朋友可以參考下2023-04-04
vue Element-ui input 遠(yuǎn)程搜索與修改建議顯示模版的示例代碼
本文分為html,js和css代碼給大家詳細(xì)介紹了vue Element-ui input 遠(yuǎn)程搜索與修改建議顯示模版功能,感興趣的朋友一起看看吧2017-10-10

