作者 RuoYi

支持自定义显示Excel属性列

@@ -195,6 +195,11 @@ public class ExcelUtil<T> @@ -195,6 +195,11 @@ public class ExcelUtil<T>
195 public Class<T> clazz; 195 public Class<T> clazz;
196 196
197 /** 197 /**
  198 + * 需要显示列属性
  199 + */
  200 + public String[] includeFields;
  201 +
  202 + /**
198 * 需要排除列属性 203 * 需要排除列属性
199 */ 204 */
200 public String[] excludeFields; 205 public String[] excludeFields;
@@ -205,10 +210,19 @@ public class ExcelUtil<T> @@ -205,10 +210,19 @@ public class ExcelUtil<T>
205 } 210 }
206 211
207 /** 212 /**
  213 + * 仅在Excel中显示列属性
  214 + *
  215 + * @param fields 列属性名 示例[单个"name"/多个"id","name"]
  216 + */
  217 + public void showColumn(String... fields)
  218 + {
  219 + this.includeFields = fields;
  220 + }
  221 +
  222 + /**
208 * 隐藏Excel中列属性 223 * 隐藏Excel中列属性
209 * 224 *
210 * @param fields 列属性名 示例[单个"name"/多个"id","name"] 225 * @param fields 列属性名 示例[单个"name"/多个"id","name"]
211 - * @throws Exception  
212 */ 226 */
213 public void hideColumn(String... fields) 227 public void hideColumn(String... fields)
214 { 228 {
@@ -1488,46 +1502,86 @@ public class ExcelUtil<T> @@ -1488,46 +1502,86 @@ public class ExcelUtil<T>
1488 List<Field> tempFields = new ArrayList<>(); 1502 List<Field> tempFields = new ArrayList<>();
1489 tempFields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields())); 1503 tempFields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields()));
1490 tempFields.addAll(Arrays.asList(clazz.getDeclaredFields())); 1504 tempFields.addAll(Arrays.asList(clazz.getDeclaredFields()));
1491 - for (Field field : tempFields) 1505 + if (StringUtils.isNotEmpty(includeFields))
1492 { 1506 {
1493 - if (!ArrayUtils.contains(this.excludeFields, field.getName())) 1507 + for (Field field : tempFields)
1494 { 1508 {
1495 - // 单注解  
1496 - if (field.isAnnotationPresent(Excel.class)) 1509 + if (ArrayUtils.contains(this.includeFields, field.getName()) || field.isAnnotationPresent(Excels.class))
1497 { 1510 {
1498 - Excel attr = field.getAnnotation(Excel.class);  
1499 - if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) 1511 + addField(fields, field);
  1512 + }
  1513 + }
  1514 + }
  1515 + else if (StringUtils.isNotEmpty(excludeFields))
  1516 + {
  1517 + for (Field field : tempFields)
  1518 + {
  1519 + if (!ArrayUtils.contains(this.excludeFields, field.getName()))
  1520 + {
  1521 + addField(fields, field);
  1522 + }
  1523 + }
  1524 + }
  1525 + else
  1526 + {
  1527 + for (Field field : tempFields)
  1528 + {
  1529 + addField(fields, field);
  1530 + }
  1531 + }
  1532 + return fields;
  1533 + }
  1534 +
  1535 + /**
  1536 + * 添加字段信息
  1537 + */
  1538 + public void addField(List<Object[]> fields, Field field)
  1539 + {
  1540 + // 单注解
  1541 + if (field.isAnnotationPresent(Excel.class))
  1542 + {
  1543 + Excel attr = field.getAnnotation(Excel.class);
  1544 + if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
  1545 + {
  1546 + field.setAccessible(true);
  1547 + fields.add(new Object[] { field, attr });
  1548 + }
  1549 + if (Collection.class.isAssignableFrom(field.getType()))
  1550 + {
  1551 + subMethod = getSubMethod(field.getName(), clazz);
  1552 + ParameterizedType pt = (ParameterizedType) field.getGenericType();
  1553 + Class<?> subClass = (Class<?>) pt.getActualTypeArguments()[0];
  1554 + this.subFields = FieldUtils.getFieldsListWithAnnotation(subClass, Excel.class);
  1555 + }
  1556 + }
  1557 +
  1558 + // 多注解
  1559 + if (field.isAnnotationPresent(Excels.class))
  1560 + {
  1561 + Excels attrs = field.getAnnotation(Excels.class);
  1562 + Excel[] excels = attrs.value();
  1563 + for (Excel attr : excels)
  1564 + {
  1565 + if (StringUtils.isNotEmpty(includeFields))
  1566 + {
  1567 + if (ArrayUtils.contains(this.includeFields, field.getName() + "." + attr.targetAttr())
  1568 + && (attr != null && (attr.type() == Type.ALL || attr.type() == type)))
1500 { 1569 {
1501 field.setAccessible(true); 1570 field.setAccessible(true);
1502 fields.add(new Object[] { field, attr }); 1571 fields.add(new Object[] { field, attr });
1503 } 1572 }
1504 - if (Collection.class.isAssignableFrom(field.getType()))  
1505 - {  
1506 - subMethod = getSubMethod(field.getName(), clazz);  
1507 - ParameterizedType pt = (ParameterizedType) field.getGenericType();  
1508 - Class<?> subClass = (Class<?>) pt.getActualTypeArguments()[0];  
1509 - this.subFields = FieldUtils.getFieldsListWithAnnotation(subClass, Excel.class);  
1510 - }  
1511 } 1573 }
1512 -  
1513 - // 多注解  
1514 - if (field.isAnnotationPresent(Excels.class)) 1574 + else
1515 { 1575 {
1516 - Excels attrs = field.getAnnotation(Excels.class);  
1517 - Excel[] excels = attrs.value();  
1518 - for (Excel attr : excels) 1576 + if (!ArrayUtils.contains(this.excludeFields, field.getName() + "." + attr.targetAttr())
  1577 + && (attr != null && (attr.type() == Type.ALL || attr.type() == type)))
1519 { 1578 {
1520 - if (!ArrayUtils.contains(this.excludeFields, field.getName() + "." + attr.targetAttr())  
1521 - && (attr != null && (attr.type() == Type.ALL || attr.type() == type)))  
1522 - {  
1523 - field.setAccessible(true);  
1524 - fields.add(new Object[] { field, attr });  
1525 - } 1579 + field.setAccessible(true);
  1580 + fields.add(new Object[] { field, attr });
1526 } 1581 }
1527 } 1582 }
1528 } 1583 }
1529 } 1584 }
1530 - return fields;  
1531 } 1585 }
1532 1586
1533 /** 1587 /**