Sfoglia il codice sorgente

Excel注解ColumnType类型新增文本

RuoYi 2 anni fa
parent
commit
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 42
     private String email;
43 43
 
44 44
     /** 手机号码 */
45
-    @Excel(name = "手机号码")
45
+    @Excel(name = "手机号码", cellType = ColumnType.TEXT)
46 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 167
     public enum ColumnType
168 168
     {
169
-        NUMERIC(0), STRING(1), IMAGE(2);
169
+        NUMERIC(0), STRING(1), IMAGE(2), TEXT(3);
170 170
         private final int value;
171 171
 
172 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 28
 import org.apache.poi.ss.usermodel.CellStyle;
29 29
 import org.apache.poi.ss.usermodel.CellType;
30 30
 import org.apache.poi.ss.usermodel.ClientAnchor;
31
+import org.apache.poi.ss.usermodel.DataFormat;
31 32
 import org.apache.poi.ss.usermodel.DataValidation;
32 33
 import org.apache.poi.ss.usermodel.DataValidationConstraint;
33 34
 import org.apache.poi.ss.usermodel.DataValidationHelper;
@@ -652,6 +653,8 @@ public class ExcelUtil<T>
652 653
         titleFont.setFontHeightInPoints((short) 16);
653 654
         titleFont.setBold(true);
654 655
         style.setFont(titleFont);
656
+        DataFormat dataFormat = wb.createDataFormat();
657
+        style.setDataFormat(dataFormat.getFormat("@"));
655 658
         styles.put("title", style);
656 659
 
657 660
         style = wb.createCellStyle();
@@ -714,6 +717,9 @@ public class ExcelUtil<T>
714 717
                 headerFont.setBold(true);
715 718
                 headerFont.setColor(excel.headerColor().index);
716 719
                 style.setFont(headerFont);
720
+                // 设置表格头单元格文本形式
721
+                DataFormat dataFormat = wb.createDataFormat();
722
+                style.setDataFormat(dataFormat.getFormat("@"));
717 723
                 headerStyles.put(key, style);
718 724
             }
719 725
         }
@@ -731,35 +737,67 @@ public class ExcelUtil<T>
731 737
         Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
732 738
         for (Object[] os : fields)
733 739
         {
740
+            Field field = (Field) os[0];
734 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 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 803
     public Cell createHeadCell(Excel attr, Row row, int column)
@@ -773,7 +811,7 @@ public class ExcelUtil<T>
773 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 815
             if (attr.needMerge())
778 816
             {
779 817
                 sheet.addMergedRegion(new CellRangeAddress(rownum - 1, rownum, column, column));
@@ -791,7 +829,7 @@ public class ExcelUtil<T>
791 829
      */
792 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 834
             String cellValue = Convert.toStr(value);
797 835
             // 对于任何以表达式触发字符 =-+@开头的单元格,直接使用tab字符作为前缀,防止CSV注入。
@@ -903,7 +941,7 @@ public class ExcelUtil<T>
903 941
                     CellRangeAddress cellAddress = new CellRangeAddress(subMergedFirstRowNum, subMergedLastRowNum, column, column);
904 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 947
                 Object value = getTargetValue(vo, field, attr);
@@ -1032,7 +1070,7 @@ public class ExcelUtil<T>
1032 1070
 
1033 1071
     /**
1034 1072
      * 解析导出值 0=男,1=女,2=未知
1035
-     *
1073
+     * 
1036 1074
      * @param propertyValue 参数值
1037 1075
      * @param converterExp 翻译注解
1038 1076
      * @param separator 分隔符