Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
car-database-api
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
中汽研标准应用数据库
car-database-api
Commits
a52ce7b7
Commit
a52ce7b7
authored
Sep 24, 2024
by
yuanshuo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1./Plan/Record/exportTo接口修改,调用新的Utils类生成Excel
parent
037b5069
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
656 additions
and
1 deletion
+656
-1
ExcelMultipleSheetsUtil.java
...c/main/java/com/ruoyi/common/ExcelMultipleSheetsUtil.java
+650
-0
TestPlanRecordController.java
...src/main/java/com/ruoyi/web/TestPlanRecordController.java
+2
-1
TTestPlanRecordMapper.xml
...eview/src/main/resources/mapper/TTestPlanRecordMapper.xml
+4
-0
No files found.
quality-review/src/main/java/com/ruoyi/common/ExcelMultipleSheetsUtil.java
0 → 100644
View file @
a52ce7b7
package
com
.
ruoyi
.
common
;
import
cn.hutool.core.collection.CollUtil
;
import
cn.hutool.core.convert.Convert
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.core.util.ObjectUtil
;
import
cn.hutool.core.util.StrUtil
;
import
com.ruoyi.common.annotation.Excel
;
import
com.ruoyi.common.utils.DictUtils
;
import
com.ruoyi.domain.vo.RecaordUseCasesReportVO
;
import
com.ruoyi.domain.vo.SystemReviewExportVO
;
import
org.apache.poi.ss.usermodel.*
;
import
org.apache.poi.ss.util.CellRangeAddress
;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
import
java.io.UnsupportedEncodingException
;
import
java.lang.reflect.Field
;
import
java.net.URLEncoder
;
import
java.util.*
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.stream.Collectors
;
import
static
com
.
ruoyi
.
common
.
core
.
text
.
CharsetKit
.
UTF_8
;
public
class
ExcelMultipleSheetsUtil
{
/**
* 导出excel:可多个sheet页
*
* @param data 数据:Map 集合【key == 每一个sheet页的名称,value == sheet页数据】
* @param excelFileName excel文件名
* @param suffixName 后缀名
* @param response 响应
* @throws IOException 异常
*/
public
static
void
excelMultipleSheets
(
Map
<
String
,
Object
>
data
,
String
excelFileName
,
String
suffixName
,
HttpServletResponse
response
,
Map
<
String
,
String
>
cover
)
throws
IOException
{
// 创建工作簿
try
(
Workbook
workbook
=
new
XSSFWorkbook
())
{
Sheet
sheetFirst
=
workbook
.
createSheet
(
"概要"
);
sheetFirst
.
setColumnWidth
(
0
,
25
*
256
);
sheetFirst
.
setColumnWidth
(
1
,
25
*
256
);
// 创建数据行
Row
dataRowHead
=
sheetFirst
.
createRow
(
1
);
dataRowHead
.
setHeightInPoints
(
20
);
Cell
cell
=
dataRowHead
.
createCell
(
0
);
dataRowHead
.
createCell
(
1
).
setCellStyle
(
getStyle
(
sheetFirst
.
getWorkbook
()));
cell
.
setCellValue
(
"检测方案"
);
cell
.
setCellStyle
(
getStyle
(
sheetFirst
.
getWorkbook
()));
// 合并区域:从第1行到第2行,第1列到第2列(注意:行和列的索引从0开始)
CellRangeAddress
mergeRegion
=
new
CellRangeAddress
(
1
,
2
,
0
,
1
);
// 将合并区域添加到工作表中
sheetFirst
.
addMergedRegion
(
mergeRegion
);
final
int
[]
i
=
{
2
};
cover
.
forEach
((
key
,
value
)->{
Row
dataRow
=
sheetFirst
.
createRow
(
i
[
0
]);
dataRow
.
setHeightInPoints
(
20
);
Cell
cellTitle
=
dataRow
.
createCell
(
0
);
cellTitle
.
setCellValue
(
key
);
cellTitle
.
setCellStyle
(
createCellStyle
(
sheetFirst
.
getWorkbook
()));
Cell
cellText
=
dataRow
.
createCell
(
1
);
cellText
.
setCellValue
(
value
);
cellText
.
setCellStyle
(
createCellStyle
(
sheetFirst
.
getWorkbook
()));
i
[
0
]
=
i
[
0
]
+
1
;
});
for
(
Map
.
Entry
<
String
,
Object
>
entry
:
data
.
entrySet
())
{
String
sheetName
=
entry
.
getKey
();
Object
sheetData
=
entry
.
getValue
();
Sheet
sheet
=
workbook
.
createSheet
(
sheetName
);
if
(
ObjectUtil
.
isNotEmpty
(
sheetData
))
{
createSheetWithData
(
sheet
,
sheetData
);
}
}
setResponseHeader
(
response
,
excelFileName
,
suffixName
);
// 写出文件
workbook
.
write
(
response
.
getOutputStream
());
}
}
private
static
CellStyle
getStyle
(
Workbook
workbook
)
{
CellStyle
style
=
workbook
.
createCellStyle
();
// 设置底边框;
style
.
setBorderBottom
(
BorderStyle
.
THIN
);
// 设置底边框颜色;
style
.
setBottomBorderColor
(
IndexedColors
.
GREY_50_PERCENT
.
getIndex
());
// 设置左边框;
style
.
setBorderLeft
(
BorderStyle
.
THIN
);
// 设置左边框颜色;
style
.
setLeftBorderColor
(
IndexedColors
.
GREY_50_PERCENT
.
getIndex
());
// 设置右边框;
style
.
setBorderRight
(
BorderStyle
.
THIN
);
// 设置右边框颜色;
style
.
setRightBorderColor
(
IndexedColors
.
GREY_50_PERCENT
.
getIndex
());
// 设置顶边框;
style
.
setBorderTop
(
BorderStyle
.
THIN
);
// 设置顶边框颜色;
style
.
setTopBorderColor
(
IndexedColors
.
GREY_50_PERCENT
.
getIndex
());
// 设置字体
Font
font
=
workbook
.
createFont
();
// 设置字体大小
font
.
setFontHeightInPoints
((
short
)
20
);
// 设置字体名字
font
.
setFontName
(
"微软雅黑"
);
// 在样式用应用设置的字体;
style
.
setFont
(
font
);
// 设置自动换行;
style
.
setWrapText
(
true
);
// 设置水平对齐的样式为居中对齐;
style
.
setAlignment
(
HorizontalAlignment
.
CENTER
);
// 设置垂直对齐的样式为居中对齐;
style
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
// 设置填充样式(实心填充)
style
.
setFillPattern
(
FillPatternType
.
SOLID_FOREGROUND
);
// 设置单元格颜色
style
.
setFillForegroundColor
(
IndexedColors
.
GREY_25_PERCENT
.
getIndex
());
return
style
;
}
/**
* 创建表单并填充数据
*
* @param sheet 表单
*
*/
// 根据数据类型生成不同的 sheet
public
static
void
createSheetWithData
(
Sheet
sheet
,
Object
sheetData
)
{
if
(
sheetData
instanceof
List
)
{
List
<?>
dataList
=
(
List
<?>)
sheetData
;
if
(!
dataList
.
isEmpty
())
{
if
(
dataList
.
get
(
0
)
instanceof
RecaordUseCasesReportVO
)
{
mergeAndCreateRowsForUseCases
(
sheet
,
(
List
<
RecaordUseCasesReportVO
>)
sheetData
);
}
else
if
(
dataList
.
get
(
0
)
instanceof
SystemReviewExportVO
)
{
mergeAndCreateRowsForReview
(
sheet
,
(
List
<
SystemReviewExportVO
>)
sheetData
);
}
}
}
}
//自动换行
public
static
CellStyle
createCellStyleWithWrapText
(
Workbook
workbook
)
{
CellStyle
cellStyle
=
workbook
.
createCellStyle
();
cellStyle
.
setWrapText
(
true
);
// 启用自动换行
return
cellStyle
;
}
// 合并相同章节号的逻辑 (车型检验)
public
static
void
mergeAndCreateRowsForUseCases
(
Sheet
sheet
,
List
<
RecaordUseCasesReportVO
>
dataList
)
{
// 对 dataList 按章节号排序,支持 '4.3.5 a)' 格式
dataList
.
sort
(
Comparator
.
comparing
(
RecaordUseCasesReportVO:
:
getSerialNumber
,
(
chapter1
,
chapter2
)
->
{
// 将章节号拆分为数字和字母部分,并进行比较
Pattern
pattern
=
Pattern
.
compile
(
"(\\d+)|(\\D+)"
);
// 匹配数字和非数字部分
Matcher
matcher1
=
pattern
.
matcher
(
chapter1
);
Matcher
matcher2
=
pattern
.
matcher
(
chapter2
);
List
<
String
>
parts1
=
new
ArrayList
<>();
List
<
String
>
parts2
=
new
ArrayList
<>();
// 提取章节号的每个部分
while
(
matcher1
.
find
())
{
parts1
.
add
(
matcher1
.
group
());
}
while
(
matcher2
.
find
())
{
parts2
.
add
(
matcher2
.
group
());
}
int
len
=
Math
.
min
(
parts1
.
size
(),
parts2
.
size
());
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
String
part1
=
parts1
.
get
(
i
);
String
part2
=
parts2
.
get
(
i
);
// 判断是数字还是字母
if
(
part1
.
matches
(
"\\d+"
)
&&
part2
.
matches
(
"\\d+"
))
{
// 数字部分按数值比较
int
num1
=
Integer
.
parseInt
(
part1
);
int
num2
=
Integer
.
parseInt
(
part2
);
if
(
num1
!=
num2
)
{
return
Integer
.
compare
(
num1
,
num2
);
}
}
else
{
// 字母部分按字典顺序比较
int
cmp
=
part1
.
compareTo
(
part2
);
if
(
cmp
!=
0
)
{
return
cmp
;
}
}
}
// 如果所有部分都相同,比较长度(部分少的优先)
return
Integer
.
compare
(
parts1
.
size
(),
parts2
.
size
());
}));
Object
firstItem
=
dataList
.
get
(
0
);
createHeaderRow
(
sheet
,
firstItem
.
getClass
());
// 创建标题行
int
rowIndex
=
1
;
// 按章节号分组并保留顺序,使用 LinkedHashMap
Map
<
String
,
List
<
RecaordUseCasesReportVO
>>
groupedBySerialNumber
=
dataList
.
stream
()
.
collect
(
Collectors
.
groupingBy
(
RecaordUseCasesReportVO:
:
getSerialNumber
,
LinkedHashMap:
:
new
,
Collectors
.
toList
()));
// 遍历每个章节号
for
(
Map
.
Entry
<
String
,
List
<
RecaordUseCasesReportVO
>>
entry
:
groupedBySerialNumber
.
entrySet
())
{
String
serialNumber
=
entry
.
getKey
();
List
<
RecaordUseCasesReportVO
>
serialRecords
=
entry
.
getValue
();
// 按标准要求分组并保留顺序
Map
<
String
,
List
<
RecaordUseCasesReportVO
>>
groupedByRequirements
=
serialRecords
.
stream
()
.
collect
(
Collectors
.
groupingBy
(
RecaordUseCasesReportVO:
:
getRequirements
,
Collectors
.
toList
()));
int
startRowForSerial
=
rowIndex
;
// 记录章节号的开始行
// 遍历每个标准要求组
for
(
Map
.
Entry
<
String
,
List
<
RecaordUseCasesReportVO
>>
reqEntry
:
groupedByRequirements
.
entrySet
())
{
String
requirements
=
reqEntry
.
getKey
();
List
<
RecaordUseCasesReportVO
>
requirementRecords
=
reqEntry
.
getValue
();
int
startRowForRequirement
=
rowIndex
;
// 记录标准要求的开始行
// 遍历每条记录,写入用例编号和用例描述
for
(
RecaordUseCasesReportVO
record
:
requirementRecords
)
{
Row
row
=
sheet
.
createRow
(
rowIndex
++);
// 设置用例编号和用例描述
Cell
idCell
=
row
.
createCell
(
2
);
idCell
.
setCellValue
(
record
.
getCustomizedID
());
idCell
.
setCellStyle
(
getCenteredWrapTextStyle
(
sheet
.
getWorkbook
()));
Cell
descCell
=
row
.
createCell
(
3
);
descCell
.
setCellValue
(
record
.
getDescription
());
descCell
.
setCellStyle
(
getCenteredWrapTextStyle
(
sheet
.
getWorkbook
()));
// 应用居中+自动换行样式
}
// 合并标准要求列(如果标准要求有多行)
if
(
requirementRecords
.
size
()
>
1
)
{
CellRangeAddress
reqRange
=
new
CellRangeAddress
(
startRowForRequirement
,
rowIndex
-
1
,
1
,
1
);
sheet
.
addMergedRegion
(
reqRange
);
setRegionStyle
(
sheet
,
reqRange
,
getCenteredWrapTextStyle
(
sheet
.
getWorkbook
()));
// 应用边框样式
}
// 设置标准要求列
Cell
reqCell
=
sheet
.
getRow
(
startRowForRequirement
).
createCell
(
1
);
reqCell
.
setCellValue
(
requirements
);
reqCell
.
setCellStyle
(
getCenteredWrapTextStyle
(
sheet
.
getWorkbook
()));
sheet
.
autoSizeColumn
(
1
);
}
// 合并章节号列(如果章节号有多行)
if
(
serialRecords
.
size
()
>
1
)
{
CellRangeAddress
serialRange
=
new
CellRangeAddress
(
startRowForSerial
,
rowIndex
-
1
,
0
,
0
);
sheet
.
addMergedRegion
(
serialRange
);
setRegionStyle
(
sheet
,
serialRange
,
getCenteredWrapTextStyle
(
sheet
.
getWorkbook
()));
// 应用边框样式
}
// 设置章节号列
Cell
serialCell
=
sheet
.
getRow
(
startRowForSerial
).
createCell
(
0
);
serialCell
.
setCellValue
(
serialNumber
);
serialCell
.
setCellStyle
(
getCenteredWrapTextStyle
(
sheet
.
getWorkbook
()));
}
}
// 合并相同章节号的逻辑 (体系审查)
public
static
void
mergeAndCreateRowsForReview
(
Sheet
sheet
,
List
<
SystemReviewExportVO
>
dataList
)
{
// 对 dataList 按章节号排序
dataList
.
sort
(
Comparator
.
comparing
(
SystemReviewExportVO:
:
getChapter
,
(
chapter1
,
chapter2
)
->
{
Pattern
pattern
=
Pattern
.
compile
(
"(\\d+)|(\\D+)"
);
Matcher
matcher1
=
pattern
.
matcher
(
chapter1
);
Matcher
matcher2
=
pattern
.
matcher
(
chapter2
);
List
<
String
>
parts1
=
new
ArrayList
<>();
List
<
String
>
parts2
=
new
ArrayList
<>();
while
(
matcher1
.
find
())
{
parts1
.
add
(
matcher1
.
group
());
}
while
(
matcher2
.
find
())
{
parts2
.
add
(
matcher2
.
group
());
}
int
len
=
Math
.
min
(
parts1
.
size
(),
parts2
.
size
());
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
String
part1
=
parts1
.
get
(
i
);
String
part2
=
parts2
.
get
(
i
);
if
(
part1
.
matches
(
"\\d+"
)
&&
part2
.
matches
(
"\\d+"
))
{
int
num1
=
Integer
.
parseInt
(
part1
);
int
num2
=
Integer
.
parseInt
(
part2
);
if
(
num1
!=
num2
)
{
return
Integer
.
compare
(
num1
,
num2
);
}
}
else
{
int
cmp
=
part1
.
compareTo
(
part2
);
if
(
cmp
!=
0
)
{
return
cmp
;
}
}
}
return
Integer
.
compare
(
parts1
.
size
(),
parts2
.
size
());
}));
// 创建标题行
createHeaderRow
(
sheet
,
dataList
.
get
(
0
).
getClass
());
int
rowIndex
=
1
;
// 按章节号分组
Map
<
String
,
List
<
SystemReviewExportVO
>>
groupedByChapter
=
dataList
.
stream
()
.
collect
(
Collectors
.
groupingBy
(
SystemReviewExportVO:
:
getChapter
,
LinkedHashMap:
:
new
,
Collectors
.
toList
()));
for
(
Map
.
Entry
<
String
,
List
<
SystemReviewExportVO
>>
chapterEntry
:
groupedByChapter
.
entrySet
())
{
String
chapter
=
chapterEntry
.
getKey
();
List
<
SystemReviewExportVO
>
chapterRecords
=
chapterEntry
.
getValue
();
Map
<
String
,
List
<
SystemReviewExportVO
>>
groupedByReviewStandard
=
chapterRecords
.
stream
()
.
collect
(
Collectors
.
groupingBy
(
SystemReviewExportVO:
:
getReviewStandard
,
Collectors
.
toList
()));
int
startRowForChapter
=
rowIndex
;
for
(
Map
.
Entry
<
String
,
List
<
SystemReviewExportVO
>>
reviewEntry
:
groupedByReviewStandard
.
entrySet
())
{
String
reviewStandard
=
reviewEntry
.
getKey
();
List
<
SystemReviewExportVO
>
standardRecords
=
reviewEntry
.
getValue
();
Map
<
String
,
List
<
SystemReviewExportVO
>>
groupedByKeyPoint
=
standardRecords
.
stream
()
.
collect
(
Collectors
.
groupingBy
(
SystemReviewExportVO:
:
getReviewKeyPoint
,
Collectors
.
toList
()));
int
startRowForStandard
=
rowIndex
;
for
(
Map
.
Entry
<
String
,
List
<
SystemReviewExportVO
>>
keyPointEntry
:
groupedByKeyPoint
.
entrySet
())
{
String
reviewKeyPoint
=
keyPointEntry
.
getKey
();
List
<
SystemReviewExportVO
>
keyPointRecords
=
keyPointEntry
.
getValue
();
Map
<
String
,
List
<
SystemReviewExportVO
>>
groupedByDetails
=
keyPointRecords
.
stream
()
.
collect
(
Collectors
.
groupingBy
(
SystemReviewExportVO:
:
getReviewDetails
,
Collectors
.
toList
()));
int
startRowForKeyPoint
=
rowIndex
;
for
(
Map
.
Entry
<
String
,
List
<
SystemReviewExportVO
>>
detailsEntry
:
groupedByDetails
.
entrySet
())
{
String
reviewDetails
=
detailsEntry
.
getKey
();
List
<
SystemReviewExportVO
>
detailRecords
=
detailsEntry
.
getValue
();
int
startRowForDetails
=
rowIndex
;
for
(
SystemReviewExportVO
record
:
detailRecords
)
{
Row
row
=
sheet
.
createRow
(
rowIndex
++);
// 设置审查场景
Cell
sceneCell
=
row
.
createCell
(
4
);
sceneCell
.
setCellValue
(
record
.
getReviewScene
());
// 设置自动换行
// CellStyle wrapStyle = sheet.getWorkbook().createCellStyle();
// wrapStyle.setWrapText(true);
sceneCell
.
setCellStyle
(
getCenteredWrapTextStyle
(
sheet
.
getWorkbook
()));
}
if
(
detailRecords
.
size
()
>
1
)
{
// sheet.addMergedRegion(new CellRangeAddress(startRowForDetails, rowIndex - 1, 3, 3));
CellRangeAddress
detailsRange
=
new
CellRangeAddress
(
startRowForDetails
,
rowIndex
-
1
,
3
,
3
);
sheet
.
addMergedRegion
(
detailsRange
);
setRegionStyle
(
sheet
,
detailsRange
,
getCenteredWrapTextStyle
(
sheet
.
getWorkbook
()));
}
// 设置审查细则列
Row
detailsRow
=
sheet
.
getRow
(
startRowForDetails
);
Cell
detailsCell
=
detailsRow
.
createCell
(
3
);
detailsCell
.
setCellValue
(
reviewDetails
);
detailsCell
.
setCellStyle
(
getCenteredWrapTextStyle
(
sheet
.
getWorkbook
()));
// 应用自动换行样式
}
if
(
keyPointRecords
.
size
()
>
1
)
{
// sheet.addMergedRegion(new CellRangeAddress(startRowForKeyPoint, rowIndex - 1, 2, 2));
CellRangeAddress
keyPointRange
=
new
CellRangeAddress
(
startRowForKeyPoint
,
rowIndex
-
1
,
2
,
2
);
sheet
.
addMergedRegion
(
keyPointRange
);
setRegionStyle
(
sheet
,
keyPointRange
,
getCenteredWrapTextStyle
(
sheet
.
getWorkbook
()));
}
Row
keyPointRow
=
sheet
.
getRow
(
startRowForKeyPoint
);
Cell
keyPointCell
=
keyPointRow
.
createCell
(
2
);
keyPointCell
.
setCellValue
(
reviewKeyPoint
);
keyPointCell
.
setCellStyle
(
getCenteredWrapTextStyle
(
sheet
.
getWorkbook
()));
// 应用自动换行样式
}
if
(
standardRecords
.
size
()
>
1
)
{
// sheet.addMergedRegion(new CellRangeAddress(startRowForStandard, rowIndex - 1, 1, 1));
CellRangeAddress
standardRange
=
new
CellRangeAddress
(
startRowForStandard
,
rowIndex
-
1
,
1
,
1
);
sheet
.
addMergedRegion
(
standardRange
);
setRegionStyle
(
sheet
,
standardRange
,
getCenteredWrapTextStyle
(
sheet
.
getWorkbook
()));
}
Row
standardRow
=
sheet
.
getRow
(
startRowForStandard
);
Cell
standardCell
=
standardRow
.
createCell
(
1
);
standardCell
.
setCellValue
(
reviewStandard
);
standardCell
.
setCellStyle
(
getCenteredWrapTextStyle
(
sheet
.
getWorkbook
()));
// 应用自动换行样式
}
if
(
chapterRecords
.
size
()
>
1
)
{
// sheet.addMergedRegion(new CellRangeAddress(startRowForChapter, rowIndex - 1, 0, 0));
CellRangeAddress
chapterRange
=
new
CellRangeAddress
(
startRowForChapter
,
rowIndex
-
1
,
0
,
0
);
sheet
.
addMergedRegion
(
chapterRange
);
setRegionStyle
(
sheet
,
chapterRange
,
getCenteredWrapTextStyle
(
sheet
.
getWorkbook
()));
}
Row
chapterRow
=
sheet
.
getRow
(
startRowForChapter
);
Cell
chapterCell
=
chapterRow
.
createCell
(
0
);
chapterCell
.
setCellValue
(
chapter
);
chapterCell
.
setCellStyle
(
getCenteredWrapTextStyle
(
sheet
.
getWorkbook
()));
// 应用自动换行样式
}
// 自动调整列宽以适应内容
for
(
int
i
=
0
;
i
<
5
;
i
++)
{
sheet
.
autoSizeColumn
(
i
);
if
(
i
==
2
){
sheet
.
setColumnWidth
(
1
,
20
*
256
);
}
}
}
// 设置表格样式
private
static
CellStyle
getCenteredWrapTextStyle
(
Workbook
workbook
)
{
CellStyle
centeredWrapTextStyle
=
workbook
.
createCellStyle
();
centeredWrapTextStyle
.
setWrapText
(
true
);
// 设置自动换行
centeredWrapTextStyle
.
setAlignment
(
HorizontalAlignment
.
LEFT
);
// 水平居中
centeredWrapTextStyle
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
// 垂直居中
// 设置黑色边框
centeredWrapTextStyle
.
setBorderTop
(
BorderStyle
.
THIN
);
centeredWrapTextStyle
.
setBorderBottom
(
BorderStyle
.
THIN
);
centeredWrapTextStyle
.
setBorderLeft
(
BorderStyle
.
THIN
);
centeredWrapTextStyle
.
setBorderRight
(
BorderStyle
.
THIN
);
// 设置边框颜色为黑色
centeredWrapTextStyle
.
setTopBorderColor
(
IndexedColors
.
BLACK
.
getIndex
());
centeredWrapTextStyle
.
setBottomBorderColor
(
IndexedColors
.
BLACK
.
getIndex
());
centeredWrapTextStyle
.
setLeftBorderColor
(
IndexedColors
.
BLACK
.
getIndex
());
centeredWrapTextStyle
.
setRightBorderColor
(
IndexedColors
.
BLACK
.
getIndex
());
return
centeredWrapTextStyle
;
}
// 设置每个单元格样式
private
static
void
setRegionStyle
(
Sheet
sheet
,
CellRangeAddress
region
,
CellStyle
style
)
{
for
(
int
row
=
region
.
getFirstRow
();
row
<=
region
.
getLastRow
();
row
++)
{
Row
sheetRow
=
sheet
.
getRow
(
row
);
if
(
sheetRow
==
null
)
{
sheetRow
=
sheet
.
createRow
(
row
);
}
for
(
int
col
=
region
.
getFirstColumn
();
col
<=
region
.
getLastColumn
();
col
++)
{
Cell
cell
=
sheetRow
.
getCell
(
col
);
if
(
cell
==
null
)
{
cell
=
sheetRow
.
createCell
(
col
);
}
cell
.
setCellStyle
(
style
);
// 设置每个单元格的样式
}
}
}
/**
* 创建列表类型数据对应的Excel表单
*
* @param sheet 表单
* @param dataList 数据列表
*/
private
static
void
createSheetWithListData
(
Sheet
sheet
,
List
<?>
dataList
)
{
if
(
CollUtil
.
isNotEmpty
(
dataList
))
{
Object
firstItem
=
dataList
.
get
(
0
);
createHeaderRow
(
sheet
,
firstItem
.
getClass
());
int
rowIndex
=
1
;
for
(
Object
item
:
dataList
)
{
createDataRow
(
sheet
,
item
,
rowIndex
++);
}
}
}
/**
* 创建对象类型数据对应的Excel表单
*
* @param sheet 表单
* @param data 数据
*/
private
static
void
createSheetWithObjectData
(
Sheet
sheet
,
Object
data
)
{
createHeaderRow
(
sheet
,
data
.
getClass
());
createDataRow
(
sheet
,
data
,
1
);
}
/**
* 创建表头行
*
* @param sheet 表单
* @param clazz 数据类
*/
private
static
void
createHeaderRow
(
Sheet
sheet
,
Class
<?>
clazz
)
{
// 创建单元格样式
CellStyle
headerCellStyle
=
createCellStyle
(
sheet
.
getWorkbook
());
headerCellStyle
.
setFillForegroundColor
(
IndexedColors
.
GREY_50_PERCENT
.
getIndex
());
// 填充效果(全景填充)
headerCellStyle
.
setFillPattern
(
FillPatternType
.
SOLID_FOREGROUND
);
headerCellStyle
.
setAlignment
(
HorizontalAlignment
.
CENTER
);
headerCellStyle
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
// 创建标题行
Row
headerRow
=
sheet
.
createRow
(
0
);
Field
[]
fields
=
clazz
.
getDeclaredFields
();
for
(
int
i
=
0
;
i
<
fields
.
length
;
i
++)
{
createHeaderCell
(
sheet
,
headerCellStyle
,
fields
,
headerRow
,
i
);
}
}
/**
* 创建数据行
*
* @param sheet 表单
* @param data 数据
* @param rowIndex 行号
*/
private
static
void
createDataRow
(
Sheet
sheet
,
Object
data
,
int
rowIndex
)
{
// 创建单元格样式
CellStyle
dataCellStyle
=
createCellStyle
(
sheet
.
getWorkbook
());
// 创建数据行
Row
dataRow
=
sheet
.
createRow
(
rowIndex
);
sheet
.
setColumnWidth
(
1
,
25
*
256
);
sheet
.
setColumnWidth
(
2
,
25
*
256
);
sheet
.
setColumnWidth
(
3
,
25
*
256
);
Field
[]
fields
=
data
.
getClass
().
getDeclaredFields
();
for
(
int
i
=
0
;
i
<
fields
.
length
;
i
++)
{
createDataCell
(
dataCellStyle
,
fields
,
dataRow
,
i
,
data
);
}
}
/**
* 创建单元格样式
*
* @param workbook 工作簿
* @return 单元格样式
*/
private
static
CellStyle
createCellStyle
(
Workbook
workbook
)
{
CellStyle
cellStyle
=
workbook
.
createCellStyle
();
// 设置自动换行;
cellStyle
.
setWrapText
(
true
);
// 设置 水平和垂直 居中对齐
cellStyle
.
setAlignment
(
HorizontalAlignment
.
CENTER
);
cellStyle
.
setVerticalAlignment
(
VerticalAlignment
.
CENTER
);
// 设置 上 下 左 右 边框及颜色
cellStyle
.
setBorderTop
(
BorderStyle
.
THIN
);
cellStyle
.
setTopBorderColor
(
IndexedColors
.
GREY_50_PERCENT
.
getIndex
());
cellStyle
.
setBorderBottom
(
BorderStyle
.
THIN
);
cellStyle
.
setBottomBorderColor
(
IndexedColors
.
GREY_50_PERCENT
.
getIndex
());
cellStyle
.
setBorderLeft
(
BorderStyle
.
THIN
);
cellStyle
.
setLeftBorderColor
(
IndexedColors
.
GREY_50_PERCENT
.
getIndex
());
cellStyle
.
setBorderRight
(
BorderStyle
.
THIN
);
cellStyle
.
setRightBorderColor
(
IndexedColors
.
GREY_50_PERCENT
.
getIndex
());
// 设置字体
Font
dataFont
=
workbook
.
createFont
();
dataFont
.
setFontName
(
"Arial"
);
dataFont
.
setFontHeightInPoints
((
short
)
10
);
cellStyle
.
setFont
(
dataFont
);
return
cellStyle
;
}
/**
* 创建Excel表头单元格
*
* @param sheet 表单
* @param headerCellStyle 单元格样式
* @param fields 字段
* @param headerRow 标题行
* @param i 序号
*/
private
static
void
createHeaderCell
(
Sheet
sheet
,
CellStyle
headerCellStyle
,
Field
[]
fields
,
Row
headerRow
,
int
i
)
{
// 默认宽度
double
width
=
16
;
Excel
excelAnnotation
=
fields
[
i
].
getAnnotation
(
Excel
.
class
);
if
(
excelAnnotation
!=
null
&&
!
ObjectUtil
.
isEmpty
(
excelAnnotation
.
width
()))
{
width
=
excelAnnotation
.
width
();
}
// 设置宽度
sheet
.
setColumnWidth
(
i
,
(
int
)
((
width
+
0.72
)
*
256
));
if
(
excelAnnotation
!=
null
)
{
Cell
cell
=
headerRow
.
createCell
(
i
);
cell
.
setCellValue
(
excelAnnotation
.
name
());
cell
.
setCellStyle
(
headerCellStyle
);
}
}
/**
* 创建Excel数据单元格
*
* @param dataCellStyle 单元格样式
* @param fields 字段
* @param dataRow 数据行
* @param i 序号
* @param data 数据
*/
private
static
void
createDataCell
(
CellStyle
dataCellStyle
,
Field
[]
fields
,
Row
dataRow
,
int
i
,
Object
data
)
{
Cell
cell
=
dataRow
.
createCell
(
i
);
cell
.
setCellStyle
(
dataCellStyle
);
try
{
fields
[
i
].
setAccessible
(
true
);
Object
value
=
fields
[
i
].
get
(
data
);
handleAnnotationAndSetValue
(
cell
,
fields
[
i
],
value
);
}
catch
(
IllegalAccessException
e
)
{
e
.
printStackTrace
();
}
}
/**
* 处理注解并设置单元格值
*
* @param cell 单元格
* @param field 字段
* @param value 值
*/
private
static
void
handleAnnotationAndSetValue
(
Cell
cell
,
Field
field
,
Object
value
)
{
if
(
field
.
isAnnotationPresent
(
Excel
.
class
)
&&
field
.
getAnnotation
(
Excel
.
class
).
dictType
().
length
()
>
0
)
{
value
=
DictUtils
.
getDictLabel
(
field
.
getAnnotation
(
Excel
.
class
).
dictType
(),
String
.
valueOf
(
value
));
}
if
(
field
.
isAnnotationPresent
(
Excel
.
class
)
&&
StrUtil
.
isNotEmpty
(
field
.
getAnnotation
(
Excel
.
class
).
dateFormat
()))
{
value
=
DateUtil
.
format
(
Convert
.
convert
(
Date
.
class
,
value
),
field
.
getAnnotation
(
Excel
.
class
).
dateFormat
());
}
cell
.
setCellValue
(
ObjectUtil
.
isEmpty
(
value
)
?
null
:
value
.
toString
());
}
/**
* 设置响应头
*
* @param response 响应
* @param excelFileName 文件名
* @param suffixName 后缀名
* @throws UnsupportedEncodingException 编码异常
*/
private
static
void
setResponseHeader
(
HttpServletResponse
response
,
String
excelFileName
,
String
suffixName
)
throws
UnsupportedEncodingException
{
response
.
setContentType
(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
);
response
.
setHeader
(
"Content-Disposition"
,
"attachment; filename="
+
URLEncoder
.
encode
(
excelFileName
+
suffixName
,
UTF_8
));
}
}
quality-review/src/main/java/com/ruoyi/web/TestPlanRecordController.java
View file @
a52ce7b7
...
...
@@ -3,11 +3,12 @@ package com.ruoyi.web;
import
cn.hutool.core.date.DateUtil
;
import
com.alibaba.fastjson2.JSON
;
import
com.ruoyi.common.ExcelMultipleSheetsUtil
;
import
com.ruoyi.common.annotation.Log
;
import
com.ruoyi.common.core.domain.R
;
import
com.ruoyi.common.core.page.TableDataInfo
;
import
com.ruoyi.common.enums.BusinessType
;
import
com.ruoyi.common.utils.poi.ExcelMultipleSheetsUtil
;
//
import com.ruoyi.common.utils.poi.ExcelMultipleSheetsUtil;
import
com.ruoyi.common.utils.poi.ExcelUtil
;
import
com.ruoyi.domain.ReviewStandard
;
import
com.ruoyi.domain.TTestPlanRecord
;
...
...
quality-review/src/main/resources/mapper/TTestPlanRecordMapper.xml
View file @
a52ce7b7
...
...
@@ -87,5 +87,9 @@
LEFT JOIN t_review_details trd on trd.review_keypoint_id = trk.id
LEFT JOIN t_review_scene trsc on trsc.review_details_id = trd.id
WHERE standard_id = #{standardId} AND type = #{type}
ORDER BY
CAST(SUBSTRING_INDEX(trs.chapter, '.', 1) AS UNSIGNED),
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(trs.chapter, '.', 2), '.', -1) AS UNSIGNED),
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(trs.chapter, '.', 3), '.', -1) AS UNSIGNED)
</select>
</mapper>
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