Ver código fonte

Excel注解支持color字体颜色

RuoYi 4 anos atrás
pai
commit
54d1c58338

+ 14 - 23
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/annotation/Excel.java

@@ -5,6 +5,8 @@ import java.lang.annotation.Retention;
5 5
 import java.lang.annotation.RetentionPolicy;
6 6
 import java.lang.annotation.Target;
7 7
 import java.math.BigDecimal;
8
+import org.apache.poi.ss.usermodel.HorizontalAlignment;
9
+import org.apache.poi.ss.usermodel.IndexedColors;
8 10
 import com.ruoyi.common.core.utils.poi.ExcelHandlerAdapter;
9 11
 
10 12
 /**
@@ -52,11 +54,6 @@ public @interface Excel
52 54
     public int roundingMode() default BigDecimal.ROUND_HALF_EVEN;
53 55
 
54 56
     /**
55
-     * 导出类型(0数字 1字符串)
56
-     */
57
-    public ColumnType cellType() default ColumnType.STRING;
58
-
59
-    /**
60 57
      * 导出时在excel中每个列的高度 单位为字符
61 58
      */
62 59
     public double height() default 14;
@@ -102,9 +99,19 @@ public @interface Excel
102 99
     public boolean isStatistics() default false;
103 100
 
104 101
     /**
105
-     * 导出字段对齐方式(0:默认;1:靠左;2:居中;3:靠右)
102
+     * 导出类型(0数字 1字符串)
103
+     */
104
+    public ColumnType cellType() default ColumnType.STRING;
105
+
106
+    /**
107
+     * 导出字体颜色
108
+     */
109
+    public IndexedColors color() default IndexedColors.BLACK;
110
+
111
+    /**
112
+     * 导出字段对齐方式
106 113
      */
107
-    public Align align() default Align.AUTO;
114
+    public HorizontalAlignment align() default HorizontalAlignment.CENTER;
108 115
 
109 116
     /**
110 117
      * 自定义数据处理器
@@ -116,22 +123,6 @@ public @interface Excel
116 123
      */
117 124
     public String[] args() default {};
118 125
 
119
-    public enum Align
120
-    {
121
-        AUTO(0), LEFT(1), CENTER(2), RIGHT(3);
122
-        private final int value;
123
-
124
-        Align(int value)
125
-        {
126
-            this.value = value;
127
-        }
128
-
129
-        public int value()
130
-        {
131
-            return this.value;
132
-        }
133
-    }
134
-
135 126
     /**
136 127
      * 字段类型(0:导出导入;1:仅导出;2:仅导入)
137 128
      */

+ 29 - 17
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java

@@ -553,21 +553,6 @@ public class ExcelUtil<T>
553 553
         style.setFont(totalFont);
554 554
         styles.put("total", style);
555 555
 
556
-        style = wb.createCellStyle();
557
-        style.cloneStyleFrom(styles.get("data"));
558
-        style.setAlignment(HorizontalAlignment.LEFT);
559
-        styles.put("data1", style);
560
-
561
-        style = wb.createCellStyle();
562
-        style.cloneStyleFrom(styles.get("data"));
563
-        style.setAlignment(HorizontalAlignment.CENTER);
564
-        styles.put("data2", style);
565
-
566
-        style = wb.createCellStyle();
567
-        style.cloneStyleFrom(styles.get("data"));
568
-        style.setAlignment(HorizontalAlignment.RIGHT);
569
-        styles.put("data3", style);
570
-
571 556
         return styles;
572 557
     }
573 558
 
@@ -689,8 +674,7 @@ public class ExcelUtil<T>
689 674
             {
690 675
                 // 创建cell
691 676
                 cell = row.createCell(column);
692
-                int align = attr.align().value();
693
-                cell.setCellStyle(styles.get("data" + (align >= 1 && align <= 3 ? align : "")));
677
+                setDataCell(cell, attr);
694 678
 
695 679
                 // 用于读取对象中的属性
696 680
                 Object value = getTargetValue(vo, field, attr);
@@ -729,6 +713,34 @@ public class ExcelUtil<T>
729 713
     }
730 714
 
731 715
     /**
716
+     * 设置单元格样式
717
+     * 
718
+     * @param cell 单元格
719
+     * @param excel 注解信息
720
+     */
721
+    public void setDataCell(Cell cell, Excel excel)
722
+    {
723
+        CellStyle style = wb.createCellStyle();
724
+        style.setAlignment(HorizontalAlignment.CENTER);
725
+        style.setVerticalAlignment(VerticalAlignment.CENTER);
726
+        style.setBorderRight(BorderStyle.THIN);
727
+        style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
728
+        style.setBorderLeft(BorderStyle.THIN);
729
+        style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
730
+        style.setBorderTop(BorderStyle.THIN);
731
+        style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
732
+        style.setBorderBottom(BorderStyle.THIN);
733
+        style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
734
+        style.setAlignment(excel.align());
735
+        Font dataFont = wb.createFont();
736
+        dataFont.setFontName("Arial");
737
+        dataFont.setFontHeightInPoints((short) 10);
738
+        dataFont.setColor(excel.color().index);
739
+        style.setFont(dataFont);
740
+        cell.setCellStyle(style);
741
+    }
742
+
743
+    /**
732 744
      * 设置 POI XSSFSheet 单元格提示或选择框
733 745
      * 
734 746
      * @param sheet 表单