Parcourir la source

支持自定义隐藏Excel属性列

RuoYi il y a 3 ans
Parent
commit
fb35360f81

+ 36 - 16
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java

@@ -18,6 +18,7 @@ import java.util.Map;
18 18
 import java.util.Set;
19 19
 import java.util.stream.Collectors;
20 20
 import javax.servlet.http.HttpServletResponse;
21
+import org.apache.commons.lang3.ArrayUtils;
21 22
 import org.apache.commons.lang3.RegExUtils;
22 23
 import org.apache.poi.ss.usermodel.BorderStyle;
23 24
 import org.apache.poi.ss.usermodel.Cell;
@@ -140,11 +141,27 @@ public class ExcelUtil<T>
140 141
      */
141 142
     public Class<T> clazz;
142 143
 
144
+    /**
145
+     * 需要排除列属性
146
+     */
147
+    public String[] excludeFields;
148
+
143 149
     public ExcelUtil(Class<T> clazz)
144 150
     {
145 151
         this.clazz = clazz;
146 152
     }
147 153
 
154
+    /**
155
+     * 隐藏Excel中列属性
156
+     *
157
+     * @param fields 列属性名 示例[单个"name"/多个"id","name"]
158
+     * @throws Exception
159
+     */
160
+    public void hideColumn(String... fields)
161
+    {
162
+        this.excludeFields = fields;
163
+    }
164
+
148 165
     public void init(List<T> list, String sheetName, String title, Type type)
149 166
     {
150 167
         if (list == null)
@@ -1021,30 +1038,33 @@ public class ExcelUtil<T>
1021 1038
         tempFields.addAll(Arrays.asList(clazz.getDeclaredFields()));
1022 1039
         for (Field field : tempFields)
1023 1040
         {
1024
-            // 单注解
1025
-            if (field.isAnnotationPresent(Excel.class))
1026
-            {
1027
-                Excel attr = field.getAnnotation(Excel.class);
1028
-                if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
1029
-                {
1030
-                    field.setAccessible(true);
1031
-                    fields.add(new Object[] { field, attr });
1032
-                }
1033
-            }
1034
-
1035
-            // 多注解
1036
-            if (field.isAnnotationPresent(Excels.class))
1041
+            if (!ArrayUtils.contains(this.excludeFields, field.getName()))
1037 1042
             {
1038
-                Excels attrs = field.getAnnotation(Excels.class);
1039
-                Excel[] excels = attrs.value();
1040
-                for (Excel attr : excels)
1043
+                // 单注解
1044
+                if (field.isAnnotationPresent(Excel.class))
1041 1045
                 {
1046
+                    Excel attr = field.getAnnotation(Excel.class);
1042 1047
                     if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
1043 1048
                     {
1044 1049
                         field.setAccessible(true);
1045 1050
                         fields.add(new Object[] { field, attr });
1046 1051
                     }
1047 1052
                 }
1053
+
1054
+                // 多注解
1055
+                if (field.isAnnotationPresent(Excels.class))
1056
+                {
1057
+                    Excels attrs = field.getAnnotation(Excels.class);
1058
+                    Excel[] excels = attrs.value();
1059
+                    for (Excel attr : excels)
1060
+                    {
1061
+                        if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
1062
+                        {
1063
+                            field.setAccessible(true);
1064
+                            fields.add(new Object[] { field, attr });
1065
+                        }
1066
+                    }
1067
+                }
1048 1068
             }
1049 1069
         }
1050 1070
         return fields;