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
3b42abef
Commit
3b42abef
authored
Sep 22, 2021
by
RuoYi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Excel注解支持自定义数据处理器
parent
26f0737c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
1 deletion
+62
-1
Excel.java
...mmon/src/main/java/com/ruoyi/common/annotation/Excel.java
+12
-1
ExcelHandlerAdapter.java
.../java/com/ruoyi/common/utils/poi/ExcelHandlerAdapter.java
+19
-0
ExcelUtil.java
...n/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
+31
-0
No files found.
ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java
View file @
3b42abef
...
...
@@ -5,6 +5,7 @@ import java.lang.annotation.Retention;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
import
java.math.BigDecimal
;
import
com.ruoyi.common.utils.poi.ExcelHandlerAdapter
;
/**
* 自定义导出Excel数据注解
...
...
@@ -108,7 +109,17 @@ public @interface Excel
/**
* 导出字段对齐方式(0:默认;1:靠左;2:居中;3:靠右)
*/
Align
align
()
default
Align
.
AUTO
;
public
Align
align
()
default
Align
.
AUTO
;
/**
* 自定义数据处理器
*/
public
Class
<?>
handler
()
default
ExcelHandlerAdapter
.
class
;
/**
* 自定义数据处理器参数
*/
public
String
[]
args
()
default
{};
public
enum
Align
{
...
...
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelHandlerAdapter.java
0 → 100644
View file @
3b42abef
package
com
.
ruoyi
.
common
.
utils
.
poi
;
/**
* Excel数据格式处理适配器
*
* @author ruoyi
*/
public
interface
ExcelHandlerAdapter
{
/**
* 格式化
*
* @param value 单元格数据值
* @param args excel注解args参数组
*
* @return 处理后的值
*/
Object
format
(
Object
value
,
String
[]
args
);
}
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
View file @
3b42abef
...
...
@@ -6,6 +6,7 @@ import java.io.IOException;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Method
;
import
java.math.BigDecimal
;
import
java.text.DecimalFormat
;
import
java.util.ArrayList
;
...
...
@@ -333,6 +334,10 @@ public class ExcelUtil<T>
{
val
=
reverseDictByExp
(
Convert
.
toStr
(
val
),
attr
.
dictType
(),
attr
.
separator
());
}
else
if
(!
attr
.
handler
().
equals
(
ExcelHandlerAdapter
.
class
))
{
val
=
dataFormatHandlerAdapter
(
val
,
attr
);
}
else
if
(
ColumnType
.
IMAGE
==
attr
.
cellType
()
&&
StringUtils
.
isNotEmpty
(
pictures
))
{
PictureData
image
=
pictures
.
get
(
row
.
getRowNum
()
+
"_"
+
entry
.
getKey
());
...
...
@@ -729,6 +734,10 @@ public class ExcelUtil<T>
{
cell
.
setCellValue
((((
BigDecimal
)
value
).
setScale
(
attr
.
scale
(),
attr
.
roundingMode
())).
toString
());
}
else
if
(!
attr
.
handler
().
equals
(
ExcelHandlerAdapter
.
class
))
{
cell
.
setCellValue
(
dataFormatHandlerAdapter
(
value
,
attr
));
}
else
{
// 设置列类型
...
...
@@ -901,6 +910,28 @@ public class ExcelUtil<T>
return
DictUtils
.
getDictValue
(
dictType
,
dictLabel
,
separator
);
}
/**
* 数据处理器
*
* @param value 数据值
* @param excel 数据注解
* @return
*/
public
String
dataFormatHandlerAdapter
(
Object
value
,
Excel
excel
)
{
try
{
Object
instance
=
excel
.
handler
().
newInstance
();
Method
formatMethod
=
excel
.
handler
().
getMethod
(
"format"
,
new
Class
[]
{
Object
.
class
,
String
[].
class
});
value
=
formatMethod
.
invoke
(
instance
,
value
,
excel
.
args
());
}
catch
(
Exception
e
)
{
log
.
error
(
"不能格式化数据 "
+
excel
.
handler
(),
e
.
getMessage
());
}
return
Convert
.
toStr
(
value
);
}
/**
* 合计统计信息
*/
...
...
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