作者 RuoYi

支持自定义隐藏Excel属性列

@@ -22,6 +22,7 @@ import java.util.Set; @@ -22,6 +22,7 @@ import java.util.Set;
22 import java.util.UUID; 22 import java.util.UUID;
23 import java.util.stream.Collectors; 23 import java.util.stream.Collectors;
24 import javax.servlet.http.HttpServletResponse; 24 import javax.servlet.http.HttpServletResponse;
  25 +import org.apache.commons.lang3.ArrayUtils;
25 import org.apache.commons.lang3.RegExUtils; 26 import org.apache.commons.lang3.RegExUtils;
26 import org.apache.poi.hssf.usermodel.HSSFClientAnchor; 27 import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
27 import org.apache.poi.hssf.usermodel.HSSFPicture; 28 import org.apache.poi.hssf.usermodel.HSSFPicture;
@@ -163,11 +164,27 @@ public class ExcelUtil<T> @@ -163,11 +164,27 @@ public class ExcelUtil<T>
163 */ 164 */
164 public Class<T> clazz; 165 public Class<T> clazz;
165 166
  167 + /**
  168 + * 需要排除列属性
  169 + */
  170 + public String[] excludeFields;
  171 +
166 public ExcelUtil(Class<T> clazz) 172 public ExcelUtil(Class<T> clazz)
167 { 173 {
168 this.clazz = clazz; 174 this.clazz = clazz;
169 } 175 }
170 176
  177 + /**
  178 + * 隐藏Excel中列属性
  179 + *
  180 + * @param fields 列属性名 示例[单个"name"/多个"id","name"]
  181 + * @throws Exception
  182 + */
  183 + public void hideColumn(String... fields)
  184 + {
  185 + this.excludeFields = fields;
  186 + }
  187 +
171 public void init(List<T> list, String sheetName, String title, Type type) 188 public void init(List<T> list, String sheetName, String title, Type type)
172 { 189 {
173 if (list == null) 190 if (list == null)
@@ -1202,30 +1219,33 @@ public class ExcelUtil<T> @@ -1202,30 +1219,33 @@ public class ExcelUtil<T>
1202 tempFields.addAll(Arrays.asList(clazz.getDeclaredFields())); 1219 tempFields.addAll(Arrays.asList(clazz.getDeclaredFields()));
1203 for (Field field : tempFields) 1220 for (Field field : tempFields)
1204 { 1221 {
1205 - // 单注解  
1206 - if (field.isAnnotationPresent(Excel.class))  
1207 - {  
1208 - Excel attr = field.getAnnotation(Excel.class);  
1209 - if (attr != null && (attr.type() == Type.ALL || attr.type() == type))  
1210 - {  
1211 - field.setAccessible(true);  
1212 - fields.add(new Object[] { field, attr });  
1213 - }  
1214 - }  
1215 -  
1216 - // 多注解  
1217 - if (field.isAnnotationPresent(Excels.class)) 1222 + if (!ArrayUtils.contains(this.excludeFields, field.getName()))
1218 { 1223 {
1219 - Excels attrs = field.getAnnotation(Excels.class);  
1220 - Excel[] excels = attrs.value();  
1221 - for (Excel attr : excels) 1224 + // 单注解
  1225 + if (field.isAnnotationPresent(Excel.class))
1222 { 1226 {
  1227 + Excel attr = field.getAnnotation(Excel.class);
1223 if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) 1228 if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
1224 { 1229 {
1225 field.setAccessible(true); 1230 field.setAccessible(true);
1226 fields.add(new Object[] { field, attr }); 1231 fields.add(new Object[] { field, attr });
1227 } 1232 }
1228 } 1233 }
  1234 +
  1235 + // 多注解
  1236 + if (field.isAnnotationPresent(Excels.class))
  1237 + {
  1238 + Excels attrs = field.getAnnotation(Excels.class);
  1239 + Excel[] excels = attrs.value();
  1240 + for (Excel attr : excels)
  1241 + {
  1242 + if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
  1243 + {
  1244 + field.setAccessible(true);
  1245 + fields.add(new Object[] { field, attr });
  1246 + }
  1247 + }
  1248 + }
1229 } 1249 }
1230 } 1250 }
1231 return fields; 1251 return fields;