Просмотр исходного кода

Excel注解ColumnType类型新增文本

RuoYi лет назад: 2
Родитель
Сommit
735273d69f

+ 1 - 1
ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/domain/SysUser.java

@@ -42,7 +42,7 @@ public class SysUser extends BaseEntity
42
     private String email;
42
     private String email;
43
 
43
 
44
     /** 手机号码 */
44
     /** 手机号码 */
45
-    @Excel(name = "手机号码")
45
+    @Excel(name = "手机号码", cellType = ColumnType.TEXT)
46
     private String phonenumber;
46
     private String phonenumber;
47
 
47
 
48
     /** 用户性别 */
48
     /** 用户性别 */

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

@@ -166,7 +166,7 @@ public @interface Excel
166
 
166
 
167
     public enum ColumnType
167
     public enum ColumnType
168
     {
168
     {
169
-        NUMERIC(0), STRING(1), IMAGE(2);
169
+        NUMERIC(0), STRING(1), IMAGE(2), TEXT(3);
170
         private final int value;
170
         private final int value;
171
 
171
 
172
         ColumnType(int value)
172
         ColumnType(int value)

+ 63 - 25
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java

@@ -28,6 +28,7 @@ import org.apache.poi.ss.usermodel.Cell;
28
 import org.apache.poi.ss.usermodel.CellStyle;
28
 import org.apache.poi.ss.usermodel.CellStyle;
29
 import org.apache.poi.ss.usermodel.CellType;
29
 import org.apache.poi.ss.usermodel.CellType;
30
 import org.apache.poi.ss.usermodel.ClientAnchor;
30
 import org.apache.poi.ss.usermodel.ClientAnchor;
31
+import org.apache.poi.ss.usermodel.DataFormat;
31
 import org.apache.poi.ss.usermodel.DataValidation;
32
 import org.apache.poi.ss.usermodel.DataValidation;
32
 import org.apache.poi.ss.usermodel.DataValidationConstraint;
33
 import org.apache.poi.ss.usermodel.DataValidationConstraint;
33
 import org.apache.poi.ss.usermodel.DataValidationHelper;
34
 import org.apache.poi.ss.usermodel.DataValidationHelper;
@@ -652,6 +653,8 @@ public class ExcelUtil<T>
652
         titleFont.setFontHeightInPoints((short) 16);
653
         titleFont.setFontHeightInPoints((short) 16);
653
         titleFont.setBold(true);
654
         titleFont.setBold(true);
654
         style.setFont(titleFont);
655
         style.setFont(titleFont);
656
+        DataFormat dataFormat = wb.createDataFormat();
657
+        style.setDataFormat(dataFormat.getFormat("@"));
655
         styles.put("title", style);
658
         styles.put("title", style);
656
 
659
 
657
         style = wb.createCellStyle();
660
         style = wb.createCellStyle();
@@ -714,6 +717,9 @@ public class ExcelUtil<T>
714
                 headerFont.setBold(true);
717
                 headerFont.setBold(true);
715
                 headerFont.setColor(excel.headerColor().index);
718
                 headerFont.setColor(excel.headerColor().index);
716
                 style.setFont(headerFont);
719
                 style.setFont(headerFont);
720
+                // 设置表格头单元格文本形式
721
+                DataFormat dataFormat = wb.createDataFormat();
722
+                style.setDataFormat(dataFormat.getFormat("@"));
717
                 headerStyles.put(key, style);
723
                 headerStyles.put(key, style);
718
             }
724
             }
719
         }
725
         }
@@ -731,35 +737,67 @@ public class ExcelUtil<T>
731
         Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
737
         Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
732
         for (Object[] os : fields)
738
         for (Object[] os : fields)
733
         {
739
         {
740
+            Field field = (Field) os[0];
734
             Excel excel = (Excel) os[1];
741
             Excel excel = (Excel) os[1];
735
-            String key = StringUtils.format("data_{}_{}_{}", excel.align(), excel.color(), excel.backgroundColor());
736
-            if (!styles.containsKey(key))
742
+            if (Collection.class.isAssignableFrom(field.getType()))
737
             {
743
             {
738
-                CellStyle style = wb.createCellStyle();
739
-                style.setAlignment(excel.align());
740
-                style.setVerticalAlignment(VerticalAlignment.CENTER);
741
-                style.setBorderRight(BorderStyle.THIN);
742
-                style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
743
-                style.setBorderLeft(BorderStyle.THIN);
744
-                style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
745
-                style.setBorderTop(BorderStyle.THIN);
746
-                style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
747
-                style.setBorderBottom(BorderStyle.THIN);
748
-                style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
749
-                style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
750
-                style.setFillForegroundColor(excel.backgroundColor().getIndex());
751
-                Font dataFont = wb.createFont();
752
-                dataFont.setFontName("Arial");
753
-                dataFont.setFontHeightInPoints((short) 10);
754
-                dataFont.setColor(excel.color().index);
755
-                style.setFont(dataFont);
756
-                styles.put(key, style);
744
+                ParameterizedType pt = (ParameterizedType) field.getGenericType();
745
+                Class<?> subClass = (Class<?>) pt.getActualTypeArguments()[0];
746
+                List<Field> subFields = FieldUtils.getFieldsListWithAnnotation(subClass, Excel.class);
747
+                for (Field subField : subFields)
748
+                {
749
+                    Excel subExcel = subField.getAnnotation(Excel.class);
750
+                    annotationDataStyles(styles, subField, subExcel);
751
+                }
752
+            }
753
+            else
754
+            {
755
+                annotationDataStyles(styles, field, excel);
757
             }
756
             }
758
         }
757
         }
