1. 程式人生 > 其它 >poi生成表格自動合併單元格

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<rowCount;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部落格