poi生成表格自动合并单元格


直接复制这个工具类即可使用:

/**
 * 合并单元格
 * @author tongyao
 * @param sheet sheet页
 * @param titleColumn 标题占用行
 * @param cellIndex 想要自动合并的列
 */
public static void mergeCell(HSSFSheet sheet,int titleColumn,int cellIndex){
    //多少行
    int rowCount = sheet.getPhysicalNumberOfRows();
    String cellText = "";
    //开始下标
    int startIndex = 0;
    //结束下标
    int endIndex = 0;
    HSSFRow row = null;
    Cell cell = null;
    for(int i = titleColumn;i){
        row = sheet.getRow(i);
        cell = row.getCell(cellIndex);

        if(!cell.getStringCellValue().equals(cellText)){
            if(startIndex != 0){
                endIndex = (i-1);
                sheet.addMergedRegion(new CellRangeAddress(startIndex, endIndex, cellIndex, cellIndex));
            }
            cellText = cell.getStringCellValue();
            startIndex = i;
        }

    }

    endIndex = (rowCount-1);
    sheet.addMergedRegion(new CellRangeAddress(startIndex, endIndex, cellIndex, cellIndex));
}

原文地址:(52条消息) springboot 使用Poi 自定义封装方法 合并excel中的单元格_Tongyao-CSDN博客

相关