Sfoglia il codice sorgente

Excel注解支持backgroundColor属性设置背景色

RuoYi 3 anni fa
parent
commit
9ebd484f46

+ 16 - 1
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/annotation/Excel.java

@@ -104,7 +104,22 @@ public @interface Excel
104
     public ColumnType cellType() default ColumnType.STRING;
104
     public ColumnType cellType() default ColumnType.STRING;
105
 
105
 
106
     /**
106
     /**
107
-     * 导出字体颜色
107
+     * 导出列头背景色
108
+     */
109
+    public IndexedColors headerBackgroundColor() default IndexedColors.GREY_50_PERCENT;
110
+
111
+    /**
112
+     * 导出列头字体颜色
113
+     */
114
+    public IndexedColors headerColor() default IndexedColors.WHITE;
115
+
116
+    /**
117
+     * 导出单元格背景色
118
+     */
119
+    public IndexedColors backgroundColor() default IndexedColors.WHITE;
120
+
121
+    /**
122
+     * 导出单元格字体颜色
108
      */
123
      */
109
     public IndexedColors color() default IndexedColors.BLACK;
124
     public IndexedColors color() default IndexedColors.BLACK;
110
 
125
 

+ 44 - 20
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java

@@ -525,20 +525,6 @@ public class ExcelUtil<T>
525
         styles.put("data", style);
525
         styles.put("data", style);
526
 
526
 
527
         style = wb.createCellStyle();
527
         style = wb.createCellStyle();
528
-        style.cloneStyleFrom(styles.get("data"));
529
-        style.setAlignment(HorizontalAlignment.CENTER);
530
-        style.setVerticalAlignment(VerticalAlignment.CENTER);
531
-        style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
532
-        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
533
-        Font headerFont = wb.createFont();
534
-        headerFont.setFontName("Arial");
535
-        headerFont.setFontHeightInPoints((short) 10);
536
-        headerFont.setBold(true);
537
-        headerFont.setColor(IndexedColors.WHITE.getIndex());
538
-        style.setFont(headerFont);
539
-        styles.put("header", style);
540
-
541
-        style = wb.createCellStyle();
542
         style.setAlignment(HorizontalAlignment.CENTER);
528
         style.setAlignment(HorizontalAlignment.CENTER);
543
         style.setVerticalAlignment(VerticalAlignment.CENTER);
529
         style.setVerticalAlignment(VerticalAlignment.CENTER);
544
         Font totalFont = wb.createFont();
530
         Font totalFont = wb.createFont();
@@ -547,24 +533,60 @@ public class ExcelUtil<T>
547
         style.setFont(totalFont);
533
         style.setFont(totalFont);
548
         styles.put("total", style);
534
         styles.put("total", style);
549
 
535
 
550
-        styles.putAll(annotationStyles(wb));
536
+        styles.putAll(annotationHeaderStyles(wb, styles));
537
+
538
+        styles.putAll(annotationDataStyles(wb));
551
 
539
 
552
         return styles;
540
         return styles;
553
     }
541
     }
554
 
542
 
555
     /**
543
     /**
556
-     * 根据Excel注解创建表格样式
544
+     * 根据Excel注解创建表格头样式
545
+     * 
546
+     * @param wb 工作薄对象
547
+     * @return 自定义样式列表
548
+     */
549
+    private Map<String, CellStyle> annotationHeaderStyles(Workbook wb, Map<String, CellStyle> styles)
550
+    {
551
+        Map<String, CellStyle> headerStyles = new HashMap<String, CellStyle>();
552
+        for (Object[] os : fields)
553
+        {
554
+            Excel excel = (Excel) os[1];
555
+            String key = StringUtils.format("header_{}_{}", excel.headerColor(), excel.headerBackgroundColor());
556
+            if (!headerStyles.containsKey(key))
557
+            {
558
+                CellStyle style = wb.createCellStyle();
559
+                style = wb.createCellStyle();
560
+                style.cloneStyleFrom(styles.get("data"));
561
+                style.setAlignment(HorizontalAlignment.CENTER);
562
+                style.setVerticalAlignment(VerticalAlignment.CENTER);
563
+                style.setFillForegroundColor(excel.headerBackgroundColor().index);
564
+                style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
565
+                Font headerFont = wb.createFont();
566
+                headerFont.setFontName("Arial");
567
+                headerFont.setFontHeightInPoints((short) 10);
568
+                headerFont.setBold(true);
569
+                headerFont.setColor(excel.headerColor().index);
570
+                style.setFont(headerFont);
571
+                headerStyles.put(key, style);
572
+            }
573
+        }
574
+        return headerStyles;
575
+    }
576
+
577
+    /**
578
+     * 根据Excel注解创建表格列样式
557
      * 
579
      * 
558
      * @param wb 工作薄对象
580
      * @param wb 工作薄对象
559
      * @return 自定义样式列表
581
      * @return 自定义样式列表
560
      */
582
      */
561
-    private Map<String, CellStyle> annotationStyles(Workbook wb)
583
+    private Map<String, CellStyle> annotationDataStyles(Workbook wb)
562
     {
584
     {
563
         Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
585
         Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
564
         for (Object[] os : fields)
586
         for (Object[] os : fields)
565
         {
587
         {
566
             Excel excel = (Excel) os[1];
588
             Excel excel = (Excel) os[1];
567
-            String key = "data_" + excel.align() + "_" + excel.color();
589
+            String key = StringUtils.format("data_{}_{}_{}", excel.align(), excel.color(), excel.backgroundColor());
568
             if (!styles.containsKey(key))
590
             if (!styles.containsKey(key))
569
             {
591
             {
570
                 CellStyle style = wb.createCellStyle();
592
                 CellStyle style = wb.createCellStyle();
@@ -579,6 +601,8 @@ public class ExcelUtil<T>
579
                 style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
601
                 style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
580
                 style.setBorderBottom(BorderStyle.THIN);
602
                 style.setBorderBottom(BorderStyle.THIN);
581
                 style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
603
                 style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
604
+                style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
605
+                style.setFillForegroundColor(excel.backgroundColor().getIndex());
582
                 Font dataFont = wb.createFont();
606
                 Font dataFont = wb.createFont();
583
                 dataFont.setFontName("Arial");
607
                 dataFont.setFontName("Arial");
584
                 dataFont.setFontHeightInPoints((short) 10);
608
                 dataFont.setFontHeightInPoints((short) 10);
@@ -600,7 +624,7 @@ public class ExcelUtil<T>
600
         // 写入列信息
624
         // 写入列信息
601
         cell.setCellValue(attr.name());
625
         cell.setCellValue(attr.name());
602
         setDataValidation(attr, row, column);
626
         setDataValidation(attr, row, column);
603
-        cell.setCellStyle(styles.get("header"));
627
+        cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor())));
604
         return cell;
628
         return cell;
605
     }
629
     }
606
 
630
 
@@ -708,7 +732,7 @@ public class ExcelUtil<T>
708
             {
732
             {
709
                 // 创建cell
733
                 // 创建cell
710
                 cell = row.createCell(column);
734
                 cell = row.createCell(column);
711
-                cell.setCellStyle(styles.get("data_" + attr.align() + "_" + attr.color()));
735
+                cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor())));
712
 
736
 
713
                 // 用于读取对象中的属性
737
                 // 用于读取对象中的属性
714
                 Object value = getTargetValue(vo, field, attr);
738
                 Object value = getTargetValue(vo, field, attr);