ソースを参照

Excel支持注解align对齐方式

RuoYi 5 年 前
コミット
e526e33030

+ 21 - 0
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/annotation/Excel.java

@@ -101,6 +101,27 @@ public @interface Excel
101 101
     public boolean isStatistics() default false;
102 102
 
103 103
     /**
104
+     * 导出字段对齐方式(0:默认;1:靠左;2:居中;3:靠右)
105
+     */
106
+    Align align() default Align.AUTO;
107
+
108
+    public enum Align
109
+    {
110
+        AUTO(0), LEFT(1), CENTER(2), RIGHT(3);
111
+        private final int value;
112
+
113
+        Align(int value)
114
+        {
115
+            this.value = value;
116
+        }
117
+
118
+        public int value()
119
+        {
120
+            return this.value;
121
+        }
122
+    }
123
+
124
+    /**
104 125
      * 字段类型(0:导出导入;1:仅导出;2:仅导入)
105 126
      */
106 127
     Type type() default Type.ALL;

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

@@ -464,6 +464,21 @@ public class ExcelUtil<T>
464 464
         style.setFont(totalFont);
465 465
         styles.put("total", style);
466 466
 
467
+        style = wb.createCellStyle();
468
+        style.cloneStyleFrom(styles.get("data"));
469
+        style.setAlignment(HorizontalAlignment.LEFT);
470
+        styles.put("data1", style);
471
+
472
+        style = wb.createCellStyle();
473
+        style.cloneStyleFrom(styles.get("data"));
474
+        style.setAlignment(HorizontalAlignment.CENTER);
475
+        styles.put("data2", style);
476
+
477
+        style = wb.createCellStyle();
478
+        style.cloneStyleFrom(styles.get("data"));
479
+        style.setAlignment(HorizontalAlignment.RIGHT);
480
+        styles.put("data3", style);
481
+
467 482
         return styles;
468 483
     }
469 484
 
@@ -546,7 +561,8 @@ public class ExcelUtil<T>
546 561
             {
547 562
                 // 创建cell
548 563
                 cell = row.createCell(column);
549
-                cell.setCellStyle(styles.get("data"));
564
+                int align = attr.align().value();
565
+                cell.setCellStyle(styles.get("data" + (align >= 1 && align <= 3 ? align : "")));
550 566
 
551 567
                 // 用于读取对象中的属性
552 568
                 Object value = getTargetValue(vo, field, attr);