easyexcel合并单元格

导出的excel,相邻行的单元格数据相同时,则合并

效果如下

合并前

在这里插入图片描述

合并后

在这里插入图片描述

代码如下

import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.handler.context.RowWriteHandlerContext;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.List;
**
 * 单元格合并策略
 * 单元格数据跟前一行的单元格数据相同,则合并单元格
 */
public class CellMergeStrategy implements RowWriteHandler {
    /**
     * 前几列需要合并
     */
    private final int mergeColumnIndex;


    public CellMergeStrategy(int mergeColumnIndex) {
        this.mergeColumnIndex = mergeColumnIndex;
    }

    @Override
    public void afterRowDispose(RowWriteHandlerContext context) {
        if (!context.getHead() && context.getRelativeRowIndex() != null) {
            // 获取当前行索引
            Sheet sheet = context.getWriteSheetHolder().getSheet();
            this.cellMerge(sheet, context.getRow(), context.getRowIndex());
        }
    }

    /**
     * 合并相同内容的单元格
     *
     * @param sheet    sheet
     * @param row      当前行
     * @param rowIndex 行索引
     */
    public void cellMerge(Sheet sheet, Row row, Integer rowIndex) {
        for (int col = 0; col < mergeColumnIndex; col++) {
            Cell currentCell = row.getCell(col);
            if (currentCell != null) {
                // 前一行
                int startIndex = rowIndex - 1;
                Row prevRow = sheet.getRow(startIndex);
                // 获取当前单元格的值
                String currentValue = currentCell.getStringCellValue();
                if (prevRow != null) {
                    Cell prevCell = prevRow.getCell(col);
                    // 如果当前列的前一行存在,且值相同,则进行合并操作
                    if (prevCell != null && currentValue.equals(prevCell.getStringCellValue())) {
                        // 需要判断当前列的前一行是否已经合并过,合并过的要先解除合并
                        List<CellRangeAddress> cellRangeAddressList = sheet.getMergedRegions();
                        for (int index = 0; index < cellRangeAddressList.size(); index++) {
                            CellRangeAddress cellRangeAddress = cellRangeAddressList.get(index);
                            if (cellRangeAddress.isInRange(startIndex, col)) {
                                // 解除合并
                                sheet.removeMergedRegion(index);
                                // 重新赋值需要合并的开始行
                                startIndex = cellRangeAddress.getFirstRow();
                            }
                        }
                        // 找到相同值的上一个单元格,进行合并操作
                        CellRangeAddress rangeAddress = new CellRangeAddress(
                                // first row (0-based)
                                startIndex,
                                // last row
                                rowIndex,
                                // first column (0-based)
                                col,
                                // last column
                                col
                        );
                        sheet.addMergedRegion(rangeAddress);
                    }
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值