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
0004b5a5
Commit
0004b5a5
authored
Feb 22, 2023
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
执行时间
parent
63cf9379
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
94 additions
and
83 deletions
+94
-83
ExcelConsole.java
...-db/src/main/java/com/yanzuoguang/excel/ExcelConsole.java
+79
-82
ExportData.java
...il-db/src/main/java/com/yanzuoguang/excel/ExportData.java
+15
-1
No files found.
yzg-util-db/src/main/java/com/yanzuoguang/excel/ExcelConsole.java
View file @
0004b5a5
...
@@ -30,6 +30,7 @@ import java.util.regex.Pattern;
...
@@ -30,6 +30,7 @@ import java.util.regex.Pattern;
* @author 颜佐光
* @author 颜佐光
*/
*/
public
class
ExcelConsole
<
T
extends
Object
>
implements
DbRow
<
T
>
{
public
class
ExcelConsole
<
T
extends
Object
>
implements
DbRow
<
T
>
{
private
final
Pattern
chinese
=
Pattern
.
compile
(
"[^x00-xff]"
);
private
final
Pattern
chinese
=
Pattern
.
compile
(
"[^x00-xff]"
);
/**
/**
* 配置信息
* 配置信息
...
@@ -84,11 +85,6 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -84,11 +85,6 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
*/
*/
private
Map
<
String
,
ExcelMergerData
>
mergerGroupData
;
private
Map
<
String
,
ExcelMergerData
>
mergerGroupData
;
/**
* 最大字节长度
*/
private
Map
<
Integer
,
Integer
>
columnBytes
;
/**
/**
* 控制台输出Excel
* 控制台输出Excel
*
*
...
@@ -111,10 +107,20 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -111,10 +107,20 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
this
.
rowHandle
=
rowHandle
;
this
.
rowHandle
=
rowHandle
;
}
}
/**
* 缓存行
*
* @return 行号
*/
public
int
getCacheRow
()
{
public
int
getCacheRow
()
{
return
cacheRow
;
return
cacheRow
;
}
}
/**
* 缓存行
*
* @param cacheRow 行号
*/
public
void
setCacheRow
(
int
cacheRow
)
{
public
void
setCacheRow
(
int
cacheRow
)
{
this
.
cacheRow
=
cacheRow
;
this
.
cacheRow
=
cacheRow
;
}
}
...
@@ -131,9 +137,15 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -131,9 +137,15 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
/**
/**
* 行处理方式
* 行处理方式
*
*
* @return
* @return
当前处理行
*/
*/
public
ExcelRow
<
T
>
getRowHandle
()
{
public
ExcelRow
<
T
>
getRowHandle
()
{
ExcelRow
rowHandle
;
if
(
this
.
rowHandle
==
null
)
{
rowHandle
=
ExcelRowDefault
.
getInstance
();
}
else
{
rowHandle
=
this
.
rowHandle
;
}
return
rowHandle
;
return
rowHandle
;
}
}
...
@@ -150,19 +162,10 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -150,19 +162,10 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
/**
/**
* 检测参数是否异常
* 检测参数是否异常
*/
*/
public
ExcelConsole
check
()
{
public
ExcelConsole
<
T
>
check
()
{
CheckerHelper
check
=
CheckerHelper
.
newInstance
()
CheckerHelper
check
=
CheckerHelper
.
newInstance
().
notBlankCheck
(
"导出xls配置"
,
this
.
config
).
notBlankCheck
(
"导出xls.标题"
,
this
.
config
.
getTitle
()).
notBlankCheck
(
"导出xls.子标题"
,
this
.
config
.
getSubTitle
()).
notBlankCheck
(
"导出xls.服务器路径"
,
this
.
config
.
getServerPath
()).
notBlankCheck
(
"导出xls.文件名"
,
this
.
config
.
getFileName
()).
notBlankListCheck
(
"导出xls.列"
,
this
.
config
.
getColumns
()).
checkException
();
.
notBlankCheck
(
"导出xls配置"
,
this
.
config
)
.
notBlankCheck
(
"导出xls.标题"
,
this
.
config
.
getTitle
())
.
notBlankCheck
(
"导出xls.子标题"
,
this
.
config
.
getSubTitle
())
.
notBlankCheck
(
"导出xls.服务器路径"
,
this
.
config
.
getServerPath
())
.
notBlankCheck
(
"导出xls.文件名"
,
this
.
config
.
getFileName
())
.
notBlankListCheck
(
"导出xls.列"
,
this
.
config
.
getColumns
())
.
checkException
();
for
(
ExportColumn
column
:
this
.
config
.
getColumns
())
{
for
(
ExportColumn
column
:
this
.
config
.
getColumns
())
{
check
.
notBlankCheck
(
"导出xls.列名"
,
column
.
getName
())
check
.
notBlankCheck
(
"导出xls.列名"
,
column
.
getName
()).
notBlankCheck
(
"导出xls.标题"
,
column
.
getTitle
()).
checkException
();
.
notBlankCheck
(
"导出xls.标题"
,
column
.
getTitle
())
.
checkException
();
}
}
return
this
;
return
this
;
...
@@ -193,9 +196,8 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -193,9 +196,8 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
throw
YzgError
.
getRuntimeException
(
"034"
);
throw
YzgError
.
getRuntimeException
(
"034"
);
}
}
// 创建合并对象数据检测
// 创建合并对象数据检测
mergerGroup
=
new
HashMap
<>();
mergerGroup
=
new
HashMap
<>(
20
);
mergerGroupData
=
new
HashMap
<>();
mergerGroupData
=
new
HashMap
<>(
20
);
columnBytes
=
new
HashMap
<>();
if
(
this
.
cacheRow
<
1
)
{
if
(
this
.
cacheRow
<
1
)
{
this
.
cacheRow
=
1000
;
this
.
cacheRow
=
1000
;
...
@@ -206,7 +208,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -206,7 +208,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
workbook
.
setCompressTempFiles
(
true
);
workbook
.
setCompressTempFiles
(
true
);
sheet
=
workbook
.
createSheet
();
sheet
=
workbook
.
createSheet
();
style
=
initColumnCenter
s
tyle
(
workbook
);
style
=
initColumnCenter
S
tyle
(
workbook
);
// 行和列都是从0开始计数,且起始结束都会合并
// 行和列都是从0开始计数,且起始结束都会合并
...
@@ -218,7 +220,6 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -218,7 +220,6 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
writeHead
(
head
);
writeHead
(
head
);
rowIndex
+=
head
.
getTotalRow
();
rowIndex
+=
head
.
getTotalRow
();
}
}
/**
/**
...
@@ -233,7 +234,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -233,7 +234,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 创建一行
// 创建一行
Row
row
=
sheet
.
createRow
(
rowIndex
);
Row
row
=
sheet
.
createRow
(
rowIndex
);
row
.
setHeight
(
getUnit
(
rowHeight
));
row
.
setHeight
(
getUnit
(
rowHeight
));
Cell
cell
=
createCell
(
row
,
0
,
content
,
false
);
createCell
(
row
,
0
,
content
);
// 这里是合并excel中多列为1列
// 这里是合并excel中多列为1列
CellRangeAddress
region
=
new
CellRangeAddress
(
rowIndex
,
rowIndex
,
0
,
columnLength
);
CellRangeAddress
region
=
new
CellRangeAddress
(
rowIndex
,
rowIndex
,
0
,
columnLength
);
sheet
.
addMergedRegion
(
region
);
sheet
.
addMergedRegion
(
region
);
...
@@ -255,7 +256,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -255,7 +256,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 写入列头
// 写入列头
for
(
TableHeadItem
headItem
:
head
.
getColumns
())
{
for
(
TableHeadItem
headItem
:
head
.
getColumns
())
{
Row
row
=
rows
[
headItem
.
getRow
()];
Row
row
=
rows
[
headItem
.
getRow
()];
Cell
cell
=
createCell
(
row
,
headItem
.
getColumn
(),
headItem
.
getName
(),
true
);
createCell
(
row
,
headItem
.
getColumn
(),
headItem
.
getName
()
);
// 判断是否需要合并列头
// 判断是否需要合并列头
if
(
headItem
.
getColumnCell
()
>
1
||
headItem
.
getRowCell
()
>
1
)
{
if
(
headItem
.
getColumnCell
()
>
1
||
headItem
.
getRowCell
()
>
1
)
{
...
@@ -284,12 +285,21 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -284,12 +285,21 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 当前合并組中添加需要合并的列
// 当前合并組中添加需要合并的列
mergerGroup
.
get
(
group
).
add
(
column
.
getName
());
mergerGroup
.
get
(
group
).
add
(
column
.
getName
());
}
}
// 设置列宽度
int
columnPos
=
0
;
for
(
ExportColumn
column
:
this
.
config
.
getColumns
())
{
sheet
.
setColumnWidth
(
columnPos
,
getUnit
(
column
.
getWidth
()));
columnPos
++;
}
}
}
/**
/**
* 开始生成Excel文件
* 开始生成Excel文件
*
* @return 当前对象
*/
*/
public
ExcelConsole
open
()
{
public
ExcelConsole
<
T
>
open
()
{
this
.
check
();
this
.
check
();
TableHead
head
=
this
.
initHead
();
TableHead
head
=
this
.
initHead
();
initExcel
(
head
);
initExcel
(
head
);
...
@@ -304,15 +314,11 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -304,15 +314,11 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
*/
*/
@Override
@Override
public
void
handle
(
T
t
)
{
public
void
handle
(
T
t
)
{
ExcelRow
rowHandle
=
this
.
rowHandle
;
ExcelRow
<
T
>
rowHandle
=
this
.
getRowHandle
();
if
(
rowHandle
==
null
)
{
rowHandle
=
ExcelRowDefault
.
getInstance
();
}
// 创建一行
// 创建一行
Row
row
=
sheet
.
createRow
(
rowIndex
);
Row
row
=
sheet
.
createRow
(
rowIndex
);
row
.
setHeight
(
getUnit
(
this
.
config
.
getRowHeight
()));
// 高度
int
height
=
getUnit
(
this
.
config
.
getRowHeight
());
// 合并組数据处理
// 合并組数据处理
for
(
Map
.
Entry
<
String
,
List
<
String
>>
groupKvp
:
mergerGroup
.
entrySet
())
{
for
(
Map
.
Entry
<
String
,
List
<
String
>>
groupKvp
:
mergerGroup
.
entrySet
())
{
// 将当前組生成值密钥
// 将当前組生成值密钥
...
@@ -333,13 +339,23 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -333,13 +339,23 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
for
(
ExportColumn
column
:
this
.
config
.
getColumns
())
{
for
(
ExportColumn
column
:
this
.
config
.
getColumns
())
{
String
columnName
=
column
.
getName
();
String
columnName
=
column
.
getName
();
String
value
=
StringHelper
.
getFirst
(
rowHandle
.
get
(
t
,
columnName
));
String
value
=
StringHelper
.
getFirst
(
rowHandle
.
get
(
t
,
columnName
));
// 列宽度
int
columnWidth
=
getUnit
(
column
.
getWidth
());
// 实际内容宽度
int
chinaWidth
=
(
int
)
Math
.
round
(
value
.
length
()
*
1.5
);
// 当前单元格实际行高
int
nowCellHeight
=
StringHelper
.
getPage
(
chinaWidth
,
columnWidth
)
*
getUnit
(
this
.
config
.
getRowHeight
());
// 当前行行高
height
=
Math
.
max
(
nowCellHeight
,
height
);
// 当不需要合并历史记录时,则创建新的内容
// 当不需要合并历史记录时,则创建新的内容
Cell
cell
=
createCell
(
row
,
columnPos
,
value
,
tr
ue
);
createCell
(
row
,
columnPos
,
val
ue
);
mergerColumn
(
column
,
columnPos
,
false
);
mergerColumn
(
column
,
columnPos
,
false
);
columnPos
++;
columnPos
++;
}
}
row
.
setHeight
((
short
)
Math
.
min
(
height
,
Short
.
MAX_VALUE
));
rowIndex
++;
rowIndex
++;
rowData
++;
rowData
++;
...
@@ -384,49 +400,38 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -384,49 +400,38 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
CellRangeAddress
region
=
new
CellRangeAddress
(
rowStart
,
rowEnd
,
columnPos
,
columnPos
);
CellRangeAddress
region
=
new
CellRangeAddress
(
rowStart
,
rowEnd
,
columnPos
,
columnPos
);
sheet
.
addMergedRegion
(
region
);
sheet
.
addMergedRegion
(
region
);
Cell
cell
=
sheet
.
getRow
(
rowStart
).
getCell
(
columnPos
);
}
}
/**
/**
* 会自动在生成完毕调用该函数
* 会自动在生成完毕调用该函数
*
* @return 当前对象
*/
*/
public
ExcelConsole
save
()
{
public
ExcelConsole
<
T
>
save
()
{
if
(
this
.
excelStatus
!=
null
)
{
if
(
this
.
excelStatus
!=
null
)
{
excelStatus
.
excelFinish
(
this
);
excelStatus
.
excelFinish
(
this
);
}
}
if
(
workbook
==
null
)
{
if
(
workbook
==
null
)
{
return
this
;
return
this
;
}
}
// 合并数据配置
// 合并数据配置
if
(
this
.
rowData
>
0
)
{
if
(
this
.
rowData
>
0
)
{
int
columnPos
=
0
;
int
columnPos
=
0
;
for
(
ExportColumn
column
:
this
.
config
.
getColumns
())
{
for
(
ExportColumn
column
:
this
.
config
.
getColumns
())
{
mergerColumn
(
column
,
columnPos
,
true
);
mergerColumn
(
column
,
columnPos
,
true
);
if
(
column
.
getWidth
()
<
1
)
{
//设置单元格长度, 这里要乘上256
int
maxBytes
=
columnBytes
.
get
(
columnPos
);
sheet
.
setColumnWidth
(
columnPos
,
maxBytes
*
256
);
}
else
{
sheet
.
setColumnWidth
(
columnPos
,
getUnit
(
column
.
getWidth
()));
}
columnPos
++;
columnPos
++;
}
}
}
}
try
{
String
fileNameTemp
=
this
.
getFileNameTemp
();
String
fileNameTemp
=
this
.
getFileNameTemp
();
// 保存为临时文件
// 保存为临时文件
OutputStream
out
=
new
FileOutputStream
(
fileNameTemp
);
try
(
OutputStream
out
=
new
FileOutputStream
(
fileNameTemp
))
{
try
{
workbook
.
write
(
out
);
workbook
.
write
(
out
);
out
.
flush
();
out
.
flush
();
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
throw
YzgError
.
getRuntimeException
(
"035"
);
throw
YzgError
.
getRuntimeException
(
"035"
);
}
finally
{
out
.
close
();
}
}
// 判断临时文件是否存在
// 判断临时文件是否存在
File
file
=
new
File
(
fileNameTemp
);
File
file
=
new
File
(
fileNameTemp
);
...
@@ -435,10 +440,9 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -435,10 +440,9 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
}
}
// 重命名成正式文件
// 重命名成正式文件
String
fileName
=
this
.
getFileName
();
String
fileName
=
this
.
getFileName
();
file
.
renameTo
(
new
File
(
fileName
));
boolean
b
=
file
.
renameTo
(
new
File
(
fileName
));
}
catch
(
IOException
e
)
{
if
(!
b
)
{
e
.
printStackTrace
();
throw
YzgError
.
getRuntimeException
(
"036"
,
fileName
);
throw
YzgError
.
getRuntimeException
(
"035"
);
}
}
return
this
;
return
this
;
}
}
...
@@ -446,9 +450,9 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -446,9 +450,9 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
/**
/**
* 删除生成的Excel文件
* 删除生成的Excel文件
*
*
* @return
* @return
当前文件
*/
*/
public
ExcelConsole
remove
()
{
public
ExcelConsole
<
T
>
remove
()
{
if
(
this
.
workbook
!=
null
)
{
if
(
this
.
workbook
!=
null
)
{
workbook
=
null
;
workbook
=
null
;
}
}
...
@@ -468,8 +472,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -468,8 +472,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
*/
*/
public
String
getFileName
()
{
public
String
getFileName
()
{
FileHelper
.
createDirectory
(
this
.
config
.
getServerPath
());
FileHelper
.
createDirectory
(
this
.
config
.
getServerPath
());
String
fileName
=
String
.
format
(
"%s/%s"
,
this
.
config
.
getServerPath
(),
this
.
config
.
getFileName
());
return
String
.
format
(
"%s/%s"
,
this
.
config
.
getServerPath
(),
this
.
config
.
getFileName
());
return
fileName
;
}
}
/**
/**
...
@@ -484,19 +487,13 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -484,19 +487,13 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
/**
/**
* 创建单元格
* 创建单元格
*
*
* @param row
* @param row
行
* @param column
* @param column
列
* @param content
* @param content
内容
* @return
* @return
单元格
*/
*/
private
Cell
createCell
(
Row
row
,
int
column
,
String
content
,
boolean
isMax
)
{
private
Cell
createCell
(
Row
row
,
int
column
,
String
content
)
{
// 获取字节数、用于设置最大宽度
// 获取字节数、用于设置最大宽度
if
(
isMax
)
{
int
chinaCount
=
(
int
)
Math
.
round
(
content
.
length
()
*
1.5
);
int
maxBytes
=
Math
.
max
(
columnBytes
.
getOrDefault
(
column
,
0
),
chinaCount
);
columnBytes
.
put
(
column
,
maxBytes
);
}
Cell
cell
=
row
.
createCell
(
column
);
Cell
cell
=
row
.
createCell
(
column
);
cell
.
setCellStyle
(
style
);
cell
.
setCellStyle
(
style
);
cell
.
setCellValue
(
content
);
cell
.
setCellValue
(
content
);
...
@@ -510,7 +507,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
...
@@ -510,7 +507,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
* <b>@param wb
* <b>@param wb
* <b>@return</b>
* <b>@return</b>
*/
*/
public
CellStyle
initColumnCenter
s
tyle
(
Workbook
wb
)
{
public
CellStyle
initColumnCenter
S
tyle
(
Workbook
wb
)
{
CellStyle
cellStyle
=
wb
.
createCellStyle
();
CellStyle
cellStyle
=
wb
.
createCellStyle
();
...
...
yzg-util-db/src/main/java/com/yanzuoguang/excel/ExportData.java
View file @
0004b5a5
...
@@ -39,7 +39,7 @@ public class ExportData {
...
@@ -39,7 +39,7 @@ public class ExportData {
public
static
final
short
ROW_HEIGHT
=
30
;
public
static
final
short
ROW_HEIGHT
=
30
;
@ApiModelProperty
(
notes
=
"列默认宽度"
)
@ApiModelProperty
(
notes
=
"列默认宽度"
)
public
static
final
short
COLUMN_WIDTH
=
0
;
public
static
final
short
COLUMN_WIDTH
=
10
0
;
/**
/**
* 服务器保存路径
* 服务器保存路径
*/
*/
...
@@ -91,6 +91,12 @@ public class ExportData {
...
@@ -91,6 +91,12 @@ public class ExportData {
@ApiModelProperty
(
notes
=
"行高度"
)
@ApiModelProperty
(
notes
=
"行高度"
)
private
short
rowHeight
;
private
short
rowHeight
;
/**
* 自动换行
*/
@ApiModelProperty
(
notes
=
"自动换行"
)
private
boolean
line
;
/**
/**
* 包含列
* 包含列
*/
*/
...
@@ -181,6 +187,14 @@ public class ExportData {
...
@@ -181,6 +187,14 @@ public class ExportData {
this
.
rowHeight
=
rowHeight
;
this
.
rowHeight
=
rowHeight
;
}
}
public
boolean
isLine
()
{
return
line
;
}
public
void
setLine
(
boolean
line
)
{
this
.
line
=
line
;
}
public
List
<
ExportColumn
>
getColumns
()
{
public
List
<
ExportColumn
>
getColumns
()
{
return
columns
;
return
columns
;
}
}
...
...
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