Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
S
sts网站
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
liyang
sts网站
Commits
e4ccbc66
Commit
e4ccbc66
authored
Nov 07, 2024
by
RuoYi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
支持自定义显示Excel属性列
parent
430e6d4d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
81 additions
and
27 deletions
+81
-27
ExcelUtil.java
...n/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
+81
-27
No files found.
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
View file @
e4ccbc66
...
...
@@ -194,6 +194,11 @@ public class ExcelUtil<T>
*/
public
Class
<
T
>
clazz
;
/**
* 需要显示列属性
*/
public
String
[]
includeFields
;
/**
* 需要排除列属性
*/
...
...
@@ -204,11 +209,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
)
{
...
...
@@ -1488,46 +1502,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
;
}
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment