导出样式
测试数据
创建测试数据SQL
CREATE TABLE ` test_export` (
` id` bigint ( 0 ) NOT NULL AUTO_INCREMENT COMMENT 'id' ,
` sort` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '排序' ,
` goal_content` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '内容' ,
` kr_sort` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 'kr排序' ,
` kr_content` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT 'kr内容' ,
` cycle` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '结束时间' ,
` level` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '优先级' ,
` expect_type` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '信心指数' ,
` block` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '是否阻塞(1正常推进、2需要帮助)' ,
` last_block` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '上次复盘是否阻塞(1正常推进、2需要帮助)' ,
` last_score` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '上次评分' ,
` score` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '本次评分' ,
` last_replay_content` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '上次复盘内容' ,
` replay_content` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '本次复盘内容' ,
` duty_name` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '负责人' ,
PRIMARY KEY ( ` id` ) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
INSERT INTO ` test_export` VALUES ( 1 , '1' , '目标1' , '1' , '目标1_KR1' , '2022-08-01' , '1' , '5' , '1' , '2' , '0.1' , '0.5' , '目标1_KR1_上次复盘内容' , '目标1_KR1_本次复盘内容' , '目标1_KR1_负责人' ) ;
INSERT INTO ` test_export` VALUES ( 2 , '1' , '目标1' , '2' , '目标1_KR2' , '2023-02-07' , '2' , '4' , '1' , '2' , '0.2' , '0.6' , '目标1_KR2_上次复盘内容' , '目标1_KR2_本次复盘内容' , '目标1_KR2_负责人' ) ;
INSERT INTO ` test_export` VALUES ( 3 , '1' , '目标1' , '3' , '目标1_KR3' , '2023-04-13' , '3' , '3' , '2' , '2' , '0.3' , '0.7' , '目标1_KR3_上次复盘内容' , '目标1_KR3_本次复盘内容' , '目标1_KR3_负责人' ) ;
INSERT INTO ` test_export` VALUES ( 4 , '1' , '目标1' , '4' , '目标1_KR4' , '2023-05-17' , '4' , '2' , '2' , '1' , '0.4' , '0.8' , '目标1_KR4_上次复盘内容' , '目标1_KR4_本次复盘内容' , '目标1_KR4_负责人' ) ;
INSERT INTO ` test_export` VALUES ( 5 , '2' , '目标2' , '1' , '目标2_KR1' , '2023-06-01' , '5' , '1' , '1' , '1' , '0.5' , '0.9' , '目标2_KR1_上次复盘内容' , '目标2_KR1_本次复盘内容' , '目标2_KR1_负责人' ) ;
INSERT INTO ` test_export` VALUES ( 6 , '2' , '目标2' , '2' , '目标2_KR2' , '2023-06-27' , '1' , '5' , '1' , '2' , '0.6' , '1' , '目标2_KR2_上次复盘内容' , '目标2_KR2_本次复盘内容' , '目标2_KR2_负责人' ) ;
INSERT INTO ` test_export` VALUES ( 7 , '2' , '目标2' , '3' , '目标2_KR3' , '2023-06-28' , '2' , '4' , '1' , '2' , '0.4' , '0.7' , '目标2_KR3_上次复盘内容' , '目标2_KR3_本次复盘内容' , '目标2_KR3_负责人' ) ;
INSERT INTO ` test_export` VALUES ( 8 , '2' , '目标2' , '4' , '目标2_KR4' , '2023-07-01' , '3' , '3' , '2' , '1' , '0.6' , '0.8' , '目标2_KR4_上次复盘内容' , '目标2_KR4_本次复盘内容' , '目标2_KR4_负责人' ) ;
INSERT INTO ` test_export` VALUES ( 9 , '3' , '目标3' , '1' , '目标3_KR1' , '2023-07-31' , '4' , '2' , '1' , '1' , '0.3' , '0.9' , '目标3_KR1_上次复盘内容' , '目标3_KR1_本次复盘内容' , '目标3_KR1_负责人' ) ;
INSERT INTO ` test_export` VALUES ( 10 , '3' , '目标3' , '2' , '目标3_KR2' , '2023-08-01' , '5' , '1' , '1' , '1' , '0.7' , '1' , '目标3_KR2_上次复盘内容' , '目标3_KR2_本次复盘内容' , '目标3_KR2_负责人' ) ;
INSERT INTO ` test_export` VALUES ( 11 , '3' , '目标3' , '3' , '目标3_KR3' , '2023-08-01' , '1' , '5' , '2' , '1' , '0.1' , '0.5' , '目标3_KR3_上次复盘内容' , '目标3_KR3_本次复盘内容' , '目标3_KR3_负责人' ) ;
INSERT INTO ` test_export` VALUES ( 12 , '4' , '目标4' , '1' , '目标4_KR1' , '2023-08-01' , '2' , '4' , '2' , '1' , '0.2' , '0.6' , '目标4_KR1_上次复盘内容' , '目标4_KR1_本次复盘内容' , '目标4_KR1_负责人' ) ;
INSERT INTO ` test_export` VALUES ( 13 , '4' , '目标4' , '2' , '目标4_KR2' , '2023-08-01' , '3' , '3' , '1' , '2' , '0.3' , '0.7' , '目标4_KR2_上次复盘内容' , '目标4_KR2_本次复盘内容' , '目标4_KR2_负责人' ) ;
INSERT INTO ` test_export` VALUES ( 14 , '4' , '目标4' , '3' , '目标4_KR3' , '2023-08-01' , '4' , '2' , '1' , '2' , '0.4' , '0.8' , '目标4_KR3_上次复盘内容' , '目标4_KR3_本次复盘内容' , '目标4_KR3_负责人' ) ;
INSERT INTO ` test_export` VALUES ( 15 , '4' , '目标4' , '4' , '目标4_KR4' , '2023-08-01' , '5' , '1' , '1' , '1' , '0.5' , '0.9' , '目标4_KR4_上次复盘内容' , '目标4_KR4_本次复盘内容' , '目标4_KR4_负责人' ) ;
实现方法
public void exportTest ( HttpServletResponse response) {
List < OkrSubDto > list = baseMapper. getTestList ( ) ;
InputStream inputStream = null ;
OutputStream outputStream = null ;
try {
String fileName = URLEncoder . encode ( "OKR统计表.xlsx" , "UTF-8" ) ;
response. addHeader ( "Content-Disposition" , "attachment;filename=" + fileName) ;
response. setContentType ( "application/vnd.ms-excel" ) ;
inputStream = this . getClass ( ) . getClassLoader ( ) . getResourceAsStream ( "templates/OKR统计表.xlsx" ) ;
assert inputStream != null ;
XSSFWorkbook workbook = new XSSFWorkbook ( inputStream) ;
XSSFSheet sheet = workbook. getSheetAt ( 0 ) ;
workbook. setSheetName ( 0 , "OKR报表" ) ;
outputStream = response. getOutputStream ( ) ;
XSSFFont font = workbook. createFont ( ) ;
font. setFontName ( "仿宋" ) ;
font. setFontHeightInPoints ( ( short ) 12 ) ;
XSSFCellStyle styleCenterVertically = workbook. createCellStyle ( ) ;
styleCenterVertically. setFont ( font) ;
styleCenterVertically. setVerticalAlignment ( VerticalAlignment . CENTER ) ;
styleCenterVertically. setAlignment ( HorizontalAlignment . CENTER ) ;
styleCenterVertically. setWrapText ( false ) ;
styleCenterVertically. setBorderBottom ( BorderStyle . THIN ) ;
styleCenterVertically. setBorderLeft ( BorderStyle . THIN ) ;
styleCenterVertically. setBorderTop ( BorderStyle . THIN ) ;
styleCenterVertically. setBorderRight ( BorderStyle . THIN ) ;
XSSFCellStyle styleLeftVertically = workbook. createCellStyle ( ) ;
styleLeftVertically. setFont ( font) ;
styleLeftVertically. setVerticalAlignment ( VerticalAlignment . CENTER ) ;
styleLeftVertically. setAlignment ( HorizontalAlignment . LEFT ) ;
styleLeftVertically. setWrapText ( false ) ;
styleLeftVertically. setBorderBottom ( BorderStyle . THIN ) ;
styleLeftVertically. setBorderLeft ( BorderStyle . THIN ) ;
styleLeftVertically. setBorderTop ( BorderStyle . THIN ) ;
styleLeftVertically. setBorderRight ( BorderStyle . THIN ) ;
XSSFCellStyle styleGreyPercent = workbook. createCellStyle ( ) ;
styleGreyPercent. setFillForegroundColor ( IndexedColors . GREY_50_PERCENT . getIndex ( ) ) ;
styleGreyPercent. setFillPattern ( FillPatternType . SOLID_FOREGROUND ) ;
styleGreyPercent. setFont ( font) ;
styleGreyPercent. setVerticalAlignment ( VerticalAlignment . CENTER ) ;
styleGreyPercent. setAlignment ( HorizontalAlignment . CENTER ) ;
styleGreyPercent. setWrapText ( false ) ;
styleGreyPercent. setBorderBottom ( BorderStyle . THIN ) ;
styleGreyPercent. setBorderLeft ( BorderStyle . THIN ) ;
styleGreyPercent. setBorderTop ( BorderStyle . THIN ) ;
styleGreyPercent. setBorderRight ( BorderStyle . THIN ) ;
XSSFCellStyle styleGold = workbook. createCellStyle ( ) ;
styleGold. setFillForegroundColor ( IndexedColors . GOLD . getIndex ( ) ) ;
styleGold. setFillPattern ( FillPatternType . SOLID_FOREGROUND ) ;
styleGold. setFont ( font) ;
styleGold. setVerticalAlignment ( VerticalAlignment . CENTER ) ;
styleGold. setAlignment ( HorizontalAlignment . CENTER ) ;
styleGold. setWrapText ( false ) ;
styleGold. setBorderBottom ( BorderStyle . THIN ) ;
styleGold. setBorderLeft ( BorderStyle . THIN ) ;
styleGold. setBorderTop ( BorderStyle . THIN ) ;
styleGold. setBorderRight ( BorderStyle . THIN ) ;
XSSFCellStyle styleYellow = workbook. createCellStyle ( ) ;
styleYellow. setFillForegroundColor ( IndexedColors . YELLOW . getIndex ( ) ) ;
styleYellow. setFillPattern ( FillPatternType . SOLID_FOREGROUND ) ;
styleYellow. setFont ( font) ;
styleYellow. setVerticalAlignment ( VerticalAlignment . CENTER ) ;
styleYellow. setAlignment ( HorizontalAlignment . CENTER ) ;
styleYellow. setWrapText ( false ) ;
styleYellow. setBorderBottom ( BorderStyle . THIN ) ;
styleYellow. setBorderLeft ( BorderStyle . THIN ) ;
styleYellow. setBorderTop ( BorderStyle . THIN ) ;
styleYellow. setBorderRight ( BorderStyle . THIN ) ;
XSSFCellStyle styleLime = workbook. createCellStyle ( ) ;
styleLime. setFillForegroundColor ( IndexedColors . LIME . getIndex ( ) ) ;
styleLime. setFillPattern ( FillPatternType . SOLID_FOREGROUND ) ;
styleLime. setFont ( font) ;
styleLime. setVerticalAlignment ( VerticalAlignment . CENTER ) ;
styleLime. setAlignment ( HorizontalAlignment . CENTER ) ;
styleLime. setWrapText ( false ) ;
styleLime. setBorderBottom ( BorderStyle . THIN ) ;
styleLime. setBorderLeft ( BorderStyle . THIN ) ;
styleLime. setBorderTop ( BorderStyle . THIN ) ;
styleLime. setBorderRight ( BorderStyle . THIN ) ;
XSSFCellStyle styleGreen = workbook. createCellStyle ( ) ;
styleGreen. setFillForegroundColor ( IndexedColors . GREEN . getIndex ( ) ) ;
styleGreen. setFillPattern ( FillPatternType . SOLID_FOREGROUND ) ;
styleGreen. setFont ( font) ;
styleGreen. setVerticalAlignment ( VerticalAlignment . CENTER ) ;
styleGreen. setAlignment ( HorizontalAlignment . CENTER ) ;
styleGreen. setWrapText ( false ) ;
styleGreen. setBorderBottom ( BorderStyle . THIN ) ;
styleGreen. setBorderLeft ( BorderStyle . THIN ) ;
styleGreen. setBorderTop ( BorderStyle . THIN ) ;
styleGreen. setBorderRight ( BorderStyle . THIN ) ;
XSSFCellStyle styleRed = workbook. createCellStyle ( ) ;
styleRed. setFillForegroundColor ( IndexedColors . RED . getIndex ( ) ) ;
styleRed. setFillPattern ( FillPatternType . SOLID_FOREGROUND ) ;
styleRed. setFont ( font) ;
styleRed. setVerticalAlignment ( VerticalAlignment . CENTER ) ;
styleRed. setAlignment ( HorizontalAlignment . CENTER ) ;
styleRed. setWrapText ( false ) ;
styleRed. setBorderBottom ( BorderStyle . THIN ) ;
styleRed. setBorderLeft ( BorderStyle . THIN ) ;
styleRed. setBorderTop ( BorderStyle . THIN ) ;
styleRed. setBorderRight ( BorderStyle . THIN ) ;
String oldGoalId = "" ;
String goalId = "" ;
int index = 0 ;
int currentLastRowIndex = sheet. getLastRowNum ( ) ;
for ( int i = 0 ; i < list. size ( ) ; i++ ) {
OkrSubDto item = list. get ( i) ;
int newRowIndex = currentLastRowIndex + 1 + i;
XSSFRow newRow = sheet. createRow ( newRowIndex) ;
int cellIndex = 0 ;
XSSFCell cellRow = newRow. createCell ( cellIndex++ , Cell . CELL_TYPE_STRING ) ;
cellRow. setCellValue ( item. getSort ( ) ) ;
cellRow. setCellStyle ( styleCenterVertically) ;
XSSFCell cell = newRow. createCell ( cellIndex++ , Cell . CELL_TYPE_STRING ) ;
if ( i != 0 ) {
oldGoalId = list. get ( i - 1 ) . getSort ( ) ;
}
goalId = item. getSort ( ) ;
if ( goalId. equals ( oldGoalId) ) {
index++ ;
} else {
if ( index != 0 ) {
cellRow = newRow. createCell ( 0 , Cell . CELL_TYPE_STRING ) ;
sheet. addMergedRegion ( new CellRangeAddress ( i - index, i, 0 , 0 ) ) ;
cellRow. setCellValue ( item. getSort ( ) ) ;
cellRow. setCellStyle ( styleCenterVertically) ;
sheet. addMergedRegion ( new CellRangeAddress ( i - index, i, 1 , 1 ) ) ;
}
index = 0 ;
}
if ( i == list. size ( ) - 1 ) {
if ( index != 0 ) {
if ( index > 1 ) {
int last = i - index + 1 ;
cellRow = newRow. createCell ( 0 , Cell . CELL_TYPE_STRING ) ;
sheet. addMergedRegion ( new CellRangeAddress ( last, i + 1 , 0 , 0 ) ) ;
cellRow. setCellValue ( item. getSort ( ) ) ;
cellRow. setCellStyle ( styleCenterVertically) ;
sheet. addMergedRegion ( new CellRangeAddress ( last, i + 1 , 1 , 1 ) ) ;
} else {
cellRow = newRow. createCell ( 0 , Cell . CELL_TYPE_STRING ) ;
sheet. addMergedRegion ( new CellRangeAddress ( i, i + index, 0 , 0 ) ) ;
cellRow. setCellValue ( item. getSort ( ) ) ;
cellRow. setCellStyle ( styleCenterVertically) ;
sheet. addMergedRegion ( new CellRangeAddress ( i, i + index, 1 , 1 ) ) ;
}
}
}
cell. setCellValue ( item. getGoalContent ( ) ) ;
cell. setCellStyle ( styleLeftVertically) ;
cell = newRow. createCell ( cellIndex++ , Cell . CELL_TYPE_STRING ) ;
cell. setCellValue ( item. getKrSort ( ) ) ;
cell. setCellStyle ( styleCenterVertically) ;
cell = newRow. createCell ( cellIndex++ , Cell . CELL_TYPE_STRING ) ;
cell. setCellValue ( item. getKrContent ( ) ) ;
cell. setCellStyle ( styleLeftVertically) ;
cell = newRow. createCell ( cellIndex++ , Cell . CELL_TYPE_STRING ) ;
cell. setCellValue ( item. getCycle ( ) ) ;
cell. setCellStyle ( styleLeftVertically) ;
cell = newRow. createCell ( cellIndex++ , Cell . CELL_TYPE_STRING ) ;
cell. setCellValue ( item. getLevel ( ) ) ;
cell. setCellStyle ( styleCenterVertically) ;
cell = newRow. createCell ( cellIndex++ , Cell . CELL_TYPE_STRING ) ;
cell. setCellValue ( item. getExpectType ( ) ) ;
cell. setCellStyle ( styleCenterVertically) ;
cell = newRow. createCell ( cellIndex++ , Cell . CELL_TYPE_STRING ) ;
if ( ObjectUtil . isNotEmpty ( item. getLastScore ( ) ) ) {
cell. setCellValue ( item. getLastScore ( ) ) ;
if ( "-" . equals ( item. getLastScore ( ) ) ) {
cell. setCellStyle ( styleCenterVertically) ;
} else if ( Double . parseDouble ( item. getLastScore ( ) ) == 0 ) {
cell. setCellStyle ( styleGreyPercent) ;
} else if ( Double . parseDouble ( item. getLastScore ( ) ) <= 0.3 ) {
cell. setCellStyle ( styleGold) ;
} else if ( Double . parseDouble ( item. getLastScore ( ) ) <= 0.6 ) {
cell. setCellStyle ( styleYellow) ;
} else if ( Double . parseDouble ( item. getLastScore ( ) ) == 0.7 ) {
cell. setCellStyle ( styleLime) ;
} else {
cell. setCellStyle ( styleGreen) ;
}
if ( ObjectUtil . isNotEmpty ( item. getLastBlock ( ) ) && item. getLastBlock ( ) == 2 ) {
cell. setCellStyle ( styleRed) ;
}
} else {
cell. setCellValue ( "" ) ;
cell. setCellStyle ( styleCenterVertically) ;
}
cell = newRow. createCell ( cellIndex++ , Cell . CELL_TYPE_STRING ) ;
if ( ObjectUtil . isNotEmpty ( item. getScore ( ) ) ) {
cell. setCellValue ( item. getScore ( ) ) ;
if ( "-" . equals ( item. getScore ( ) ) ) {
cell. setCellStyle ( styleCenterVertically) ;
} else if ( Double . parseDouble ( item. getScore ( ) ) == 0 ) {
cell. setCellStyle ( styleGreyPercent) ;
} else if ( Double . parseDouble ( item. getScore ( ) ) <= 0.3 ) {
cell. setCellStyle ( styleGold) ;
} else if ( Double . parseDouble ( item. getScore ( ) ) <= 0.6 ) {
cell. setCellStyle ( styleYellow) ;
} else if ( Double . parseDouble ( item. getScore ( ) ) == 0.7 ) {
cell. setCellStyle ( styleLime) ;
} else {
cell. setCellStyle ( styleGreen) ;
}
if ( ObjectUtil . isNotEmpty ( item. getBlock ( ) ) && item. getBlock ( ) == 2 ) {
cell. setCellStyle ( styleRed) ;
}
} else {
cell. setCellValue ( "" ) ;
cell. setCellStyle ( styleCenterVertically) ;
}
cell = newRow. createCell ( cellIndex++ , Cell . CELL_TYPE_STRING ) ;
cell. setCellValue ( item. getLastReplayContent ( ) ) ;
cell. setCellStyle ( styleLeftVertically) ;
cell = newRow. createCell ( cellIndex++ , Cell . CELL_TYPE_STRING ) ;
cell. setCellValue ( item. getReplayContent ( ) ) ;
cell. setCellStyle ( styleLeftVertically) ;
cell = newRow. createCell ( cellIndex++ , Cell . CELL_TYPE_STRING ) ;
cell. setCellValue ( item. getDutyName ( ) ) ;
cell. setCellStyle ( styleCenterVertically) ;
}
workbook. write ( outputStream) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
try {
if ( inputStream != null ) {
inputStream. close ( ) ;
}
if ( outputStream != null ) {
outputStream. flush ( ) ;
outputStream. close ( ) ;
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
OkrSubDto实体类
package com. ibuild. ti. business. entity ;
import lombok. * ;
import java. io. Serializable ;
@Data
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class OkrSubDto implements Serializable {
private static final long serialVersionUID = 1L ;
private String id;
private String sort;
private String goalContent;
private String krSort;
private String krContent;
private String cycle;
private String level;
private String expectType;
private Integer block;
private Integer lastBlock;
private String lastScore;
private String score;
private String lastReplayContent;
private String replayContent;
private String dutyName;
}
getTestList查询SQL
select
id as id,
sort as sort,
goal_content as goalContent,
kr_sort as krSort,
kr_content as krContent,
cycle as cycle ,
level as level ,
expect_type as expectType,
block as block,
last_block as lastBlock,
last_score as lastScore,
score as score,
last_replay_content as lastReplayContent,
replay_content as replayContent,
duty_name as dutyName
from
test_export
order by id