759
         return styles;
758
         return styles;
760
     }
759
     }
761
 
760
 
762
     /**
761
     /**
762
+     * 根据Excel注解创建表格列样式
763
+     * 
764
+     * @param styles 自定义样式列表
765
+     * @param field  属性列信息
766
+     * @param excel  注解信息
767
+     */
768
+    public void annotationDataStyles(Map<String, CellStyle> styles, Field field, Excel excel)
769
+    {
770
+        String key = StringUtils.format("data_{}_{}_{}_{}", excel.align(), excel.color(), excel.backgroundColor(), excel.cellType());
771
+        if (!styles.containsKey(key))
772
+        {
773
+            CellStyle style = wb.createCellStyle();
774
+            style.setAlignment(excel.align());
775
+            style.setVerticalAlignment(VerticalAlignment.CENTER);
776
+            style.setBorderRight(BorderStyle.THIN);
777
+            style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
778
+            style.setBorderLeft(BorderStyle.THIN);
779
+            style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
780
+            style.setBorderTop(BorderStyle.THIN);
781
+            style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
782
+            style.setBorderBottom(BorderStyle.THIN);
783
+            style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
784
+            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
785
+            style.setFillForegroundColor(excel.backgroundColor().getIndex());
786
+            Font dataFont = wb.createFont();
787
+            dataFont.setFontName("Arial");
788
+            dataFont.setFontHeightInPoints((short) 10);
789
+            dataFont.setColor(excel.color().index);
790
+            style.setFont(dataFont);
791
+            if (ColumnType.TEXT == excel.cellType())
792
+            {
793
+                DataFormat dataFormat = wb.createDataFormat();
794
+                style.setDataFormat(dataFormat.getFormat("@"));
795
+            }
796
+            styles.put(key, style);
797
+        }
798
+    }
799
+
800
+    /**
763
      * 创建单元格
801
      * 创建单元格
764
      */
802
      */
765
     public Cell createHeadCell(Excel attr, Row row, int column)
803
     public Cell createHeadCell(Excel attr, Row row, int column)
@@ -773,7 +811,7 @@ public class ExcelUtil<T>
773
         if (isSubList())
811
         if (isSubList())
774
         {
812
         {
775
             // 填充默认样式,防止合并单元格样式失效
813
             // 填充默认样式,防止合并单元格样式失效
776
-            sheet.setDefaultColumnStyle(column, styles.get(StringUtils.format("data_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor())));
814
+            sheet.setDefaultColumnStyle(column, styles.get(StringUtils.format("data_{}_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor(), attr.cellType())));
777
             if (attr.needMerge())
815
             if (attr.needMerge())
778
             {
816
             {
779
                 sheet.addMergedRegion(new CellRangeAddress(rownum - 1, rownum, column, column));
817
                 sheet.addMergedRegion(new CellRangeAddress(rownum - 1, rownum, column, column));
@@ -791,7 +829,7 @@ public class ExcelUtil<T>
791
      */
829
      */
792
     public void setCellVo(Object value, Excel attr, Cell cell)
830
     public void setCellVo(Object value, Excel attr, Cell cell)
793
     {
831
     {
794
-        if (ColumnType.STRING == attr.cellType())
832
+        if (ColumnType.STRING == attr.cellType() || ColumnType.TEXT == attr.cellType())
795
         {
833
         {
796
             String cellValue = Convert.toStr(value);
834
             String cellValue = Convert.toStr(value);
797
             // 对于任何以表达式触发字符 =-+@开头的单元格,直接使用tab字符作为前缀,防止CSV注入。
835
             // 对于任何以表达式触发字符 =-+@开头的单元格,直接使用tab字符作为前缀,防止CSV注入。
@@ -903,7 +941,7 @@ public class ExcelUtil<T>
903
                     CellRangeAddress cellAddress = new CellRangeAddress(subMergedFirstRowNum, subMergedLastRowNum, column, column);
941
                     CellRangeAddress cellAddress = new CellRangeAddress(subMergedFirstRowNum, subMergedLastRowNum, column, column);
904
                     sheet.addMergedRegion(cellAddress);
942
                     sheet.addMergedRegion(cellAddress);
905
                 }
943
                 }
906
-                cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor())));
944
+                cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor(), attr.cellType())));
907
 
945
 
908
                 // 用于读取对象中的属性
946
                 // 用于读取对象中的属性
909
                 Object value = getTargetValue(vo, field, attr);
947
                 Object value = getTargetValue(vo, field, attr);
@@ -1032,7 +1070,7 @@ public class ExcelUtil<T>
1032
 
1070
 
1033
     /**
1071
     /**
1034
      * 解析导出值 0=男,1=女,2=未知
1072
      * 解析导出值 0=男,1=女,2=未知
1035
-     *
1073
+     * 
1036
      * @param propertyValue 参数值
1074
      * @param propertyValue 参数值
1037
      * @param converterExp 翻译注解
1075
      * @param converterExp 翻译注解
1038
      * @param separator 分隔符
1076
      * @param separator 分隔符