@ -167,6 +167,11 @@ public class ExcelUtil<T>
* /
public Class < T > clazz ;
/ * *
* 需 要 显 示 列 属 性
* /
public String [ ] includeFields ;
/ * *
* 需 要 排 除 列 属 性
* /
@ -177,11 +182,20 @@ public class ExcelUtil<T>
this . clazz = clazz ;
}
/ * *
* 仅 在 Excel 中 显 示 列 属 性
*
* @param fields 列 属 性 名 示 例 [ 单 个 "name" / 多 个 "id" , "name" ]
* /
public void showColumn ( String . . . fields )
{
this . includeFields = fields ;
}
/ * *
* 隐 藏 Excel 中 列 属 性
*
* @param fields 列 属 性 名 示 例 [ 单 个 "name" / 多 个 "id" , "name" ]
* @throws Exception
* /
public void hideColumn ( String . . . fields )
{
@ -1280,46 +1294,86 @@ public class ExcelUtil<T>
List < Field > tempFields = new ArrayList < > ( ) ;
tempFields . addAll ( Arrays . asList ( clazz . getSuperclass ( ) . getDeclaredFields ( ) ) ) ;
tempFields . addAll ( Arrays . asList ( clazz . getDeclaredFields ( ) ) ) ;
for ( Field field : tempFields )
if ( StringUtils . isNotEmpty ( includeFields ) )
{
if ( ! ArrayUtils . contains ( this . excludeFields , field . getName ( ) ) )
for ( Field field : tempFields )
{
// 单注解
if ( field . isAnnotationPresent ( Excel . class ) )
if ( ArrayUtils . contains ( this . includeFields , field . getName ( ) ) | | field . isAnnotationPresent ( Excels . class ) )
{
Excel attr = field . getAnnotation ( Excel . class ) ;
if ( attr ! = null & & ( attr . type ( ) = = Type . ALL | | attr . type ( ) = = type ) )
addField ( fields , field ) ;
}
}
}
else if ( StringUtils . isNotEmpty ( excludeFields ) )
{
for ( Field field : tempFields )
{
if ( ! ArrayUtils . contains ( this . excludeFields , field . getName ( ) ) )
{
addField ( fields , field ) ;
}
}
}
else
{
for ( Field field : tempFields )
{
addField ( fields , field ) ;
}
}
return fields ;
}
/ * *
* 添 加 字 段 信 息
* /
public void addField ( List < Object [ ] > fields , Field field )
{
// 单注解
if ( field . isAnnotationPresent ( Excel . class ) )
{
Excel attr = field . getAnnotation ( Excel . class ) ;
if ( attr ! = null & & ( attr . type ( ) = = Type . ALL | | attr . type ( ) = = type ) )
{
field . setAccessible ( true ) ;
fields . add ( new Object [ ] { field , attr } ) ;
}
if ( Collection . class . isAssignableFrom ( field . getType ( ) ) )
{
subMethod = getSubMethod ( field . getName ( ) , clazz ) ;
ParameterizedType pt = ( ParameterizedType ) field . getGenericType ( ) ;
Class < ? > subClass = ( Class < ? > ) pt . getActualTypeArguments ( ) [ 0 ] ;
this . subFields = FieldUtils . getFieldsListWithAnnotation ( subClass , Excel . class ) ;
}
}
// 多注解
if ( field . isAnnotationPresent ( Excels . class ) )
{
Excels attrs = field . getAnnotation ( Excels . class ) ;
Excel [ ] excels = attrs . value ( ) ;
for ( Excel attr : excels )
{
if ( StringUtils . isNotEmpty ( includeFields ) )
{
if ( ArrayUtils . contains ( this . includeFields , field . getName ( ) + "." + attr . targetAttr ( ) )
& & ( attr ! = null & & ( attr . type ( ) = = Type . ALL | | attr . type ( ) = = type ) ) )
{
field . setAccessible ( true ) ;
fields . add ( new Object [ ] { field , attr } ) ;
}
if ( Collection . class . isAssignableFrom ( field . getType ( ) ) )
{
subMethod = getSubMethod ( field . getName ( ) , clazz ) ;
ParameterizedType pt = ( ParameterizedType ) field . getGenericType ( ) ;
Class < ? > subClass = ( Class < ? > ) pt . getActualTypeArguments ( ) [ 0 ] ;
this . subFields = FieldUtils . getFieldsListWithAnnotation ( subClass , Excel . class ) ;
}
}
// 多注解
if ( field . isAnnotationPresent ( Excels . class ) )
else
{
Excels attrs = field . getAnnotation ( Excels . class ) ;
Excel [ ] excels = attrs . value ( ) ;
for ( Excel attr : excels )
if ( ! ArrayUtils . contains ( this . excludeFields , field . getName ( ) + "." + attr . targetAttr ( ) )
& & ( attr ! = null & & ( attr . type ( ) = = Type . ALL | | attr . type ( ) = = type ) ) )
{
if ( ! ArrayUtils . contains ( this . excludeFields , field . getName ( ) + "." + attr . targetAttr ( ) )
& & ( attr ! = null & & ( attr . type ( ) = = Type . ALL | | attr . type ( ) = = type ) ) )
{
field . setAccessible ( true ) ;
fields . add ( new Object [ ] { field , attr } ) ;
}
field . setAccessible ( true ) ;
fields . add ( new Object [ ] { field , attr } ) ;
}
}
}
}
return fields ;
}
/ * *