您好,登錄后才能下訂單哦!
本篇內容介紹了“Springboot怎么實現前后端分離excel下載”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
現在公司的技術棧是springboot作為后端,前端是vue, 現在要做excel的導出功能, 之前沒做過,寫一下記錄下.
springboot版本是2.0.6 poi 3.14 ,jdk1.8
類上面的注解是: @RestController
/** * 導出excel * */ @GetMapping("export") public void exportExcel() { XSSFWorkbook workbook = placeStatService.exportExcel(); // 設置生成的Excel的文件名,并以中文進行編碼 String fileName = null; try { fileName = URLEncoder.encode("房間預約使用統計表" + ".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); // 響應類型,編碼 response.setContentType("application/octet-stream;charset=UTF-8"); try { // 形成輸出流 OutputStream osOut = response.getOutputStream(); // 將指定的字節寫入此輸出流 workbook.write(osOut); // 刷新此輸出流并強制將所有緩沖的輸出字節被寫出 osOut.flush(); // 關閉流 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 工具類. 導出功能 */ public class ExcelUtil { /** * 數據導出, 獲取一個excel對象 * * @param */ public static XSSFWorkbook setExcelData(List<RoomOrderDetailModel> orderDetailModels) { //創建一個book,對應一個Excel文件 XSSFWorkbook workbook = new XSSFWorkbook(); //在book中添加一個sheet,對應Excel文件中的sheet XSSFSheet sheet = workbook.createSheet("教室預約使用記錄"); //設置六列的寬度 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(); //設置水平對齊的樣式為居中對齊; cellStyle.setAlignment(HorizontalAlignment.CENTER); //垂直居中 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); return cellStyle; } /** * 設置第一行的表頭 * * @param centerStyle * @param row */ private static void setFirstRow(XSSFCellStyle centerStyle, XSSFRow row) { XSSFCell cell0 = row.createCell(0); cell0.setCellValue("序號"); cell0.setCellStyle(centerStyle); XSSFCell cell1 = row.createCell(1); cell1.setCellValue("樓棟信息"); cell1.setCellStyle(centerStyle); XSSFCell cell2 = row.createCell(2); cell2.setCellValue("房號"); cell2.setCellStyle(centerStyle); XSSFCell cell3 = row.createCell(3); cell3.setCellValue("房間名稱"); cell3.setCellStyle(centerStyle); XSSFCell cell4 = row.createCell(4); cell4.setCellValue("活動類型"); cell4.setCellStyle(centerStyle); XSSFCell cell5 = row.createCell(5); cell5.setCellValue("活動名稱"); cell5.setCellStyle(centerStyle); XSSFCell cell6 = row.createCell(6); cell6.setCellValue("使用人"); cell6.setCellStyle(centerStyle); /** 其實完全使用這種方式, 會更加的簡單,便于修改 List<String> title = Stream.of("序號", "專業", "班級", "課程名稱", "課程內容", "授課教師", "授課時長", "授課時間", "學分", "授課房間") .collect(Collectors.toList()); for (int i = 0; i < title.size(); i++) { XSSFCell cell = row.createCell(i); cell.setCellValue(title.get(i)); cell.setCellStyle(centerStyle); } */ } }
其實使用很簡單,就是excel的文件名需要進行編碼,這個需要注意,其他沒啥的了.
前端:vue+elementUI
后端:springCloud
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) } })
后端處理:導出文件處理
// 輸出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內容,生成Excel文件 wb.write(out);
“Springboot怎么實現前后端分離excel下載”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。