ソースを参照

Excel注解支持wrapText是否允许内容换行

RuoYi 1 年間 前
コミット
6c3b01c3c5

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

@@ -79,6 +79,11 @@ public @interface Excel
79 79
     public String prompt() default "";
80 80
 
81 81
     /**
82
+     * 是否允许内容换行 
83
+     */
84
+    public boolean wrapText() default false;
85
+
86
+    /**
82 87
      * 设置只能选择不能输入的列内容.
83 88
      */
84 89
     public String[] combo() default {};

+ 4 - 3
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java

@@ -814,7 +814,7 @@ public class ExcelUtil<T>
814 814
      */
815 815
     public void annotationDataStyles(Map<String, CellStyle> styles, Field field, Excel excel)
816 816
     {
817
-        String key = StringUtils.format("data_{}_{}_{}_{}", excel.align(), excel.color(), excel.backgroundColor(), excel.cellType());
817
+        String key = StringUtils.format("data_{}_{}_{}_{}_{}", excel.align(), excel.color(), excel.backgroundColor(), excel.cellType(), excel.wrapText());
818 818
         if (!styles.containsKey(key))
819 819
         {
820 820
             CellStyle style = wb.createCellStyle();
@@ -830,6 +830,7 @@ public class ExcelUtil<T>
830 830
             style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
831 831
             style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
832 832
             style.setFillForegroundColor(excel.backgroundColor().getIndex());
833
+            style.setWrapText(excel.wrapText());
833 834
             Font dataFont = wb.createFont();
834 835
             dataFont.setFontName("Arial");
835 836
             dataFont.setFontHeightInPoints((short) 10);
@@ -858,7 +859,7 @@ public class ExcelUtil<T>
858 859
         if (isSubList())
859 860
         {
860 861
             // 填充默认样式,防止合并单元格样式失效
861
-            sheet.setDefaultColumnStyle(column, styles.get(StringUtils.format("data_{}_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor(), attr.cellType())));
862
+            sheet.setDefaultColumnStyle(column, styles.get(StringUtils.format("data_{}_{}_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor(), attr.cellType(), attr.wrapText())));
862 863
             if (attr.needMerge())
863 864
             {
864 865
                 sheet.addMergedRegion(new CellRangeAddress(rownum - 1, rownum, column, column));
@@ -990,7 +991,7 @@ public class ExcelUtil<T>
990 991
                         sheet.addMergedRegion(new CellRangeAddress(subMergedFirstRowNum, subMergedLastRowNum, column, column));
991 992
                     }
992 993
                 }
993
-                cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor(), attr.cellType())));
994
+                cell.setCellStyle(styles.get(StringUtils.format("data_{}_{}_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor(), attr.cellType(), attr.wrapText())));
994 995
 
995 996
                 // 用于读取对象中的属性
996 997
                 Object value = getTargetValue(vo, field, attr);