Springboot實(shí)現(xiàn)前后端分離excel下載
Springboot前后端分離excel下載
現(xiàn)在公司的技術(shù)棧是springboot作為后端,前端是vue, 現(xiàn)在要做excel的導(dǎo)出功能, 之前沒做過,寫一下記錄下.
springboot版本是2.0.6 poi 3.14 ,jdk1.8
類上面的注解是: @RestController
/** * 導(dǎo)出excel * */ @GetMapping("export") public void exportExcel() { XSSFWorkbook workbook = placeStatService.exportExcel(); // 設(shè)置生成的Excel的文件名,并以中文進(jìn)行編碼 String fileName = null; try { fileName = URLEncoder.encode("房間預(yù)約使用統(tǒng)計(jì)表" + ".xlsx", "utf-8").replaceAll("\\+", "%20"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } response.setCharacterEncoding("UTF-8"); response.setHeader("Content-type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName); // 響應(yīng)類型,編碼 response.setContentType("application/octet-stream;charset=UTF-8"); try { // 形成輸出流 OutputStream osOut = response.getOutputStream(); // 將指定的字節(jié)寫入此輸出流 workbook.write(osOut); // 刷新此輸出流并強(qiáng)制將所有緩沖的輸出字節(jié)被寫出 osOut.flush(); // 關(guān)閉流 osOut.close(); workbook.close(); } catch (IOException e) { e.printStackTrace(); } }
@Override public XSSFWorkbook exportExcel) { List<RoomOrderDetailModel> roomOrdersList = getRoomOrderList(); XSSFWorkbook data = ExcelUtil.setExcelData(roomOrdersList); return data; }
package com.util; import com.curefun.place.model.RoomOrderDetailModel; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; /** * @author excel 工具類. 導(dǎo)出功能 */ public class ExcelUtil { /** * 數(shù)據(jù)導(dǎo)出, 獲取一個(gè)excel對象 * * @param */ public static XSSFWorkbook setExcelData(List<RoomOrderDetailModel> orderDetailModels) { //創(chuàng)建一個(gè)book,對應(yīng)一個(gè)Excel文件 XSSFWorkbook workbook = new XSSFWorkbook(); //在book中添加一個(gè)sheet,對應(yīng)Excel文件中的sheet XSSFSheet sheet = workbook.createSheet("教室預(yù)約使用記錄"); //設(shè)置六列的寬度 sheet.setColumnWidth(0, 4000); sheet.setColumnWidth(1, 3000); sheet.setColumnWidth(2, 3800); sheet.setColumnWidth(3, 2800); sheet.setColumnWidth(4, 3200); sheet.setColumnWidth(5, 3600); sheet.setColumnWidth(6, 2850); //居中的樣式 XSSFCellStyle centerStyle = getCenterStyle(workbook); // 第三步,在sheet中添加表頭第0行 XSSFRow row0 = sheet.createRow(0); setFirstRow(centerStyle, row0); int rowNum = 1; for (RoomOrderDetailModel model : orderDetailModels) { XSSFRow row = sheet.createRow(rowNum); row.createCell(0).setCellValue(rowNum); rowNum++; row.createCell(1).setCellValue(model.getBuildingName()); row.createCell(2).setCellValue(model.getRoomNo()); row.createCell(3).setCellValue(model.getRoomName()); row.createCell(4).setCellValue(model.getEventType()); row.createCell(5).setCellValue(model.getEventName()); row.createCell(6).setCellValue(model.getUserRealName()); } return workbook; } /** * 獲取居中的樣式. * * @param workbook * @return */ private static XSSFCellStyle getCenterStyle(XSSFWorkbook workbook) { XSSFCellStyle cellStyle = workbook.createCellStyle(); //設(shè)置水平對齊的樣式為居中對齊; cellStyle.setAlignment(HorizontalAlignment.CENTER); //垂直居中 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); return cellStyle; } /** * 設(shè)置第一行的表頭 * * @param centerStyle * @param row */ private static void setFirstRow(XSSFCellStyle centerStyle, XSSFRow row) { XSSFCell cell0 = row.createCell(0); cell0.setCellValue("序號(hào)"); cell0.setCellStyle(centerStyle); XSSFCell cell1 = row.createCell(1); cell1.setCellValue("樓棟信息"); cell1.setCellStyle(centerStyle); XSSFCell cell2 = row.createCell(2); cell2.setCellValue("房號(hào)"); cell2.setCellStyle(centerStyle); XSSFCell cell3 = row.createCell(3); cell3.setCellValue("房間名稱"); cell3.setCellStyle(centerStyle); XSSFCell cell4 = row.createCell(4); cell4.setCellValue("活動(dòng)類型"); cell4.setCellStyle(centerStyle); XSSFCell cell5 = row.createCell(5); cell5.setCellValue("活動(dòng)名稱"); cell5.setCellStyle(centerStyle); XSSFCell cell6 = row.createCell(6); cell6.setCellValue("使用人"); cell6.setCellStyle(centerStyle); /** 其實(shí)完全使用這種方式, 會(huì)更加的簡單,便于修改 List<String> title = Stream.of("序號(hào)", "專業(yè)", "班級(jí)", "課程名稱", "課程內(nèi)容", "授課教師", "授課時(shí)長", "授課時(shí)間", "學(xué)分", "授課房間") .collect(Collectors.toList()); for (int i = 0; i < title.size(); i++) { XSSFCell cell = row.createCell(i); cell.setCellValue(title.get(i)); cell.setCellStyle(centerStyle); } */ } }
其實(shí)使用很簡單,就是excel的文件名需要進(jìn)行編碼,這個(gè)需要注意,其他沒啥的了.
前后端分離Excle下載亂碼問題
前端:vue+elementUI
后端:springCloud
前端請求方式 : ajax請求
this.$.ajax({ url :this.url + "/", type : 'post', data : formData, contentType: false, processData: false, xhrFields: {withCredentials: true, responseType:'arraybuffer'}, headers : {'Access-Control-Allow-Origin': '*', "Authorization": this.ajaxRequest.getToken()}, success: function (res, textStatus, request) { var downloadFile = document.createElement('a'); let blob = new Blob([res], {type : "application/vnd.ms-excel;charset=UTF-8"}); downloadFile.href = window.URL.createObjectURL(blob); console.log(request.getResponseHeader('Content-disposition')); downloadFile.download = decodeURI(request.getResponseHeader('Content-disposition').split('filename=')[1] ); downloadFile.click(); window.URL.revokeObjectURL(downloadFile.href); }, error : function (res) { console.log(res) } })
后端處理:導(dǎo)出文件處理
// 輸出Excel文件 OutputStream out = null; out = response.getOutputStream(); response.reset(); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF8")); response.setContentType("application/vnd.ms-excel; charset=utf-8"); // 輸出Excel內(nèi)容,生成Excel文件 wb.write(out);
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Json轉(zhuǎn)list二層解析轉(zhuǎn)換代碼實(shí)例
這篇文章主要介紹了Json轉(zhuǎn)list二層解析轉(zhuǎn)換代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12Mybatis動(dòng)態(tài)SQL實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于Mybatis動(dòng)態(tài)SQL的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11spring-boot通過@Scheduled配置定時(shí)任務(wù)及定時(shí)任務(wù)@Scheduled注解的方法
這篇文章主要介紹了spring-boot通過@Scheduled配置定時(shí)任務(wù),文中還給大家介紹了springboot 定時(shí)任務(wù)@Scheduled注解的方法,需要的朋友可以參考下2017-11-11Idea自定義方法注釋模板的教程詳解(去param括號(hào)、return全類名)
這篇文章主要介紹了Idea自定義方法注釋模板(去param括號(hào)、return全類名),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08SpringBoot項(xiàng)目打jar包與war包的詳細(xì)步驟
SpringBoot和我們之前學(xué)習(xí)的web應(yīng)用程序不一樣,其本質(zhì)上是一個(gè) Java應(yīng)用程序,那么又如何部署呢?這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目打jar包與war包的詳細(xì)步驟,需要的朋友可以參考下2023-02-02mybatis使用foreach語句實(shí)現(xiàn)IN查詢(三種)
這篇文章主要介紹了mybatis使用foreach語句實(shí)現(xiàn)IN查詢(三種),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12MySQL實(shí)現(xiàn)遠(yuǎn)程登錄的方法
Host 'Local' is not allowed to connect to this MySQL server 的解決方法,需要的朋友可以參考一下2013-03-03簡易版SpringBoot自定義模擬實(shí)現(xiàn)
SpringBoot作為目前最流行的框架之一,極大地提高了開發(fā)效率和降低了學(xué)習(xí)成本,使得開發(fā)人員能夠更專注于業(yè)務(wù)邏輯的實(shí)現(xiàn),而無需過多關(guān)注底層框架的配置和集成,本文模擬實(shí)現(xiàn)簡易版SpringBoot2024-01-01