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
1f0e7427
Commit
1f0e7427
authored
Jul 20, 2022
by
RuoYi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Excel注解支持backgroundColor属性设置背景色
parent
28b9fbb4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
21 deletions
+60
-21
Excel.java
...mmon/src/main/java/com/ruoyi/common/annotation/Excel.java
+16
-1
ExcelUtil.java
...n/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
+44
-20
No files found.
ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java
View file @
1f0e7427
...
...
@@ -109,7 +109,22 @@ public @interface Excel
public
ColumnType
cellType
()
default
ColumnType
.
STRING
;
/**
* 导出字体颜色
* 导出列头背景色
*/
public
IndexedColors
headerBackgroundColor
()
default
IndexedColors
.
GREY_50_PERCENT
;
/**
* 导出列头字体颜色
*/
public
IndexedColors
headerColor
()
default
IndexedColors
.
WHITE
;
/**
* 导出单元格背景色
*/
public
IndexedColors
backgroundColor
()
default
IndexedColors
.
WHITE
;
/**
* 导出单元格字体颜色
*/
public
IndexedColors
color
()
default
IndexedColors
.
BLACK
;
...
...
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
View file @
1f0e7427
...
...
@@ -649,20 +649,6 @@ public class ExcelUtil<T>
style
.
setFont
(
dataFont
);
styles
.
put
(
"data"
,
style
);
style
=
wb
.
createCellStyle
();
style
.
cloneStyleFrom
(
styles
.
get
(
"data"
));
style
.
setAlignment
(
HorizontalAlignment
.
CENTER
);
style
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
style
.
setFillForegroundColor
(
IndexedColors
.
GREY_50_PERCENT
.
getIndex
());
style
.
setFillPattern
(
FillPatternType
.
SOLID_FOREGROUND
);
Font
headerFont
=
wb
.
createFont
();
headerFont
.
setFontName
(
"Arial"
);
headerFont
.
setFontHeightInPoints
((
short
)
10
);
headerFont
.
setBold
(
true
);
headerFont
.
setColor
(
IndexedColors
.
WHITE
.
getIndex
());
style
.
setFont
(
headerFont
);
styles
.
put
(
"header"
,
style
);
style
=
wb
.
createCellStyle
();
style
.
setAlignment
(
HorizontalAlignment
.
CENTER
);
style
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
...
...
@@ -672,24 +658,60 @@ public class ExcelUtil<T>
style
.
setFont
(
totalFont
);
styles
.
put
(
"total"
,
style
);
styles
.
putAll
(
annotationStyles
(
wb
));
styles
.
putAll
(
annotationHeaderStyles
(
wb
,
styles
));
styles
.
putAll
(
annotationDataStyles
(
wb
));
return
styles
;
}
/**
* 根据Excel注解创建表格样式
* 根据Excel注解创建表格头样式
*
* @param wb 工作薄对象
* @return 自定义样式列表
*/
private
Map
<
String
,
CellStyle
>
annotationHeaderStyles
(
Workbook
wb
,
Map
<
String
,
CellStyle
>
styles
)
{
Map
<
String
,
CellStyle
>
headerStyles
=
new
HashMap
<
String
,
CellStyle
>();
for
(
Object
[]
os
:
fields
)
{
Excel
excel
=
(
Excel
)
os
[
1
];
String
key
=
StringUtils
.
format
(
"header_{}_{}"
,
excel
.
headerColor
(),
excel
.
headerBackgroundColor
());
if
(!
headerStyles
.
containsKey
(
key
))
{
CellStyle
style
=
wb
.
createCellStyle
();
style
=
wb
.
createCellStyle
();
style
.
cloneStyleFrom
(
styles
.
get
(
"data"
));
style
.
setAlignment
(
HorizontalAlignment
.
CENTER
);
style
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
style
.
setFillForegroundColor
(
excel
.
headerBackgroundColor
().
index
);
style
.
setFillPattern
(
FillPatternType
.
SOLID_FOREGROUND
);
Font
headerFont
=
wb
.
createFont
();
headerFont
.
setFontName
(
"Arial"
);
headerFont
.
setFontHeightInPoints
((
short
)
10
);
headerFont
.
setBold
(
true
);
headerFont
.
setColor
(
excel
.
headerColor
().
index
);
style
.
setFont
(
headerFont
);
headerStyles
.
put
(
key
,
style
);
}
}
return
headerStyles
;
}
/**
* 根据Excel注解创建表格列样式
*
* @param wb 工作薄对象
* @return 自定义样式列表
*/
private
Map
<
String
,
CellStyle
>
annotationStyles
(
Workbook
wb
)
private
Map
<
String
,
CellStyle
>
annotation
Data
Styles
(
Workbook
wb
)
{
Map
<
String
,
CellStyle
>
styles
=
new
HashMap
<
String
,
CellStyle
>();
for
(
Object
[]
os
:
fields
)
{
Excel
excel
=
(
Excel
)
os
[
1
];
String
key
=
"data_"
+
excel
.
align
()
+
"_"
+
excel
.
color
(
);
String
key
=
StringUtils
.
format
(
"data_{}_{}_{}"
,
excel
.
align
(),
excel
.
color
(),
excel
.
backgroundColor
()
);
if
(!
styles
.
containsKey
(
key
))
{
CellStyle
style
=
wb
.
createCellStyle
();
...
...
@@ -704,6 +726,8 @@ public class ExcelUtil<T>
style
.
setTopBorderColor
(
IndexedColors
.
GREY_50_PERCENT
.
getIndex
());
style
.
setBorderBottom
(
BorderStyle
.
THIN
);
style
.
setBottomBorderColor
(
IndexedColors
.
GREY_50_PERCENT
.
getIndex
());
style
.
setFillPattern
(
FillPatternType
.
SOLID_FOREGROUND
);
style
.
setFillForegroundColor
(
excel
.
backgroundColor
().
getIndex
());
Font
dataFont
=
wb
.
createFont
();
dataFont
.
setFontName
(
"Arial"
);
dataFont
.
setFontHeightInPoints
((
short
)
10
);
...
...
@@ -725,7 +749,7 @@ public class ExcelUtil<T>
// 写入列信息
cell
.
setCellValue
(
attr
.
name
());
setDataValidation
(
attr
,
row
,
column
);
cell
.
setCellStyle
(
styles
.
get
(
"header"
));
cell
.
setCellStyle
(
styles
.
get
(
StringUtils
.
format
(
"header_{}_{}"
,
attr
.
headerColor
(),
attr
.
headerBackgroundColor
())
));
return
cell
;
}
...
...
@@ -833,7 +857,7 @@ public class ExcelUtil<T>
{
// 创建cell
cell
=
row
.
createCell
(
column
);
cell
.
setCellStyle
(
styles
.
get
(
"data_"
+
attr
.
align
()
+
"_"
+
attr
.
color
(
)));
cell
.
setCellStyle
(
styles
.
get
(
StringUtils
.
format
(
"data_{}_{}_{}"
,
attr
.
align
(),
attr
.
color
(),
attr
.
backgroundColor
()
)));
// 用于读取对象中的属性
Object
value
=
getTargetValue
(
vo
,
field
,
attr
);
...
...
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