Vue請求后端接口導(dǎo)出excel表格方式
vue請求后端接口導(dǎo)出excel
項(xiàng)目中遇到一個(gè)需求,用戶下載文件,會(huì)從后端那里請求接口獲得數(shù)據(jù)并下載導(dǎo)出excel表格
后端小哥給我返回的是二進(jìn)制數(shù)據(jù)流,需要前端自己去處理這些數(shù)據(jù)
如下圖,請求接口返回的數(shù)據(jù)都是亂碼

這里我們可以在axios的請求里添加,這樣返回的二進(jìn)制數(shù)據(jù)就會(huì)被讀取為Blob的數(shù)據(jù),
responseType: ‘blob’
fetchGet1(url, params) {
return axios({
url,
method: 'get',
params,
header: {
headers: { 'Content-Type': 'application/x-download' }
},
responseType: 'blob' //指明返回格式
})
}
//下載接口
export var downLoadOrder = (orderId) => ajax.fetchGet1(`/api/order/excel/${orderId}`)

當(dāng)我點(diǎn)擊下載訂單的按鈕后,瀏覽器就自動(dòng)彈出下載excel文件一欄了,要注意的是,我的電腦好像沒有xlsx格式的文件,所以在定義文件名那里改成了xls的格式

調(diào)用后端接口導(dǎo)出excel無效果,直接訪問后端url可以
controller層代碼
@ApiOperation(value="導(dǎo)出模板")
? ? @RequestMapping(value="/getTemplate" , method= RequestMethod.GET)
? ? @ResponseBody
? ? public void getTemplate(HttpServletRequest req,HttpServletResponse res) throws IOException {
? ? ?? ?standingBookService.getTemplate(req, res);
? ? }serviceImpl代碼
?? ?public void getTemplate(HttpServletRequest req, HttpServletResponse res) throws IOException {
?? ??? ?String templateName = "standingBookTemplate";
? ? ? ? String exportName = "template";
? ? ? ??
? ? ? ? ExcelUtil.downloadExcelTemplate(req, res, templateName, exportName);
?? ?}導(dǎo)出模板路徑

工具箱代碼
? ? /**
? ? ?* 下載批量導(dǎo)入模板
? ? ?* @param HttpServletRequest
? ? ?* @param HttpServletResponse
? ? ?* @param templateName execl模板名稱
? ? ?* @param exportName 導(dǎo)出表單名稱
? ? ?* @throws IOException
? ? ?*/
? ? public static void downloadExcelTemplate(HttpServletRequest req,HttpServletResponse res,String templateName,
? ? ? ? String exportName) throws IOException{
? ? ? ??
? ? ? ? String fullFileName = req.getServletContext().getRealPath("/doc/import/excelTemplate");
? ? ? ? fullFileName += (File.separator + templateName + ".xls");
? ? ? ??
? ? ? ? String export = "";
? ? ? ? if(DataValidUtil.isEmpty(exportName)){
? ? ? ? ? ? export = templateName;
? ? ? ? }else{
? ? ? ? ? ? export = exportName;
? ? ? ? }
? ? ? ??
? ? ? ? String userAgent = req.getHeader("USER-AGENT");
? ? ? ? //文件下載亂碼問題
? ? ? ? if (StringUtils.contains(userAgent.toUpperCase(),"MSIE")||StringUtils.contains(userAgent,"Trident")) { ?
? ? ? ? ? ? export = URLEncoder.encode(export, "UTF-8"); ?
? ? ? ? } else { ?
? ? ? ? ? ? export = new String(export.getBytes("UTF-8"), "ISO8859-1"); ?
? ? ? ? }?
? ? ? ??
? ? ? ? //設(shè)置Content-Disposition
? ? ? ? res.setHeader("Content-disposition","attachment; filename="+export+".xls");
? ? ? ??
? ? ? ? //設(shè)置文件MIME類型?
? ? ? ? //res.setContentType("application/vnd.ms-excel");
? ? ? ? //前端框架自定義類型
? ? ? ? res.setContentType("application/export.file");
? ? ? ??
? ? ? ? ? ? OutputStream out = res.getOutputStream();
? ? ? ? ? ? FileInputStream in = new FileInputStream(fullFileName);
? ? ? ? ? ? res.setCharacterEncoding("UTF-8");
? ? ? ? ? ??
? ? ? ? ? ? byte[] b = new byte[1024];
? ? ? ? ? ? int n = -1;
? ? ? ? ? ??
? ? ? ? ? ? while((n=in.read(b))!=-1){
? ? ? ? ? ? ? ? out.write(b, 0, n);
? ? ? ? ? ? }
? ? ? ? ? ??
? ? ? ? ? ? in.close();
? ? ? ? ? ? out.close();
? ? }vue前端寫法
//模板下載
getTemplate(){
const that = this;
window.location='/test/test/getTemplate';//正確寫法,直接訪問你的請求路徑
//這種寫法會(huì)導(dǎo)致后臺不報(bào)錯(cuò),但是前端無導(dǎo)出效果
/*window.axios.get('/test/test/getTemplate',{responseType: 'arraybuffer'}).then((res) => {
}).catch((err) => {
});*/
},
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)監(jiān)控視頻直播的示例代碼
本文主要介紹了vue實(shí)現(xiàn)監(jiān)控視頻直播的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Vue中video標(biāo)簽如何實(shí)現(xiàn)不靜音自動(dòng)播放
最近在做大屏展示需要在一開始播放引導(dǎo)視頻,產(chǎn)生自動(dòng)播放需求,下面這篇文章主要給大家介紹了關(guān)于Vue中video標(biāo)簽如何實(shí)現(xiàn)不靜音自動(dòng)播放的相關(guān)資料,需要的朋友可以參考下2023-01-01
vue中v-model動(dòng)態(tài)生成的實(shí)例詳解
這篇文章主要介紹了vue中v-model動(dòng)態(tài)生成的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10
原生JS?Intersection?Observer?API實(shí)現(xiàn)懶加載
這篇文章主要為大家介紹了原生JS?Intersection?Observer?API實(shí)現(xiàn)懶加載示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Vue.js設(shè)計(jì)與實(shí)現(xiàn)無限遞歸學(xué)習(xí)總結(jié)
這篇文章主要為大家介紹了Vue.js設(shè)計(jì)與實(shí)現(xiàn)無限遞歸學(xué)習(xí)總結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

