Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
T
topology
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
姜立玮
topology
Commits
bf771ae5
提交
bf771ae5
authored
8月 26, 2020
作者:
RuoYi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Excel注解支持自动统计数据总和
上级
fa5596bb
变更
2
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
74 行增加
和
0 行删除
+74
-0
Excel.java
...mmon/src/main/java/com/ruoyi/common/annotation/Excel.java
+5
-0
ExcelUtil.java
...n/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
+69
-0
没有找到文件。
ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java
浏览文件 @
bf771ae5
...
@@ -100,6 +100,11 @@ public @interface Excel
...
@@ -100,6 +100,11 @@ public @interface Excel
*/
*/
public
String
targetAttr
()
default
""
;
public
String
targetAttr
()
default
""
;
/**
* 是否自动统计数据,在最后追加一行统计数据总和
*/
public
boolean
isStatistics
()
default
false
;
/**
/**
* 字段类型(0:导出导入;1:仅导出;2:仅导入)
* 字段类型(0:导出导入;1:仅导出;2:仅导入)
*/
*/
...
...
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
浏览文件 @
bf771ae5
...
@@ -16,6 +16,7 @@ import java.util.Date;
...
@@ -16,6 +16,7 @@ import java.util.Date;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.UUID
;
import
java.util.UUID
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
import
org.apache.poi.hssf.usermodel.HSSFDateUtil
;
import
org.apache.poi.hssf.usermodel.HSSFDateUtil
;
...
@@ -103,6 +104,16 @@ public class ExcelUtil<T>
...
@@ -103,6 +104,16 @@ public class ExcelUtil<T>
*/
*/
private
List
<
Object
[]>
fields
;
private
List
<
Object
[]>
fields
;
/**
* 统计列表
*/
private
Map
<
Integer
,
Double
>
statistics
=
new
HashMap
<
Integer
,
Double
>();
/**
* 数字格式
*/
private
static
final
DecimalFormat
DOUBLE_FORMAT
=
new
DecimalFormat
(
"######0.00"
);
/**
/**
* 实体对象
* 实体对象
*/
*/
...
@@ -342,6 +353,7 @@ public class ExcelUtil<T>
...
@@ -342,6 +353,7 @@ public class ExcelUtil<T>
if
(
Type
.
EXPORT
.
equals
(
type
))
if
(
Type
.
EXPORT
.
equals
(
type
))
{
{
fillExcelData
(
index
,
row
);
fillExcelData
(
index
,
row
);
addStatisticsRow
();
}
}
}
}
String
filename
=
encodingFilename
(
sheetName
);
String
filename
=
encodingFilename
(
sheetName
);
...
@@ -448,6 +460,15 @@ public class ExcelUtil<T>
...
@@ -448,6 +460,15 @@ public class ExcelUtil<T>
headerFont
.
setColor
(
IndexedColors
.
WHITE
.
getIndex
());
headerFont
.
setColor
(
IndexedColors
.
WHITE
.
getIndex
());
style
.
setFont
(
headerFont
);
style
.
setFont
(
headerFont
);
styles
.
put
(
"header"
,
style
);
styles
.
put
(
"header"
,
style
);
style
=
wb
.
createCellStyle
();
style
.
setAlignment
(
HorizontalAlignment
.
CENTER
);
style
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
Font
totalFont
=
wb
.
createFont
();
totalFont
.
setFontName
(
"Arial"
);
totalFont
.
setFontHeightInPoints
((
short
)
10
);
style
.
setFont
(
totalFont
);
styles
.
put
(
"total"
,
style
);
return
styles
;
return
styles
;
}
}
...
@@ -560,6 +581,7 @@ public class ExcelUtil<T>
...
@@ -560,6 +581,7 @@ public class ExcelUtil<T>
// 设置列类型
// 设置列类型
setCellVo
(
value
,
attr
,
cell
);
setCellVo
(
value
,
attr
,
cell
);
}
}
addStatisticsData
(
column
,
Convert
.
toStr
(
value
),
attr
);
}
}
}
}
catch
(
Exception
e
)
catch
(
Exception
e
)
...
@@ -726,6 +748,53 @@ public class ExcelUtil<T>
...
@@ -726,6 +748,53 @@ public class ExcelUtil<T>
return
DictUtils
.
getDictValue
(
dictType
,
dictLabel
,
separator
);
return
DictUtils
.
getDictValue
(
dictType
,
dictLabel
,
separator
);
}
}
/**
* 合计统计信息
*/
private
void
addStatisticsData
(
Integer
index
,
String
text
,
Excel
entity
)
{
if
(
entity
!=
null
&&
entity
.
isStatistics
())
{
Double
temp
=
0
D
;
if
(!
statistics
.
containsKey
(
index
))
{
statistics
.
put
(
index
,
temp
);
}
try
{
temp
=
Double
.
valueOf
(
text
);
}
catch
(
NumberFormatException
e
)
{
}
statistics
.
put
(
index
,
statistics
.
get
(
index
)
+
temp
);
}
}
/**
* 创建统计行
*/
public
void
addStatisticsRow
()
{
if
(
statistics
.
size
()
>
0
)
{
Cell
cell
=
null
;
Row
row
=
sheet
.
createRow
(
sheet
.
getLastRowNum
()
+
1
);
Set
<
Integer
>
keys
=
statistics
.
keySet
();
cell
=
row
.
createCell
(
0
);
cell
.
setCellStyle
(
styles
.
get
(
"total"
));
cell
.
setCellValue
(
"合计"
);
for
(
Integer
key
:
keys
)
{
cell
=
row
.
createCell
(
key
);
cell
.
setCellStyle
(
styles
.
get
(
"total"
));
cell
.
setCellValue
(
DOUBLE_FORMAT
.
format
(
statistics
.
get
(
key
)));
}
statistics
.
clear
();
}
}
/**
/**
* 编码文件名
* 编码文件名
*/
*/
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论