java+vue下载xls文件到本地
生活随笔
收集整理的這篇文章主要介紹了
java+vue下载xls文件到本地
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.后端java springboot
背景:文件是以xls類型的文件。
第一步:添加依賴
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.14/version> </dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.14</version> </dependency>其他公共包自行添加
第二步:代碼
@PostMapping("/downLoad") @ResponseBody public void downLoad(@RequestParam(value = "path") String path,HttpServletResponse response) {String fileName = "fileName.xls"try{//注意這里是處理xls ,用的是HSSFWorkbook,path 是文件的存儲路徑HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(path));OutputStream fos = null;fos = response.getOutputStream();response.setCharacterEncoding("UTF-8");response.setContentType("application/vnd.ms-excel;charset=utf-8") response.setHeader("Content-Disposition",fileName);response.setHeader("filename",fileName);response.setHeader("Access-Control-Expose-Headers","filename");wb.write(fos);fos.close();}catch(Exception e){e.printStackTrace();}}2.后端vue+elementUI
//頁面 <el-button @click="downLoadFun(scope.row.path)"></el-button>//script方法downLoadFun(path){const url = hostUrl + path;axios({method:"post",url:url,params:{path:path},responseType:"blob",headers:{"X-Requested-With":"XMLHttpRequest" }}).then(res => {const link = document.createElement("a");let blob = new Blob([res.data],{type:"application/vnd.ms-excel"});link.style.display="none";const blobUrl = (link.href = URL.createObjectURL(blob));const filename = res.header.filename;link.setAttribute("download",filename);link.click();URL.revokeObjectURL(blobUrl);document.body.removeChild(link);}).catch(error => {this.$message.error("Download exception!");}) }總結
以上是生活随笔為你收集整理的java+vue下载xls文件到本地的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 广工数控课设:平面凸轮的数控加工程序的编
- 下一篇: 解决宏碁非凡S3 安装Win11时无法找