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

這里我們可以在axios的請求里添加,這樣返回的二進(jìn)制數(shù)據(jù)就會被讀取為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)擊下載訂單的按鈕后,瀏覽器就自動彈出下載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';//正確寫法,直接訪問你的請求路徑
//這種寫法會導(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項(xiàng)目在安卓低版本機(jī)顯示空白的原因分析(兩種)
本文給大家?guī)韛ue項(xiàng)目在安卓低版本機(jī)顯示空白的原因分析,根據(jù)各自需求給大家?guī)砹藘煞N原因分析,大家可以參考下2018-09-09
前端報(bào)錯(cuò)npm ERR! cb() never called!問題解決辦法
最近接手了一個(gè)前臺項(xiàng)目,執(zhí)行npm install的時(shí)候一直報(bào)錯(cuò),所以這里就給大家總結(jié)下,這篇文章主要給給大家介紹了關(guān)于前端報(bào)錯(cuò)npm?ERR! cb() never called!問題的解決辦法,需要的朋友可以參考下2024-05-05
使用Pinia Persistedstate插件實(shí)現(xiàn)狀態(tài)持久化的操作方法
Pinia 作為 Vue 的新一代狀態(tài)管理工具,以其輕量化和易用性受到開發(fā)者的喜愛,然而,Pinia 默認(rèn)使用內(nèi)存存儲狀態(tài),為了解決這個(gè)問題,我們可以借助 Pinia Persistedstate 插件來實(shí)現(xiàn)狀態(tài)的持久化存儲,本文將詳細(xì)介紹該插件的使用方法,需要的朋友可以參考下2024-11-11
解決removeEventListener 無法清除監(jiān)聽的問題
這篇文章主要介紹了解決removeEventListener 無法清除監(jiān)聽的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
v-for中動態(tài)校驗(yàn)el-form表單項(xiàng)的實(shí)踐
在項(xiàng)目開發(fā)中,我們經(jīng)常會遇到表單保存的功能,本文主要介紹了v-for中動態(tài)校驗(yàn)el-form表單項(xiàng)的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>2022-05-05

