Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
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
YZG
yzg-util
Commits
d744bb15
Commit
d744bb15
authored
Oct 10, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
接口文档的支持
parent
536e9672
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
56 deletions
+72
-56
ExcelConsole.java
...-db/src/main/java/com/yanzuoguang/excel/ExcelConsole.java
+25
-34
ExcelMergerData.java
.../src/main/java/com/yanzuoguang/excel/ExcelMergerData.java
+47
-22
No files found.
yzg-util-db/src/main/java/com/yanzuoguang/excel/ExcelConsole.java
View file @
d744bb15
...
...
@@ -268,6 +268,22 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
row
.
setHeight
(
getUnit
(
this
.
config
.
getRowHeight
()));
// 合并組数据处理
for
(
Map
.
Entry
<
String
,
List
<
String
>>
groupKvp
:
mergerGroup
.
entrySet
())
{
// 将当前組生成值密钥
StringBuilder
sb
=
new
StringBuilder
();
for
(
String
columnName
:
groupKvp
.
getValue
())
{
String
value
=
StringHelper
.
getFirst
(
rowHandle
.
get
(
t
,
columnName
));
sb
.
append
(
value
.
replace
(
":"
,
"::"
));
sb
.
append
(
":"
);
}
String
groupValue
=
StringHelper
.
md5
(
sb
.
toString
());
// 更新合并内容
ExcelMergerData
mergerData
=
mergerGroupData
.
get
(
groupKvp
.
getKey
());
mergerData
.
updateMerger
(
rowIndex
,
groupValue
);
}
// 写入本行内容
for
(
int
columnPos
=
0
;
columnPos
<
this
.
config
.
getColumns
().
size
();
columnPos
++)
{
ExportColumn
column
=
this
.
config
.
getColumns
().
get
(
columnPos
);
...
...
@@ -276,16 +292,18 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 判断列是否需要合并
if
(
column
.
isMerger
())
{
ExcelMergerData
mergerData
=
mergerGroupData
.
get
(
column
.
getMegerGroup
());
// 判断是否需要合并历史记录
if
(
mergerData
.
isMergerHistory
(
value
)
)
{
if
(
mergerData
.
getRowIndexHistory
()
!=
mergerData
.
getRowIndex
()
-
1
)
{
// 合并历史记录单元格
mergerData
(
mergerData
,
columnPos
);
}
else
{
// 当不需要合并历史记录时,则创建新的内容
Cell
cell
=
row
.
createCell
(
columnPos
);
cell
.
setCellValue
(
value
);
}
// 当不需要合并历史记录时,则创建新的内容
Cell
cell
=
row
.
createCell
(
columnPos
);
cell
.
setCellValue
(
value
);
}
else
{
// 不合并时直接写入单元格内容
Cell
cell
=
row
.
createCell
(
columnPos
);
...
...
@@ -293,22 +311,6 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
}
}
// 合并組数据处理
for
(
Map
.
Entry
<
String
,
List
<
String
>>
groupKvp
:
mergerGroup
.
entrySet
())
{
// 将当前組生成值密钥
StringBuilder
sb
=
new
StringBuilder
();
for
(
String
columnName
:
groupKvp
.
getValue
())
{
String
value
=
StringHelper
.
getFirst
(
rowHandle
.
get
(
t
,
columnName
));
sb
.
append
(
value
.
replace
(
":"
,
"::"
));
sb
.
append
(
":"
);
}
String
groupValue
=
StringHelper
.
md5
(
sb
.
toString
());
// 更新合并内容
ExcelMergerData
mergerData
=
mergerGroupData
.
get
(
groupKvp
.
getKey
());
mergerData
.
updateMerger
(
rowIndex
,
groupValue
);
}
rowIndex
++;
}
...
...
@@ -319,8 +321,8 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
* @param columnPos 合并的列位置
*/
private
void
mergerData
(
ExcelMergerData
mergerColumn
,
int
columnPos
)
{
int
rowStart
=
mergerColumn
.
getRowIndex
();
int
rowEnd
=
rowStart
+
mergerColumn
.
getRowCell
()
-
1
;
int
rowStart
=
mergerColumn
.
getRowIndex
History
();
int
rowEnd
=
rowStart
+
mergerColumn
.
getRowCell
History
()
-
1
;
CellRangeAddress
region
=
new
CellRangeAddress
(
rowStart
,
rowEnd
,
columnPos
,
columnPos
);
sheet
.
addMergedRegion
(
region
);
}
...
...
@@ -333,17 +335,6 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
return
this
;
}
// 写入最后的合并内容
for
(
int
columnPos
=
0
;
columnPos
<
this
.
config
.
getColumns
().
size
();
columnPos
++)
{
ExportColumn
column
=
this
.
config
.
getColumns
().
get
(
columnPos
);
if
(
column
.
isMerger
())
{
ExcelMergerData
mergerData
=
mergerGroupData
.
get
(
column
.
getMegerGroup
());
if
(
mergerData
.
isMergerHistory
())
{
mergerData
(
mergerData
,
columnPos
);
}
}
}
try
{
OutputStream
out
=
new
FileOutputStream
(
this
.
getFileName
());
try
{
...
...
yzg-util-db/src/main/java/com/yanzuoguang/excel/ExcelMergerData.java
View file @
d744bb15
...
...
@@ -4,6 +4,7 @@ import com.yanzuoguang.util.helper.StringHelper;
/**
* 数据合并处理
*
* @author 颜佐光
*/
public
class
ExcelMergerData
{
...
...
@@ -21,7 +22,17 @@ public class ExcelMergerData {
/**
* 数据合并行数
*/
private
int
rowCell
;
private
int
rowCell
=
1
;
/**
* 上次合并行号
*/
private
int
rowIndexHistory
=
-
1
;
/**
* 上次合并单元格
*/
private
int
rowCellHistory
=
1
;
/**
* 写入新数据
...
...
@@ -30,43 +41,57 @@ public class ExcelMergerData {
* @param nowCellData 新数据
*/
public
void
updateMerger
(
int
rowIndex
,
String
nowCellData
)
{
if
(
this
.
rowIndex
==
-
1
||
!
StringHelper
.
compare
(
this
.
data
,
nowCellData
))
{
this
.
rowIndex
=
rowIndex
;
boolean
isMergerSuccess
=
this
.
rowIndex
==
-
1
||
!
StringHelper
.
compare
(
this
.
data
,
nowCellData
);
if
(
isMergerSuccess
)
{
this
.
rowIndexHistory
=
this
.
rowIndex
;
this
.
rowCellHistory
=
this
.
rowCell
;
this
.
data
=
nowCellData
;
this
.
rowIndex
=
rowIndex
;
this
.
rowCell
=
1
;
}
else
{
this
.
rowCell
++;
}
}
/**
* 写入数据,并且判断是否需要创建单元格
*
* @param nowCellData 需要写入的单元格的数据
* @return
*/
public
boolean
isMergerHistory
(
String
nowCellData
)
{
return
isMergerHistory
()
&&
!
StringHelper
.
compare
(
this
.
data
,
nowCellData
);
}
/**
* 是否合并单元格
*
* @return 合并标记
*/
public
boolean
isMergerHistory
()
{
return
rowCell
>
1
&&
rowIndex
>=
0
;
}
public
String
getData
()
{
return
data
;
}
public
void
setData
(
String
data
)
{
this
.
data
=
data
;
}
public
int
getRowIndex
()
{
return
rowIndex
;
}
public
void
setRowIndex
(
int
rowIndex
)
{
this
.
rowIndex
=
rowIndex
;
}
public
int
getRowCell
()
{
return
rowCell
;
}
public
void
setRowCell
(
int
rowCell
)
{
this
.
rowCell
=
rowCell
;
}
public
int
getRowIndexHistory
()
{
return
rowIndexHistory
;
}
public
void
setRowIndexHistory
(
int
rowIndexHistory
)
{
this
.
rowIndexHistory
=
rowIndexHistory
;
}
public
int
getRowCellHistory
()
{
return
rowCellHistory
;
}
public
void
setRowCellHistory
(
int
rowCellHistory
)
{
this
.
rowCellHistory
=
rowCellHistory
;
}
}
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