Vue請(qǐng)求后端接口導(dǎo)出excel表格方式
vue請(qǐng)求后端接口導(dǎo)出excel
項(xiàng)目中遇到一個(gè)需求,用戶下載文件,會(huì)從后端那里請(qǐng)求接口獲得數(shù)據(jù)并下載導(dǎo)出excel表格
后端小哥給我返回的是二進(jìn)制數(shù)據(jù)流,需要前端自己去處理這些數(shù)據(jù)
如下圖,請(qǐng)求接口返回的數(shù)據(jù)都是亂碼
這里我們可以在axios的請(qǐng)求里添加,這樣返回的二進(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文件一欄了,要注意的是,我的電腦好像沒(méi)有xlsx格式的文件,所以在定義文件名那里改成了xls的格式
調(diào)用后端接口導(dǎo)出excel無(wú)效果,直接訪問(wèn)后端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"); ? ? ? ? //文件下載亂碼問(wèn)題 ? ? ? ? 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前端寫(xiě)法
//模板下載 getTemplate(){ const that = this; window.location='/test/test/getTemplate';//正確寫(xiě)法,直接訪問(wèn)你的請(qǐng)求路徑 //這種寫(xiě)法會(huì)導(dǎo)致后臺(tái)不報(bào)錯(cuò),但是前端無(wú)導(dǎo)出效果 /*window.axios.get('/test/test/getTemplate',{responseType: 'arraybuffer'}).then((res) => { }).catch((err) => { });*/ },
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue項(xiàng)目在安卓低版本機(jī)顯示空白的原因分析(兩種)
本文給大家?guī)?lái)vue項(xiàng)目在安卓低版本機(jī)顯示空白的原因分析,根據(jù)各自需求給大家?guī)?lái)了兩種原因分析,大家可以參考下2018-09-09前端報(bào)錯(cuò)npm ERR! cb() never called!問(wèn)題解決辦法
最近接手了一個(gè)前臺(tái)項(xiàng)目,執(zhí)行npm install的時(shí)候一直報(bào)錯(cuò),所以這里就給大家總結(jié)下,這篇文章主要給給大家介紹了關(guān)于前端報(bào)錯(cuò)npm?ERR! cb() never called!問(wèn)題的解決辦法,需要的朋友可以參考下2024-05-05vue3 中使用 reactive 的問(wèn)題小結(jié)
在 Vue 3 中,如果你使用 reactive 來(lái)定義一個(gè)響應(yīng)式對(duì)象,那么這個(gè)對(duì)象的屬性是不能被重新賦值的,因?yàn)?nbsp;reactive 會(huì)將對(duì)象的屬性轉(zhuǎn)換為 getter/setter,這樣 Vue 才能追蹤到屬性的變化,這篇文章主要介紹了vue3 中使用 reactive 的問(wèn)題,需要的朋友可以參考下2024-03-03Vue下拉框回顯并默認(rèn)選中隨機(jī)問(wèn)題
這篇文章主要介紹了Vue下拉框回顯并默認(rèn)選中隨機(jī)問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09使用Pinia Persistedstate插件實(shí)現(xiàn)狀態(tài)持久化的操作方法
Pinia 作為 Vue 的新一代狀態(tài)管理工具,以其輕量化和易用性受到開(kāi)發(fā)者的喜愛(ài),然而,Pinia 默認(rèn)使用內(nèi)存存儲(chǔ)狀態(tài),為了解決這個(gè)問(wèn)題,我們可以借助 Pinia Persistedstate 插件來(lái)實(shí)現(xiàn)狀態(tài)的持久化存儲(chǔ),本文將詳細(xì)介紹該插件的使用方法,需要的朋友可以參考下2024-11-11解決removeEventListener 無(wú)法清除監(jiān)聽(tīng)的問(wèn)題
這篇文章主要介紹了解決removeEventListener 無(wú)法清除監(jiān)聽(tīng)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10v-for中動(dòng)態(tài)校驗(yàn)el-form表單項(xiàng)的實(shí)踐
在項(xiàng)目開(kāi)發(fā)中,我們經(jīng)常會(huì)遇到表單保存的功能,本文主要介紹了v-for中動(dòng)態(tài)校驗(yàn)el-form表單項(xiàng)的實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧<BR>2022-05-05