Vue結(jié)合后臺(tái)導(dǎo)入導(dǎo)出Excel問題詳解
最近Vue項(xiàng)目中用到了導(dǎo)入導(dǎo)出功能,在網(wǎng)上搜索了一番之后,決定采用Blob方式,這也是大家推薦的一種的方式,特此做下記錄。
導(dǎo)出Excel功能
這里不談別人怎么實(shí)現(xiàn)的,我是從后臺(tái)生成了Excel流文件返回給前端的。
下面具體看一下后臺(tái)的代碼:
/** * 批量導(dǎo)出用戶 * @param condition * @param response */ @PostMapping("/exportUser") public void exportUser(@RequestBody UserQueryCondition condition,HttpServletResponse response){ XSSFWorkbook book = new XSSFWorkbook(); try { List<UserParam> list = indexService.exportUser(condition); if (list != null && list.size() > 0) { XSSFSheet sheet = book.createSheet("mySheent"); String[] vals = {"用戶ID", "郵箱賬號(hào)","昵稱","年齡","性別","狀態(tài)", "注冊(cè)時(shí)間"}; createExcel(sheet, 0, vals); for (int i = 0; i < list.size(); i++) { UserParam entity = list.get(i); String[] vals2 = new String[]{String.valueOf(entity.getId()), entity.getEmail(), entity.getName(), String.valueOf(entity.getAge()), entity.getSex() == 0 ? "女":"男",entity.getRemoved() == 0 ? "啟用":"禁用",entity.getRegisterDate()}; createExcel(sheet, i + 1, vals2); } book.write(generateResponseExcel("用戶列表",response)); } book.close(); }catch(Exception e){ e.printStackTrace(); } }
/** * @param excelName * 要生成的文件名字 * @return * @throws IOException */ private ServletOutputStream generateResponseExcel(String excelName, HttpServletResponse response) throws IOException { excelName = excelName == null || "".equals(excelName) ? "excel" : URLEncoder.encode(excelName, "UTF-8"); response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment; filename=" + excelName + ".xlsx"); return response.getOutputStream(); }
對(duì)于第一個(gè)函數(shù)exportUser
來說,主要是根據(jù)傳回來的條件查詢數(shù)據(jù)庫并生成相應(yīng)的Excel表格,之后book.write(generateResponseExcel(“用戶列表”,response)); 這行代碼調(diào)用第二個(gè)函數(shù)generateResponseExcel
來生成流文件以及處理返回的Response。
這里需要注意的地方就兩個(gè):
response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment; filename=" + excelName + ".xlsx");
第一行application/vnd.ms-excel說明將結(jié)果導(dǎo)出為Excel
第二行說明提供那個(gè)打開/保存對(duì)話框,將文件作為附件下載
上面就是后臺(tái)的全部代碼了,接下來看一下前端的代碼:
axios({ method: 'post', url: 'http://localhost:19090/exportUser', data: { email: this.email, userIdArray: this.userIdArray, startRegisterDate: this.registerStartTime, endRegisterDate: this.registerEndTime }, responseType: 'blob' }).then((res) => { console.log(res) const link = document.createElement('a') let blob = new Blob([res.data],{type: 'application/vnd.ms-excel'}); link.style.display = 'none' link.href = URL.createObjectURL(blob); let num = '' for(let i=0;i < 10;i++){ num += Math.ceil(Math.random() * 10) } link.setAttribute('download', '用戶_' + num + '.xlsx') document.body.appendChild(link) link.click() document.body.removeChild(link) }).catch(error => { this.$Notice.error({ title: '錯(cuò)誤', desc: '網(wǎng)絡(luò)連接錯(cuò)誤' }) console.log(error) })
這里為了方便做記錄,我是直接在頁面中使用axios發(fā)送了個(gè)post請(qǐng)求。
仔細(xì)看axios請(qǐng)求加了個(gè)responseType: 'blob'
配置,這是很重要的
可以看一下請(qǐng)求成功之后返回的數(shù)據(jù):
可以看到請(qǐng)求返回了一個(gè)Blob對(duì)象,你如果沒有正確的加上responseType: 'blob'這個(gè)參數(shù),返回的就不是個(gè)Blob對(duì)象,而是字符串了。
接下來就是將返回的Blob對(duì)象下載下來了:
const link = document.createElement('a') let blob = new Blob([res.data],{type: 'application/vnd.ms-excel'}); link.style.display = 'none' link.href = URL.createObjectURL(blob); let num = '' for(let i=0;i < 10;i++){ num += Math.ceil(Math.random() * 10) } link.setAttribute('download', '用戶_' + num + '.xlsx') document.body.appendChild(link) link.click() document.body.removeChild(link)
上面這段代碼重要的就一句:let blob = new Blob([res.data],{type: ‘a(chǎn)pplication/vnd.ms-excel'});
其余的看看就行了。
以上就是全部的Vue導(dǎo)出Excel前后端代碼了。
導(dǎo)入Excel功能
其實(shí)對(duì)于這個(gè)導(dǎo)入Excel沒有什么好說的,就和你沒采用前后分離時(shí)使用SpringMVC導(dǎo)入Excel表格一樣。Vue前端導(dǎo)入Excel代碼和Vue上傳圖片的代碼沒有區(qū)別,就是將Excel文件傳到后臺(tái),之后就是后臺(tái)處理文件的邏輯了,這個(gè)就不具體寫了,因?yàn)楹鸵郧皼]區(qū)別。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue實(shí)現(xiàn)導(dǎo)出excel表格功能
- vue中導(dǎo)出Excel表格的實(shí)現(xiàn)代碼
- 詳解如何在Vue項(xiàng)目中導(dǎo)出Excel
- Vue前端導(dǎo)出Excel文件的詳細(xì)實(shí)現(xiàn)方案
- Vue 前端導(dǎo)出后端返回的excel文件方式
- 使用vue導(dǎo)出excel遇到的坑及解決
- vue導(dǎo)出excel表格的新手詳細(xì)教程
- vue實(shí)現(xiàn)導(dǎo)出excel的多種方式總結(jié)
- Vue純前端如何實(shí)現(xiàn)導(dǎo)出簡(jiǎn)單Excel表格的功能
相關(guān)文章
Vue進(jìn)階之CodeMirror的應(yīng)用小結(jié)
CodeMirror支持在線編輯代碼,風(fēng)格包括js, java, php, c++等等100多種語言,下面這篇文章主要來和大家講講CodeMirror的應(yīng)用,感興趣的可以了解一下2023-06-06vue 修改 data 數(shù)據(jù)問題并實(shí)時(shí)顯示操作
這篇文章主要介紹了vue 修改 data 數(shù)據(jù)問題并實(shí)時(shí)顯示操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09VUE中Echarts的resize事件報(bào)錯(cuò)和移除windows的事件問題
這篇文章主要介紹了VUE中Echarts的resize事件報(bào)錯(cuò)和移除windows的事件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07源碼分析Vue.js的監(jiān)聽實(shí)現(xiàn)教程
這篇文章主要通過源碼分析介紹了Vue.js的監(jiān)聽實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-04-04vue項(xiàng)目中使用rem替換px的實(shí)現(xiàn)示例
移動(dòng)端頁面適配,rem和vw適配方案,本文主要介紹了vue項(xiàng)目中使用rem替換px的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07