🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
**1. 继承拦截器AbstractMergeStrategy** ```java public class CustomMergeStrategy extends AbstractMergeStrategy { /** * 每个单元格都调用一次 */ @Override public void merge(Sheet sheet, Cell cell, Head head, Integer rowIndex) { if(rowIndex == 1 && cell.getColumnIndex() == 2) { //合并前2行,前3列 CellRangeAddress item = new CellRangeAddress(0, 1, 0, 2); sheet.addMergedRegion(item); } } } ``` **2. 注册拦截器** ```java @Test public void write02() { Integer num = 0; List<List<Integer>> dataList = Lists.newArrayList(); for (int row = 1; row <= 100; row++) { List<Integer> rowData = Lists.newArrayList(); for (int col = 1; col <= 50; col++) { rowData.add(num++); } dataList.add(rowData); } EasyExcel.write("e:/upload/easyexcel/007.xlsx") .sheet("Sheet1") //注册拦截器 .registerWriteHandler(new CustomMergeStrategy()) .doWrite(dataList); } ```