Spring POI批量匯出Excel,壓縮zip,返回下載zip路徑
阿新 • • 發佈:2019-01-14
瞭解springPOI匯出excel
話不多,放碼過來
application.yml配置檔案
zipfilepath:
#path: /export/data/www/zip
path: E:\2345Downloads
http: http://devh5.warehouseadmin.chujiayoupin.com/zip
配置zipFilePath
package com.warehouse.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; @Component @Primary @ConfigurationProperties(prefix = "zipfilepath")//貌似zipfilepath這裡只能小寫 @Data public class zipFilePath { private String path; private String http; }
主要程式碼:
@Autowired private zipFilePath zipFilPath; /** * 批量匯出分揀單 * @param request * @param result * @return */ @CrossOrigin(origins = "*", maxAge = 3600)//跨域標籤 @ApiOperation(nickname = "batchExportPickMenu", value = "批量匯出分揀單", httpMethod = "POST") @PostMapping(value = "/batchExportPickMenu", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public ResponseData batchExportPickMenu(@Valid @RequestBody UpdatePickMemuRequest request,HttpServletResponse response, BindingResult result)throws IOException { String url= null; try{ //先建立生成excel目錄 File ff = new File(zipFilPath.getPath()); if (!ff.exists()) { ff.mkdirs(); } ArrayList<File> files = new ArrayList<>(); List<Long> ids = request.getIds(); for (Long id: ids) { //商品詳細資訊列表 List<SortingDetailListResponse> sortDetListRep = new ArrayList<>(); //根據id查詢主分揀單 SortingList sortingList = sortingListService.selectById(id); //根據id查詢分揀詳細單列表資訊 HashMap<String , Object> paraMap = new HashMap<>(); paraMap.put("slist_id",id); List<SortingListDetail> sortingListDetailList = sortingListDetailService.selectByMap(paraMap); System.out.println("sortingListDetailList="+sortingListDetailList); int productQuantity = 0; int coloneNum = 0; List<Integer> coloneIdsList = new ArrayList<>(); //迴圈獲取到線路下的分揀詳細單下的specId(規格id) for (SortingListDetail sortingListDetail : sortingListDetailList) { productQuantity = addOperation(sortingListDetail,productQuantity,sortDetListRep); //團長數 HashMap<String , Object> coloneMap = new HashMap<>(); coloneMap.put("slist_detail_id",sortingListDetail.getId()); List<ColoneSortingListDetail> coloneSortingListDetailList = coloneSortingListDetailService.selectByMap(coloneMap); System.out.println("coloneSortingListDetailList="+coloneSortingListDetailList); if(coloneSortingListDetailList != null && !coloneSortingListDetailList.isEmpty()){ for (ColoneSortingListDetail coloneSortListDetail:coloneSortingListDetailList) { coloneIdsList.add(coloneSortListDetail.getColonelId()); } } } //根據團長id進行去重 List coloneList = removeDuplicate(coloneIdsList); SinglePrintSortingList singlePrintSortingList = new SinglePrintSortingList(); singlePrintSortingList.setSortDetRespList(sortDetListRep); singlePrintSortingList.setEndTime(getCurrentTime()); singlePrintSortingList.setSortingListId(id); singlePrintSortingList.setSortingListRoute(sortingList.getName()); singlePrintSortingList.setProductQuantity(productQuantity); singlePrintSortingList.setColoneNum(coloneList.size()); System.out.println("singlePrintSortingList---"+singlePrintSortingList); //只才是主要的程式碼,以上是我copy文傑同志程式碼拿資料 //生成Excel表格物件 HSSFWorkbook workbook = new HSSFWorkbook(); //建立一個sheet物件 HSSFSheet sheet = workbook.createSheet(sortingList.getLine()); //獲取單元格樣式物件 HSSFCellStyle cellStyle = setBorderHssfCellStyle(workbook); // 設定列的寬度(第3,6列) sheet.setColumnWidth(2, 20 * 256); sheet.setColumnWidth(5, 20 * 256); //Excel頭部資訊 HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(0); String SortingName = sortingList.getName(); cell.setCellValue("分揀單線路:" + SortingName); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));//合併單元格 SetExcelHeaderCellStyle(workbook, cell); //以下也是合併單元格 SetExcelHeaderInfoHssfCell(sheet, 1, sortingList.getId() + "", "單號:", 5); SetExcelHeaderInfoHssfCell(sheet, 2, singlePrintSortingList.getProductQuantity() + "", "貨品總數:", 5); SetExcelHeaderInfoHssfCell(sheet, 3, singlePrintSortingList.getColoneNum() + "", "團長數:", 5); SetHssCellTime(workbook, sheet, 4, 5);//設定匯出時間 //表頭資訊 String[] headers = {"序號","商品編碼","商品名稱","規格","數量","備註"}; HSSFRow row5 = sheet.createRow(5); //設定行高 row5.setHeightInPoints(18); for (int i = 0; i < headers.length; i++) { HSSFCell cell6 = row5.createCell(i); SetHssfCellStyle(workbook, cell6); cell6.setCellValue(headers[i]); } //迴圈輸出商品資訊 int a = 5;//單元格遞增變數 int b = 0;//序號遞增變數 for (SortingListDetail so:sortingListDetailList) { a++; b++; HSSFRow row6 = sheet.createRow(a); setProductInfo(workbook,row6,0,b+"");//序號 setProductInfo(workbook,row6,1,so.getMerchantCode());//商品編碼 setProductInfo(workbook,row6,2,so.getProductName());//商品名稱 setProductInfo(workbook,row6,3,so.getSpec()+"");//規格 setProductInfo(workbook,row6,4,so.getTotalNum()+"");//數量 setProductInfo(workbook,row6,5,"");//備註不設值,客戶填寫 } SetRow(workbook,sheet, a, 1, "倉庫交接人:"); SetRow(workbook,sheet, a, 2, "司機:"); SetRow(workbook,sheet, a, 3, "交接時間:"); //excel下載名字 String filename=sortingList.getName()+".xls"; //指定目錄建立Excel FileOutputStream fileOutputStream = new FileOutputStream(zipFilPath.getPath() + "/" + filename); workbook.write(fileOutputStream); File file = new File(zipFilPath.getPath() + "/" + filename); //壓縮檔案列表集合 files.add(file); workbook.close(); } //壓縮包存放路徑 FileOutputStream fileOutputStream = new FileOutputStream(zipFilPath.getPath()+"/"+"分揀單"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+ ".zip"); ZipUtils.toZip(files,fileOutputStream); url =zipFilPath.getHttp()+"/"+"分揀單"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+ ".zip"; System.out.println("壓縮包地址:"+url); if (result.hasErrors()) { return new ResponseData<>(ResponseCodeEnum.ERROR_NOT_FOUND.getValue(), result.getFieldError().getDefaultMessage()); } }catch (Exception e){ e.printStackTrace(); } return ResponseData.build(ResponseCodeEnum.SUCCESS.getValue(), ResponseCodeEnum.SUCCESS.getMessage(),url); } /** * 設定產品資訊輸出 * @param workbook * @param row * @param no * @param info */ private void setProductInfo(HSSFWorkbook workbook, HSSFRow row, int no, String info){ HSSFCell cell = row.createCell(no); row.setHeightInPoints(15);//設定行高 cell.setCellStyle(setBorderHssfCellStyle(workbook));//設定單元格邊框 cell.setCellValue(info); } /** * 設定單元格上下左右邊框,並居中顯示 * @param workbook * @return */ private HSSFCellStyle setBorderHssfCellStyle(HSSFWorkbook workbook) { HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框 cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下邊框 cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框 cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中 return cellStyle; } /** * 設定列印時間,並格式化時間樣式 例如:2019/1/9 18:21:37 * @param workbook * @param sheet * @param i2 * @param i3 */ private void SetHssCellTime(HSSFWorkbook workbook, HSSFSheet sheet, int i2, int i3) { HSSFRow row4 = sheet.createRow(i2); HSSFCell cell5 = row4.createCell(0); cell5.setCellValue("列印時間:"); HSSFCell cell4 = row4.createCell(1); cell4.setCellValue(new Date()); HSSFCellStyle cellStyle1 = workbook.createCellStyle(); cellStyle1.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")); cell4.setCellStyle(cellStyle1); sheet.addMergedRegion(new CellRangeAddress(i2, i2, 1, i3)); } /** * 設定頁首資訊 * @param sheet * @param i2 行數 * @param line 資料名 * @param s 資料來源 * @param i3 每行的單元格數-1 */ private void SetExcelHeaderInfoHssfCell(HSSFSheet sheet, int i2, String line, String s, int i3) { HSSFRow row = sheet.createRow(i2); HSSFCell cell = row.createCell(0); cell.setCellValue(s + line); sheet.addMergedRegion(new CellRangeAddress(i2, i2, 0, i3)); } /** * 設定Excel頁首第一行樣式 * @param workbook * @param cell */ private void SetExcelHeaderCellStyle(HSSFWorkbook workbook, HSSFCell cell) { HSSFFont font = workbook.createFont();//獲取字型設定物件 font.setBold(true);//加粗 font.setFontHeightInPoints((short)20);//設定字號 HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setFont(font); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中 cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直居中 cell.setCellStyle(cellStyle); } /** * 設定Excel頁尾資訊 * @param sheet * @param a 自增變數 * @param i2 ++數 * @param s 輸出的文字 */ private void SetRow(HSSFWorkbook workbook,HSSFSheet sheet, int a, int i2, String s) { HSSFRow row8 = sheet.createRow(a + i2); row8.setHeightInPoints(30); HSSFCell cell = row8.createCell(0); cell.setCellValue(s); sheet.addMergedRegion(new CellRangeAddress(a + i2, a + i2, 0, 5)); } /** * 設定Excel 表頭<th>樣式 * @param workbook * @param cell */ private void SetHssfCellStyle(HSSFWorkbook workbook, HSSFCell cell) { HSSFFont font1 = workbook.createFont(); font1.setBold(true); HSSFCellStyle cellStyle2 = workbook.createCellStyle(); cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 cellStyle2.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直居中 cellStyle2.setFont(font1); cellStyle2.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框 cellStyle2.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下邊框 cellStyle2.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框 cellStyle2.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框 cell.setCellStyle(cellStyle2); }