From e4ed89aae12459bd54fd3d53a7a46ce48f74e37f Mon Sep 17 00:00:00 2001 From: jiangping <jp@doumee.com> Date: 星期五, 09 五月 2025 15:01:11 +0800 Subject: [PATCH] 最新版本541200007 --- server/system_service/src/main/java/com/doumee/core/annotation/excel/ExcelExporter.java | 86 ++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 85 insertions(+), 1 deletions(-) diff --git a/server/system_service/src/main/java/com/doumee/core/annotation/excel/ExcelExporter.java b/server/system_service/src/main/java/com/doumee/core/annotation/excel/ExcelExporter.java index 670f20c..5408db0 100644 --- a/server/system_service/src/main/java/com/doumee/core/annotation/excel/ExcelExporter.java +++ b/server/system_service/src/main/java/com/doumee/core/annotation/excel/ExcelExporter.java @@ -2,6 +2,7 @@ import com.doumee.core.constants.ResponseStatus; import com.doumee.core.exception.BusinessException; +import com.doumee.core.utils.Constants; import lombok.AllArgsConstructor; import lombok.Data; import org.apache.poi.ss.usermodel.*; @@ -359,7 +360,7 @@ /** * 閰嶇疆鍗曞厓鏍艰竟妗� */ - private void configCellBorder (CellStyle style) { + private static void configCellBorder(CellStyle style) { style.setBorderTop(BorderStyle.THIN); style.setBorderRight(BorderStyle.THIN); style.setBorderBottom(BorderStyle.THIN); @@ -428,4 +429,87 @@ private Field field; } + /** + * 瀵煎嚭鍒版寚瀹氳緭鍑烘祦 + * @param os 杈撳嚭娴� + */ + public static void exportList (List<List<String>> dataList ,String sheetName, OutputStream os) { + SXSSFWorkbook sxssfWorkbook; + try { + sxssfWorkbook = new SXSSFWorkbook(); + Sheet sheet = sxssfWorkbook.createSheet(sheetName); + // 鍒涘缓鍒楀ご + sheet.createFreezePane(0, 1); + CellStyle hstyle = configHeaderCellStatic(sxssfWorkbook); + CellStyle rowStyle = configDataCellStatic(sxssfWorkbook); + for (int i = 0; i < dataList.size(); i++) { + Row row = sheet.createRow(i); + List<String> rowList = dataList.get(i); + for (int j = 0; j < rowList.size(); j++) { + Cell cell = row.createCell(j); + cell.setCellValue(rowList.get(j)); + if(i ==0 || j==0){ + // 鍒楀璁剧疆 + sheet.setColumnWidth(i, rowList.get(i).length() * 2 * 256); + // 璁剧疆鍒楀ご鍗曞厓鏍� + cell.setCellStyle(hstyle); + }else{ + cell.setCellStyle(rowStyle); + } + } + } + sxssfWorkbook.write(os); + os.close(); + } catch (Exception e) { + throw new BusinessException(ResponseStatus.EXPORT_EXCEL_ERROR, e); + } finally { + if (os != null) { + try { + os.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + private static CellStyle configHeaderCellStatic (SXSSFWorkbook workbook) { + CellStyle style = workbook.createCellStyle(); + style.setAlignment(HorizontalAlignment.CENTER); + style.setVerticalAlignment(VerticalAlignment.CENTER); + // 璁剧疆鑳屾櫙 + style.setFillPattern(FillPatternType.SOLID_FOREGROUND); + style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); + // 瀛椾綋 + Font font = workbook.createFont(); + font.setFontHeightInPoints((short) 12); + style.setFont(font); + // 璁剧疆杈规 + configCellBorder(style); + return style; + } + + + /** + * 閰嶇疆鏁版嵁鍗曞厓鏍� + */ + private static CellStyle configDataCellStatic (SXSSFWorkbook workbook ) { + CellStyle configDataCellStyle = workbook.createCellStyle(); + configDataCellStyle.setAlignment(HorizontalAlignment.CENTER); + configDataCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); + // 璁剧疆鑳屾櫙 + configDataCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + configDataCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); + // 瀛椾綋 + Font font = workbook.createFont(); + font.setFontHeightInPoints((short) 10); + + configDataCellStyle.setFont(font); + // 杈规 + configCellBorder(configDataCellStyle); + configDataCellStyle.setWrapText(true); + + return configDataCellStyle; + } + } -- Gitblit v1.9.3