/** * 合并列单元格 * @param xfSheet 工作表对象 * @param column 需要合并的列 * @param row 从第row行开始合并 * @param begin 跳过空白行开始的行数 */ public void mergedColumn(Sheet xfSheet,int column,int row,int begin){ int regionStart = 1; Map<Integer,Integer> map = new LinkedHashMap<>(); List<Map<Integer,Integer>> list = new ArrayList<>(); for (int i = begin;i<xfSheet.getPhysicalNumberOfRows();i++){ if (i<xfSheet.getLastRowNum()){ String secondIndex = xfSheet.getRow(i).getCell(column).getStringCellValue(); String secondIndex2 = xfSheet.getRow(i+1).getCell(column).getStringCellValue(); if (!secondIndex.equals(secondIndex2)){ map.put(regionStart,i); regionStart=i+1; } }else if (i==xfSheet.getLastRowNum()){ map.put(regionStart,i); regionStart=i+1; } } list.add(map); for (Map<Integer,Integer> rmap : list){ for (Integer i : rmap.keySet()){ if (i >= row) { if (!i.equals(rmap.get(i))) { xfSheet.addMergedRegion(new CellRangeAddress(i, rmap.get(i), column, column)); } } } } }