From ef76b0c2d281a2aa649ccc0bfe99fd85c570b916 Mon Sep 17 00:00:00 2001 From: RuoYi <yzz_ivy@163.com> Date: 星期四, 21 七月 2022 13:51:34 +0800 Subject: [PATCH] 支持自定义隐藏Excel属性列 --- src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java | 116 ++++++++++++++++++++++++++++++++++++++++------------------ 1 files changed, 80 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java b/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java index d3e02dd..7e28837 100644 --- a/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java +++ b/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java @@ -22,6 +22,7 @@ import java.util.UUID; import java.util.stream.Collectors; import javax.servlet.http.HttpServletResponse; +import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.RegExUtils; import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFPicture; @@ -163,9 +164,25 @@ */ public Class<T> clazz; + /** + * 闇�瑕佹帓闄ゅ垪灞炴�� + */ + public String[] excludeFields; + public ExcelUtil(Class<T> clazz) { this.clazz = clazz; + } + + /** + * 闅愯棌Excel涓垪灞炴�� + * + * @param fields 鍒楀睘鎬у悕 绀轰緥[鍗曚釜"name"/澶氫釜"id","name"] + * @throws Exception + */ + public void hideColumn(String... fields) + { + this.excludeFields = fields; } public void init(List<T> list, String sheetName, String title, Type type) @@ -650,20 +667,6 @@ styles.put("data", style); style = wb.createCellStyle(); - style.cloneStyleFrom(styles.get("data")); - style.setAlignment(HorizontalAlignment.CENTER); - style.setVerticalAlignment(VerticalAlignment.CENTER); - style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); - style.setFillPattern(FillPatternType.SOLID_FOREGROUND); - Font headerFont = wb.createFont(); - headerFont.setFontName("Arial"); - headerFont.setFontHeightInPoints((short) 10); - headerFont.setBold(true); - headerFont.setColor(IndexedColors.WHITE.getIndex()); - style.setFont(headerFont); - styles.put("header", style); - - style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); Font totalFont = wb.createFont(); @@ -672,24 +675,60 @@ style.setFont(totalFont); styles.put("total", style); - styles.putAll(annotationStyles(wb)); + styles.putAll(annotationHeaderStyles(wb, styles)); + + styles.putAll(annotationDataStyles(wb)); return styles; } /** - * 鏍规嵁Excel娉ㄨВ鍒涘缓琛ㄦ牸鏍峰紡 + * 鏍规嵁Excel娉ㄨВ鍒涘缓琛ㄦ牸澶存牱寮� * * @param wb 宸ヤ綔钖勫璞� * @return 鑷畾涔夋牱寮忓垪琛� */ - private Map<String, CellStyle> annotationStyles(Workbook wb) + private Map<String, CellStyle> annotationHeaderStyles(Workbook wb, Map<String, CellStyle> styles) + { + Map<String, CellStyle> headerStyles = new HashMap<String, CellStyle>(); + for (Object[] os : fields) + { + Excel excel = (Excel) os[1]; + String key = StringUtils.format("header_{}_{}", excel.headerColor(), excel.headerBackgroundColor()); + if (!headerStyles.containsKey(key)) + { + CellStyle style = wb.createCellStyle(); + style = wb.createCellStyle(); + style.cloneStyleFrom(styles.get("data")); + style.setAlignment(HorizontalAlignment.CENTER); + style.setVerticalAlignment(VerticalAlignment.CENTER); + style.setFillForegroundColor(excel.headerBackgroundColor().index); + style.setFillPattern(FillPatternType.SOLID_FOREGROUND); + Font headerFont = wb.createFont(); + headerFont.setFontName("Arial"); + headerFont.setFontHeightInPoints((short) 10); + headerFont.setBold(true); + headerFont.setColor(excel.headerColor().index); + style.setFont(headerFont); + headerStyles.put(key, style); + } + } + return headerStyles; + } + + /** + * 鏍规嵁Excel娉ㄨВ鍒涘缓琛ㄦ牸鍒楁牱寮� + * + * @param wb 宸ヤ綔钖勫璞� + * @return 鑷畾涔夋牱寮忓垪琛� + */ + private Map<String, CellStyle> annotationDataStyles(Workbook wb) { Map<String, CellStyle> styles = new HashMap<String, CellStyle>(); for (Object[] os : fields) { Excel excel = (Excel) os[1]; - String key = "data_" + excel.align() + "_" + excel.color(); + String key = StringUtils.format("data_{}_{}_{}", excel.align(), excel.color(), excel.backgroundColor()); if (!styles.containsKey(key)) { CellStyle style = wb.createCellStyle(); @@ -704,6 +743,8 @@ style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setBorderBottom(BorderStyle.THIN); style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); + style.setFillPattern(FillPatternType.SOLID_FOREGROUND); + style.setFillForegroundColor(excel.backgroundColor().getIndex()); Font dataFont = wb.createFont(); dataFont.setFontName("Arial"); dataFont.setFontHeightInPoints((short) 10); @@ -725,7 +766,7 @@ // 鍐欏叆鍒椾俊鎭� cell.setCellValue(attr.name()); setDataValidation(attr, row, column); - cell.setCellStyle(styles.get("header")); + cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor()))); return cell; } @@ -833,7 +874,7 @@ { // 鍒涘缓cell cell = row.createCell(column); - cell.setCellStyle(styles.get("data_" + attr.align() + "_" + attr.color())); + cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor()))); // 鐢ㄤ簬璇诲彇瀵硅薄涓殑灞炴�� Object value = getTargetValue(vo, field, attr); @@ -1178,30 +1219,33 @@ tempFields.addAll(Arrays.asList(clazz.getDeclaredFields())); for (Field field : tempFields) { - // 鍗曟敞瑙� - if (field.isAnnotationPresent(Excel.class)) + if (!ArrayUtils.contains(this.excludeFields, field.getName())) { - Excel attr = field.getAnnotation(Excel.class); - if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) + // 鍗曟敞瑙� + if (field.isAnnotationPresent(Excel.class)) { - field.setAccessible(true); - fields.add(new Object[] { field, attr }); - } - } - - // 澶氭敞瑙� - if (field.isAnnotationPresent(Excels.class)) - { - Excels attrs = field.getAnnotation(Excels.class); - Excel[] excels = attrs.value(); - for (Excel attr : excels) - { + Excel attr = field.getAnnotation(Excel.class); if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) { field.setAccessible(true); fields.add(new Object[] { field, attr }); } } + + // 澶氭敞瑙� + if (field.isAnnotationPresent(Excels.class)) + { + Excels attrs = field.getAnnotation(Excels.class); + Excel[] excels = attrs.value(); + for (Excel attr : excels) + { + if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) + { + field.setAccessible(true); + fields.add(new Object[] { field, attr }); + } + } + } } } return fields; -- Gitblit v1.9.3