这里用的到easyExcel版本为3.3.4
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.3.4</version>
</dependency>
效果
代码部分
package com.tianyu.test;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.write.style.ContentLoopMerge;
import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import lombok.Data;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.ss.formula.functions.Count;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.junit.Test;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class test{
private String filePath="src/main/resources/file/";
@Test
public void exportExcel(){
String filePath = this.filePath+ File.separator + "exportExcel"+System.currentTimeMillis() + ".xlsx";
EasyExcel.write(filePath)
.head(this.getHead()) // 表格标题
.sheet("测试") // sheet页名称
.registerWriteHandler(setCellStyle()) // 样式
.registerWriteHandler(new CustomMergeStrategy(7, 7, 1,3)) // 合并第8行的2到4列,即合并旁边的B8:D8
.doWrite(this.getData()); // 数据
}
/**
* 表格标题
* @return
*/
private static List<List<String>> getHead(){
List<List<String>> head = new ArrayList<>();
List l1=Lists.newArrayList();
l1.add("合并");
l1.add("序号");
List l2=Lists.newArrayList();
l2.add("合并");
l2.add("年龄");
List l3=Lists.newArrayList();
l3.add("合并");
l3.add("姓名");
List l4=Lists.newArrayList();
l4.add("换行");
head.add(l1);
head.add(l2);
head.add(l3);
head.add(l4);
return head;
}
/**
* 导出数据
* @return
*/
private static List<List<Object>> getData(){
List<List<Object>> dataList = new ArrayList<List<Object>>();
for (int i = 1; i < 6; i++) {
List<Object> data = new ArrayList<>();
data.add(i);
data.add("名称"+i);
data.add(i+10);
data.add("测试自动换行"+i);
dataList.add(data);
}
List<Object> countCol=new ArrayList<>();
countCol.add("合计");
countCol.add("");
countCol.add("");
countCol.add("");
dataList.add(countCol);
return dataList;
}
/**
* 表格样式
* @return
*/
private static HorizontalCellStyleStrategy setCellStyle() {
// 表格标题样式
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
// 设置标题居中对齐
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
// 设置标题颜色
headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
// 标题字体
WriteFont headFont = new WriteFont();
// 设置标题字体大小
headFont.setFontHeightInPoints((short) 12);
headWriteCellStyle.setWriteFont(headFont);
headFont.setColor(IndexedColors.BLACK.getIndex());
//设置自动换行
headWriteCellStyle.setWrapped(true);
// 数据样式
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
// 设置数据居中对齐
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//设置数据自动换行
contentWriteCellStyle.setWrapped(true);
//设置边框样式
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
// 设置数据背景色
contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
return horizontalCellStyleStrategy;
}
// 用于合并的拦截器
public class CustomMergeStrategy extends AbstractRowWriteHandler {
int row1;
int row2;
int col1;
int col2;
CustomMergeStrategy(int row1, int row2, int col1, int col2) {
this.row1 = row1;
this.row2 = row2;
this.col1 = col1;
this.col2 = col2;
}
@Override
public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {
Sheet sheet = writeSheetHolder.getSheet();
CellRangeAddress cellRangeAddress = new CellRangeAddress(
row1, // first row (0-based)
row1, // last row (0-based)
col1, // first column (0-based)
col2 // last column (0-based)
);
sheet.addMergedRegionUnsafe(cellRangeAddress);
}
}
}
参考文献
EasyExcel无映射标题导出_easyexcel导出标题不展示-CSDN博客
apache poi 高版本 3.17后合并单元格的问题_addmergedregionunsafe-CSDN博客