Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gic-enterprise-base
Overview
Overview
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
base_platform_enterprise
gic-enterprise-base
Commits
f59737c8
Commit
f59737c8
authored
Apr 08, 2020
by
guojuxing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
导出添加2层标题结构方法
parent
b3bcc912
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
287 additions
and
8 deletions
+287
-8
DownloadExcelQO.java
...ad/src/main/java/com/gic/download/qo/DownloadExcelQO.java
+133
-0
HeaderQO.java
...-download/src/main/java/com/gic/download/qo/HeaderQO.java
+46
-0
DownloadUtils.java
...d/src/main/java/com/gic/download/utils/DownloadUtils.java
+54
-4
ExcelUtils.java
...load/src/main/java/com/gic/download/utils/ExcelUtils.java
+54
-4
No files found.
gic-enterprise-download/src/main/java/com/gic/download/qo/DownloadExcelQO.java
0 → 100644
View file @
f59737c8
package
com
.
gic
.
download
.
qo
;
import
java.io.Serializable
;
import
java.util.List
;
/**
* 下载导出文件参数列表类
* @ClassName: DownloadExcelQO
* @Description:
* @author guojuxing
* @date 2020/4/8 2:05 PM
*/
public
class
DownloadExcelQO
implements
Serializable
{
private
static
final
long
serialVersionUID
=
6989738912564565045L
;
/**
* 临时路径
*/
private
String
tempPath
;
/**
* 报表ID
*/
private
Integer
reportId
;
/**
* 导出的文件名称
*/
private
String
fileName
;
/**
* 导出的文件后缀code,详情看ExcelExtensionEnum枚举类
*/
private
Integer
excelExtensionCode
;
/**
* 第一行的名称列表(一层标题结构)
*/
private
List
<
String
>
headerList
;
/**
* 2层标题结构所需要参数
*/
private
List
<
HeaderQO
>
headers
;
/**
* 字段名称
*/
private
List
<
String
>
propertyNameList
;
/**
* 需要加密的字段
*/
private
List
<
String
>
needEncryptField
;
/**
* 列宽,默认null
*/
private
List
<
Integer
>
columnWidth
=
null
;
public
String
getTempPath
()
{
return
tempPath
;
}
public
DownloadExcelQO
setTempPath
(
String
tempPath
)
{
this
.
tempPath
=
tempPath
;
return
this
;
}
public
Integer
getReportId
()
{
return
reportId
;
}
public
DownloadExcelQO
setReportId
(
Integer
reportId
)
{
this
.
reportId
=
reportId
;
return
this
;
}
public
String
getFileName
()
{
return
fileName
;
}
public
DownloadExcelQO
setFileName
(
String
fileName
)
{
this
.
fileName
=
fileName
;
return
this
;
}
public
Integer
getExcelExtensionCode
()
{
return
excelExtensionCode
;
}
public
DownloadExcelQO
setExcelExtensionCode
(
Integer
excelExtensionCode
)
{
this
.
excelExtensionCode
=
excelExtensionCode
;
return
this
;
}
public
List
<
String
>
getHeaderList
()
{
return
headerList
;
}
public
DownloadExcelQO
setHeaderList
(
List
<
String
>
headerList
)
{
this
.
headerList
=
headerList
;
return
this
;
}
public
List
<
HeaderQO
>
getHeaders
()
{
return
headers
;
}
public
DownloadExcelQO
setHeaders
(
List
<
HeaderQO
>
headers
)
{
this
.
headers
=
headers
;
return
this
;
}
public
List
<
String
>
getPropertyNameList
()
{
return
propertyNameList
;
}
public
DownloadExcelQO
setPropertyNameList
(
List
<
String
>
propertyNameList
)
{
this
.
propertyNameList
=
propertyNameList
;
return
this
;
}
public
List
<
String
>
getNeedEncryptField
()
{
return
needEncryptField
;
}
public
DownloadExcelQO
setNeedEncryptField
(
List
<
String
>
needEncryptField
)
{
this
.
needEncryptField
=
needEncryptField
;
return
this
;
}
public
List
<
Integer
>
getColumnWidth
()
{
return
columnWidth
;
}
public
DownloadExcelQO
setColumnWidth
(
List
<
Integer
>
columnWidth
)
{
this
.
columnWidth
=
columnWidth
;
return
this
;
}
}
gic-enterprise-download/src/main/java/com/gic/download/qo/HeaderQO.java
0 → 100644
View file @
f59737c8
package
com
.
gic
.
download
.
qo
;
import
java.io.Serializable
;
import
java.util.List
;
/**
* 二级表头类
* @ClassName: HeaderQO
* @Description:
* @author guojuxing
* @date 2020/4/8 11:37 AM
*/
public
class
HeaderQO
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1804894825368314534L
;
private
String
headName
;
private
List
<
String
>
sonHeadName
;
public
HeaderQO
()
{
}
public
HeaderQO
(
String
headName
,
List
<
String
>
sonHeadName
)
{
this
.
headName
=
headName
;
this
.
sonHeadName
=
sonHeadName
;
}
public
String
getHeadName
()
{
return
headName
;
}
public
HeaderQO
setHeadName
(
String
headName
)
{
this
.
headName
=
headName
;
return
this
;
}
public
List
<
String
>
getSonHeadName
()
{
return
sonHeadName
;
}
public
HeaderQO
setSonHeadName
(
List
<
String
>
sonHeadName
)
{
this
.
sonHeadName
=
sonHeadName
;
return
this
;
}
}
gic-enterprise-download/src/main/java/com/gic/download/utils/DownloadUtils.java
View file @
f59737c8
...
...
@@ -8,6 +8,8 @@ import javax.servlet.http.HttpServletResponse;
import
com.gic.download.constants.ExcelExtensionEnum
;
import
com.gic.download.dto.DownloadReportTempDTO
;
import
com.gic.download.qo.DownloadExcelQO
;
import
com.gic.download.qo.HeaderQO
;
import
com.gic.thirdparty.BucketNameEnum
;
import
com.gic.thirdparty.FileOperateUtils
;
import
org.apache.commons.beanutils.BeanUtils
;
...
...
@@ -20,8 +22,7 @@ import org.apache.http.client.utils.DateUtils;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook
;
import
org.apache.poi.ss.usermodel.Sheet
;
import
org.apache.poi.ss.usermodel.Workbook
;
import
org.apache.poi.ss.usermodel.*
;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook
;
public
class
DownloadUtils
{
...
...
@@ -126,6 +127,44 @@ public class DownloadUtils {
* @date 2017年3月16日 上午11:47:31
*/
protected
<
T
>
void
download
(
String
tempPath
,
Integer
reportId
,
String
fileName
,
Integer
excelExtensionCode
,
List
<
String
>
headerList
,
List
<
String
>
propertyNameList
,
DownloadDataLoader
<
T
>
loader
,
List
<
String
>
needEncryptField
,
List
<
Integer
>
columnWidth
)
throws
Exception
{
downloadCommon
(
new
DownloadExcelQO
().
setTempPath
(
tempPath
).
setReportId
(
reportId
).
setFileName
(
fileName
)
.
setExcelExtensionCode
(
excelExtensionCode
).
setHeaderList
(
headerList
).
setPropertyNameList
(
propertyNameList
)
.
setNeedEncryptField
(
needEncryptField
).
setColumnWidth
(
columnWidth
),
loader
);
}
/**
* 下载文件,2层标题结构
* @Title: downloadOfDoubleHeaderTitle
* @Description:
* @author guojuxing
* @param tempPath 临时路径
* @param reportId 报表中心ID
* @param fileName 文件名称
* @param excelExtensionCode Excel文件扩展名 枚举 ExcelExtensionEnum
* @param headerList 表头名称 2层标题结构
* @param propertyNameList 字段名
* @param loader 数据加载类
* @param needEncryptField 需要加密的字段,每一个元素存的是字段,如phone(电话)
* @param columnWidth 列宽
* @return void
*/
protected
<
T
>
void
downloadOfDoubleHeaderTitle
(
String
tempPath
,
Integer
reportId
,
String
fileName
,
Integer
excelExtensionCode
,
List
<
HeaderQO
>
headerList
,
List
<
String
>
propertyNameList
,
DownloadDataLoader
<
T
>
loader
,
List
<
String
>
needEncryptField
,
List
<
Integer
>
columnWidth
)
throws
Exception
{
downloadCommon
(
new
DownloadExcelQO
().
setTempPath
(
tempPath
).
setReportId
(
reportId
).
setFileName
(
fileName
)
.
setExcelExtensionCode
(
excelExtensionCode
).
setHeaders
(
headerList
).
setPropertyNameList
(
propertyNameList
)
.
setNeedEncryptField
(
needEncryptField
).
setColumnWidth
(
columnWidth
),
loader
);
}
private
<
T
>
void
downloadCommon
(
DownloadExcelQO
param
,
DownloadDataLoader
<
T
>
loader
)
throws
Exception
{
Integer
reportId
=
param
.
getReportId
();
String
fileName
=
param
.
getFileName
();
Integer
excelExtensionCode
=
param
.
getExcelExtensionCode
();
List
<
String
>
headerList
=
param
.
getHeaderList
();
List
<
HeaderQO
>
headers
=
param
.
getHeaders
();
List
<
String
>
propertyNameList
=
param
.
getPropertyNameList
();
List
<
String
>
needEncryptField
=
param
.
getNeedEncryptField
();
String
tempPath
=
param
.
getTempPath
();
//是否是2层标题结构
boolean
isDoubleHeaderTitle
=
CollectionUtils
.
isNotEmpty
(
headers
);
if
(
StringUtils
.
isEmpty
(
fileName
)
||
loader
==
null
||
CollectionUtils
.
isEmpty
(
propertyNameList
))
{
throw
new
RuntimeException
(
"参数错误。FileName:"
+
fileName
+
",DataLoader:"
+
loader
+
",PropertyName:"
+
propertyNameList
);
...
...
@@ -166,8 +205,19 @@ public class DownloadUtils {
writeOutputStream
(
loader
,
propertyNameList
,
writer
,
needEncryptField
,
reportId
);
// 写入excel
if
(!
ExcelUtils
.
setExcelInfo
(
sheet
,
columnWidth
,
headerList
,
rows
))
{
throw
new
IOException
(
"设置导出文件内容失败。"
);
if
(
isDoubleHeaderTitle
)
{
CellStyle
style
=
workbook
.
createCellStyle
();
//// 指定单元格居中对齐
style
.
setAlignment
(
HorizontalAlignment
.
CENTER
);
// 指定单元格垂直居中对齐
style
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
if
(!
ExcelUtils
.
setHeaderTitle
(
style
,
sheet
,
param
.
getColumnWidth
(),
headers
,
rows
))
{
throw
new
IOException
(
"设置导出文件内容失败。"
);
}
}
else
{
if
(!
ExcelUtils
.
setExcelInfo
(
sheet
,
param
.
getColumnWidth
(),
headerList
,
rows
))
{
throw
new
IOException
(
"设置导出文件内容失败。"
);
}
}
workbook
.
write
(
out
);
...
...
gic-enterprise-download/src/main/java/com/gic/download/utils/ExcelUtils.java
View file @
f59737c8
...
...
@@ -7,15 +7,14 @@ import java.util.*;
import
javax.servlet.http.HttpServletResponse
;
import
com.gic.download.qo.HeaderQO
;
import
org.apache.commons.beanutils.BeanMap
;
import
org.apache.commons.collections.CollectionUtils
;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
import
org.apache.poi.hssf.usermodel.*
;
import
org.apache.poi.ss.usermodel.Cell
;
import
org.apache.poi.ss.usermodel.Row
;
import
org.apache.poi.ss.usermodel.Sheet
;
import
org.apache.poi.ss.usermodel.Workbook
;
import
org.apache.poi.ss.usermodel.*
;
import
org.apache.poi.ss.util.CellRangeAddress
;
public
class
ExcelUtils
{
/**
...
...
@@ -146,6 +145,57 @@ public class ExcelUtils {
return
true
;
}
public
static
boolean
setHeaderTitle
(
CellStyle
cellStyle
,
Sheet
sheet
,
List
<
Integer
>
columnWidth
,
List
<
HeaderQO
>
headerList
,
List
<?>
content
)
{
if
(
sheet
==
null
)
{
logger
.
info
(
"sheet is null"
);
return
false
;
}
// 设置sheet格式
setSheetStyle
(
sheet
,
columnWidth
);
Row
row1
=
sheet
.
createRow
(
0
);
Row
row2
=
sheet
.
createRow
(
1
);
for
(
int
i
=
0
,
n
=
0
,
length
=
headerList
.
size
();
i
<
length
;
i
++)
{
Cell
cell1
=
row1
.
createCell
(
n
);
List
<
String
>
sonHeader
=
headerList
.
get
(
i
).
getSonHeadName
();
String
value
=
headerList
.
get
(
i
).
getHeadName
();
cell1
.
setCellValue
(
value
);
cell1
.
setCellStyle
(
cellStyle
);
//2层标题
if
(
CollectionUtils
.
isEmpty
(
sonHeader
))
{
//单标题
Cell
cell2
=
row2
.
createCell
(
n
);
cell2
.
setCellType
(
HSSFCell
.
CELL_TYPE_STRING
);
cell2
.
setCellStyle
(
cellStyle
);
sheet
.
addMergedRegion
(
new
CellRangeAddress
(
0
,
1
,
n
,
n
));
n
++;
continue
;
}
//创建第一行大标题
sheet
.
addMergedRegion
(
new
CellRangeAddress
(
0
,
0
,
n
,
(
n
+
sonHeader
.
size
()
-
1
)));
//赋值
for
(
int
j
=
0
,
sonLength
=
sonHeader
.
size
();
j
<
sonLength
;
j
++)
{
Cell
cell2
=
row2
.
createCell
(
n
++);
cell2
.
setCellType
(
HSSFCell
.
CELL_TYPE_STRING
);
cell2
.
setCellStyle
(
cellStyle
);
cell2
.
setCellValue
(
sonHeader
.
get
(
j
));
}
}
// 如果内容为空 则退出
if
(
content
==
null
||
content
.
isEmpty
())
{
logger
.
info
(
"content is null,cannot write excel"
);
return
true
;
}
int
indexRow
=
2
;
for
(
Object
rowContent
:
content
)
{
Row
row
=
sheet
.
createRow
(
indexRow
++);
setRowInfo
(
row
,
rowContent
);
}
return
true
;
}
/**
* 導出到excel
*
...
...
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