@ApiOperation(value = "匯出多個檔案到一個Excel中的不同sheets", produces = "application/octet-stream")
@PostMapping("/exportMultipleExcel")
public void exportMultipleExcel(@RequestBody DataCountVO dataCountVO, HttpServletResponse response) throws Exception {
// 設定響應的ContentType和Header,告訴瀏覽器下載檔案
response.setContentType("application/vnd.ms-excel");
response.setHeader("Access-Control-Expose-Headers","Content-Disposition");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("資料統計表.xls", "UTF-8"));
// 建立Excel工作簿
Workbook workbook = new HSSFWorkbook();
// 獲取各個資料集
List<goodsStatusVO> goodsStatusList = snShipmentManageService.goodsStatus(dataCountVO);
List<goodsPartVO> goodsPartList = snShipmentManageService.goodsPart(dataCountVO);
List<shipCountVO> shipCountList = snShipmentManageService.shipCount(dataCountVO);
List<loadingStatusVO> loadingStatusList = snShipmentManageService.loadingStatus(dataCountVO);
// 建立引數物件(用來設定excel得sheet得內容等資訊)
ExportParams goodsExport = new ExportParams();
// 設定sheet得名稱
goodsExport.setSheetName("品種銷售情況");
// 建立sheet1使用得map
Map<String, Object> goodsExportMap = new HashMap<>();
// title的引數為ExportParams型別,目前僅僅在ExportParams中設定了sheetName
goodsExportMap.put("title", goodsExport);
// 模版匯出對應得實體型別
goodsExportMap.put("entity", goodsStatusVO.class);
// sheet中要填充得資料
goodsExportMap.put("data", goodsStatusList);
ExportParams partExport = new ExportParams();
partExport.setSheetName("品種銷量佔比");
// 建立sheet2使用得map
Map<String, Object> partExportMap = new HashMap<>();
partExportMap.put("title", partExport);
partExportMap.put("entity", goodsPartVO.class);
partExportMap.put("data", goodsPartList);
ExportParams shipExport = new ExportParams();
shipExport.setSheetName("客戶發運量統計");
// 建立sheet3使用得map
Map<String, Object> shipExportMap = new HashMap<>();
shipExportMap.put("title", shipExport);
shipExportMap.put("entity", shipCountVO.class);
shipExportMap.put("data", shipCountList);
ExportParams loadingExport = new ExportParams();
loadingExport.setSheetName("當日裝車情況");
// 建立sheet4使用得map
Map<String, Object> loadingExportMap = new HashMap<>();
loadingExportMap.put("title", loadingExport);
loadingExportMap.put("entity", loadingStatusVO.class);
loadingExportMap.put("data", loadingStatusList);
// 將sheet1、sheet2、sheet3、sheet4使用得map進行包裝
List<Map<String, Object>> sheetsList = new ArrayList<>();
sheetsList.add(goodsExportMap);
sheetsList.add(partExportMap);
sheetsList.add(shipExportMap);
sheetsList.add(loadingExportMap);
workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
// 將工作簿寫入到響應輸出流中
try (ServletOutputStream out = response.getOutputStream()) {
workbook.write(out);
}
// 關閉工作簿
workbook.close();
}
還有就是實體類:::::::VO
序號是一個虛擬展示序號 匯出檔案的時候有幾個資料就有幾個序號 在查詢的時候可以進行設定
記得加判空哦!!!!!!!!