From 332d717094fd7d85f2fbe1a762b14913ebc03a68 Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: 星期三, 20 七月 2022 09:33:37 +0800
Subject: [PATCH] Excel注解支持backgroundColor属性设置背景色
---
src/main/java/com/ruoyi/framework/aspectj/lang/annotation/Excel.java | 19 ++++++++-
src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java | 64 ++++++++++++++++++++++----------
2 files changed, 61 insertions(+), 22 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..c422a0e 100644
--- a/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
+++ b/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
@@ -650,20 +650,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 +658,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 +726,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 +749,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 +857,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);
diff --git a/src/main/java/com/ruoyi/framework/aspectj/lang/annotation/Excel.java b/src/main/java/com/ruoyi/framework/aspectj/lang/annotation/Excel.java
index a0ac8a2..8fdf7c1 100644
--- a/src/main/java/com/ruoyi/framework/aspectj/lang/annotation/Excel.java
+++ b/src/main/java/com/ruoyi/framework/aspectj/lang/annotation/Excel.java
@@ -102,14 +102,29 @@
* 鏄惁鑷姩缁熻鏁版嵁,鍦ㄦ渶鍚庤拷鍔犱竴琛岀粺璁℃暟鎹�诲拰
*/
public boolean isStatistics() default false;
-
+
/**
* 瀵煎嚭绫诲瀷锛�0鏁板瓧 1瀛楃涓诧級
*/
public ColumnType cellType() default ColumnType.STRING;
/**
- * 瀵煎嚭瀛椾綋棰滆壊
+ * 瀵煎嚭鍒楀ご鑳屾櫙鑹�
+ */
+ public IndexedColors headerBackgroundColor() default IndexedColors.GREY_50_PERCENT;
+
+ /**
+ * 瀵煎嚭鍒楀ご瀛椾綋棰滆壊
+ */
+ public IndexedColors headerColor() default IndexedColors.WHITE;
+
+ /**
+ * 瀵煎嚭鍗曞厓鏍艰儗鏅壊
+ */
+ public IndexedColors backgroundColor() default IndexedColors.WHITE;
+
+ /**
+ * 瀵煎嚭鍗曞厓鏍煎瓧浣撻鑹�
*/
public IndexedColors color() default IndexedColors.BLACK;
--
Gitblit v1.9.3