public static void mergeCell(List<QsggDTO> list, Sheet sheet, int[] index){ int id1 = 0; int id2 = 0; int firstRow = 2; int lastRow = 2; QsggDTO qsggDTO = list.get(0); id1 = qsggDTO.getId(); for (int i = 1; i < list.size(); i++) { qsggDTO = list.get(i); id2 = qsggDTO.getId(); if(id1==id2){ lastRow = i+2; }else { if(firstRow < lastRow){ for(int c:index){ sheet.addMergedRegion(new CellRangeAddress(firstRow,lastRow,c,c)); } } id1 = id2; firstRow = lastRow + 1; lastRow = firstRow; } if((i+1)==list.size()){ if(firstRow < lastRow){ for(int c:index){ sheet.addMergedRegion(new CellRangeAddress(firstRow,lastRow,c,c)); } } } } }
调用:
mergeCell(list, sheet, new int[]{0,1,2});
参数
new int[]{0,1,2}
表示合并第1,2,3列。