Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fgqyxxlr
Overview
Overview
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
yaru
fgqyxxlr
Commits
3d301780
Commit
3d301780
authored
Dec 28, 2018
by
RuoYi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Excel注解支持多级获取
parent
63ee2ba2
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
66 additions
and
4 deletions
+66
-4
ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java
+6
-0
ruoyi-common/src/main/java/com/ruoyi/common/utils/ExcelUtil.java
+59
-4
ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUser.java
+1
-0
No files found.
ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java
View file @
3d301780
...
@@ -63,4 +63,9 @@ public @interface Excel
...
@@ -63,4 +63,9 @@ public @interface Excel
* 是否导出数据,应对需求:有时我们需要导出一份模板,这是标题需要但内容需要用户手工填写.
* 是否导出数据,应对需求:有时我们需要导出一份模板,这是标题需要但内容需要用户手工填写.
*/
*/
public
boolean
isExport
()
default
true
;
public
boolean
isExport
()
default
true
;
/**
* 另一个类中的属性名称,支持多级获取,以小数点隔开
*/
public
String
targetAttr
()
default
""
;
}
}
\ No newline at end of file
ruoyi-common/src/main/java/com/ruoyi/common/utils/ExcelUtil.java
View file @
3d301780
...
@@ -6,6 +6,7 @@ import java.io.IOException;
...
@@ -6,6 +6,7 @@ import java.io.IOException;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.io.OutputStream
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Method
;
import
java.text.SimpleDateFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.Date
;
...
@@ -344,21 +345,23 @@ public class ExcelUtil<T>
...
@@ -344,21 +345,23 @@ public class ExcelUtil<T>
continue
;
continue
;
}
}
// 用于读取对象中的属性
Object
value
=
getTargetValue
(
vo
,
field
,
attr
);
String
dateFormat
=
attr
.
dateFormat
();
String
dateFormat
=
attr
.
dateFormat
();
String
readConverterExp
=
attr
.
readConverterExp
();
String
readConverterExp
=
attr
.
readConverterExp
();
if
(
StringUtils
.
isNotEmpty
(
dateFormat
))
if
(
StringUtils
.
isNotEmpty
(
dateFormat
))
{
{
cell
.
setCellValue
(
DateUtils
.
parseDateToStr
(
dateFormat
,
(
Date
)
field
.
get
(
vo
)
));
cell
.
setCellValue
(
DateUtils
.
parseDateToStr
(
dateFormat
,
(
Date
)
value
));
}
}
else
if
(
StringUtils
.
isNotEmpty
(
readConverterExp
))
else
if
(
StringUtils
.
isNotEmpty
(
readConverterExp
))
{
{
cell
.
setCellValue
(
convertByExp
(
String
.
valueOf
(
field
.
get
(
vo
)
),
readConverterExp
));
cell
.
setCellValue
(
convertByExp
(
String
.
valueOf
(
value
),
readConverterExp
));
}
}
else
else
{
{
cell
.
setCellType
(
CellType
.
STRING
);
cell
.
setCellType
(
CellType
.
STRING
);
// 如果数据存在就填入,不存在填入空格.
// 如果数据存在就填入,不存在填入空格.
cell
.
setCellValue
(
StringUtils
.
isNull
(
field
.
get
(
vo
))
?
attr
.
defaultValue
()
:
field
.
get
(
vo
)
+
attr
.
suffix
());
cell
.
setCellValue
(
StringUtils
.
isNull
(
value
)
?
attr
.
defaultValue
()
:
value
+
attr
.
suffix
());
}
}
}
}
}
}
...
@@ -455,7 +458,7 @@ public class ExcelUtil<T>
...
@@ -455,7 +458,7 @@ public class ExcelUtil<T>
sheet
.
addValidationData
(
dataValidationList
);
sheet
.
addValidationData
(
dataValidationList
);
return
sheet
;
return
sheet
;
}
}
/**
/**
* 解析导出值 0=男,1=女,2=未知
* 解析导出值 0=男,1=女,2=未知
*
*
...
@@ -509,4 +512,55 @@ public class ExcelUtil<T>
...
@@ -509,4 +512,55 @@ public class ExcelUtil<T>
}
}
return
downloadPath
;
return
downloadPath
;
}
}
/**
* 获取bean中的属性值
*
* @param vo 实体对象
* @param field 字段
* @param excel 注解
* @return 最终的属性值
* @throws Exception
*/
private
Object
getTargetValue
(
T
vo
,
Field
field
,
Excel
excel
)
throws
Exception
{
Object
o
=
field
.
get
(
vo
);
if
(
StringUtils
.
isNotEmpty
(
excel
.
targetAttr
()))
{
String
target
=
excel
.
targetAttr
();
if
(
target
.
indexOf
(
"."
)
>
-
1
)
{
String
[]
targets
=
target
.
split
(
"[.]"
);
for
(
String
name
:
targets
)
{
o
=
getValue
(
o
,
name
);
}
}
else
{
o
=
getValue
(
o
,
target
);
}
}
return
o
;
}
/**
* 以类的属性的get方法方法形式获取值
*
* @param o
* @param name
* @return value
* @throws Exception
*/
private
Object
getValue
(
Object
o
,
String
name
)
throws
Exception
{
if
(
StringUtils
.
isNotEmpty
(
name
))
{
Class
<?>
clazz
=
o
.
getClass
();
String
methodName
=
"get"
+
name
.
substring
(
0
,
1
).
toUpperCase
()
+
name
.
substring
(
1
);
Method
method
=
clazz
.
getMethod
(
methodName
);
o
=
method
.
invoke
(
o
);
}
return
o
;
}
}
}
\ No newline at end of file
ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUser.java
View file @
3d301780
...
@@ -71,6 +71,7 @@ public class SysUser extends BaseEntity
...
@@ -71,6 +71,7 @@ public class SysUser extends BaseEntity
private
Date
loginDate
;
private
Date
loginDate
;
/** 部门对象 */
/** 部门对象 */
@Excel
(
name
=
"部门名称"
,
targetAttr
=
"deptName"
)
private
SysDept
dept
;
private
SysDept
dept
;
private
List
<
SysRole
>
roles
;
private
List
<
SysRole
>
roles
;
...
...
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