在之前的一篇中,简述了如何导出简单的exce表格。但是,有些时候我们需要导出一些带有合并单元格的数据。如以下所示:
依赖导入
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
实体定义
标题栏注解
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelTitle {
/**
* 标题名称
* @return 默认空
*/
String titleName() ;
/**
* 标题背景
* @return 默认空
*/
IndexedColors titleBack() default IndexedColors.WHITE;
/**
* 标题文字大小
* @return 默认空
*/
short titleSize() default 14;
/**
* 标题文字颜色
* @return 黑色
*/
HSSFColor.HSSFColorPredefined titleColor() default HSSFColor.HSSFColorPredefined.BLACK;
/**
* 边框格式
* @return 细
*/
BorderStyle borderStyle() default BorderStyle.THIN;
/**
* 边框颜色
* @return 默认
*/
IndexedColors borderColor() default IndexedColors.AUTOMATIC;
/**
* 标题文字加粗
* @return 黑色
*/
boolean boldFont() default true;
/**
* 是否忽略
* @return 黑色
*/
boolean ignore() default false;
/**
* 排序
* @return 0
*/
int order() default 0;
}
数据栏注解
/**
* @author ZSC
* @date 2024/7/19 - 9:33
* @description: excel导出结果配置
*/
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelProperty {
/**
* 背景
* @return 默认空
*/
IndexedColors textBack() default IndexedColors.WHITE;
/**
* 内容类型,查看
* @link org.apache.poi.ss.usermodel.BuiltinFormats
* @return 默认TEXT
*/
String textType() default "TEXT";
/**
* 文字大小
* @return 默认18
*/
short textSize() default 12;
/**
* 数据属于key:value时,可以自定义转换,配置格式为:key1=value;key2=value2;.....
* @return 默认空
*/
String textKv() default "";
/**
* 文字颜色
* @return 黑色
*/
HSSFColor.HSSFColorPredefined textColor() default HSSFColor.HSSFColorPredefined.BLACK;
/**
* 水平位置
* @return 水平居中
*/
HorizontalAlignment horizontal() default HorizontalAlignment.CENTER;
/**
* 垂直位置
* @return 垂直居中
*/
VerticalAlignment vertical() default VerticalAlignment.CENTER;
/**
* 文字加粗
* @return 不加粗
*/
boolean boldFont() default false;
}
合并单元格实体定义
/**
* @date 2024/7/23 - 16:24
* @description: excel单元格合并数据定义实体
*/
@Data
public class ExcelMergeVo {
/**
* 默认的单元配置
*/
private ExcelCellStyleDto excelCellStyleDto;
/**
* 水平占据的单元各数量,默认1
*/
private int cellHorizontalSize = 1;
/**
* 占据的单元格数量,默认1
*/
private int cellVerticalSize = 1;
@ExcelProperty()
private String value;
}
合并单元格样式实体
@Data
public class ExcelCellStyleDto {
/**
* 背景 默认空
*/
private IndexedColors textBack = IndexedColors.WHITE;
/**
* 内容类型,查看 默认TEXT
* @link org.apache.poi.ss.usermodel.BuiltinFormats
*/
private String textType = "TEXT";
/**
* 文字大小 默认18
*/
private short textSize = 12;
/**
* 文字颜色 默认 黑色
*/
private HSSFColor.HSSFColorPredefined textColor = HSSFColor.HSSFColorPredefined.BLACK;
/**
* 水平位置 默认 水平居中
*/
private HorizontalAlignment horizontal = HorizontalAlignment.CENTER;
/**
* 垂直位置 垂直居中
*/
private VerticalAlignment vertical = VerticalAlignment.CENTER;
/**
* 文字加粗 默认 不加粗
*/
private boolean boldFont = true;
/**
* 边框
*/
private BorderStyle borderStyle;
}
每个sheet的数据定义
@Data
public class ExcelSheetComplexVo {
/**
* 每一个sheet页得名称
*/
private String sheetName;
/**
* 每个sheet里面得数据
* 其中Object中得注解必须时包含 @ExcelTitle 和 @ExcelProperties
*/
private List<ExcelComplexVo> sheetData;
/**
* 数据对象得类型
*/
private Class dataClass;
}
@Data
public class ExcelComplexVo<T> {
/**
* 需要合并的单元格信息处理信息,需要排序好
*/
private List<ExcelMergeVo> orderExcelMergeVos;
/**
* 每个正常的数据
* 其中Object中得注解必须时包含 @ExcelTitle 和 @ExcelProperties
*/
private List<T> subData;
}
数据生成实现
合并行主要代码
/**
* 生成每一个sheet数据
* @param wb 问价实体
* @param sxssfSheet 每个sheet
* @param excelSheetComplexVo 每个sheet的数据定义
* @param dataStartLine 起始行数据
*/
private void initSheetData(SXSSFWorkbook wb, SXSSFSheet sxssfSheet, ExcelSheetComplexVo excelSheetComplexVo, int dataStartLine) {
if (CollectionUtils.isEmpty(excelSheetComplexVo.getSheetData())) {
return;
}
try {
for (ExcelComplexVo excelComplexVo : excelSheetComplexVo.getSheetData()) {
if (CollectionUtils.isNotEmpty(excelComplexVo.getOrderExcelMergeVos())) {
//是否有合并的行数据
dataStartLine = createMergeRow(wb, sxssfSheet, excelComplexVo.getOrderExcelMergeVos(), dataStartLine);
}
for (Object dataObject : excelComplex