使用JAVA中的Apache POI合并具有相同值的Excel工作表中的连续单元格

时间:2016-11-21 08:47:12

标签: java apache-poi

您好我使用Apache poi库创建了一个excell表。我想合并具有相同值的连续单元格。我正在使用以下提供的逻辑。但是它没有正常工作。我需要合并下面提供的图像列。 enter image description here

System.out.println("Total rows: " + pageListAcrToDept.size());
            int commonval = 0;
            for (int column = 0; column < 13; column++) {
                for (int rowno = 8; rowno < pageListAcrToDept.size() + 8; rowno++) {
                    for (int j = rowno + 1; j < pageListAcrToDept.size() + 8; j++) {
                        try {
                            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>   " + rowno + " " + j);
                            if (sheet.getRow(rowno).getCell(column).toString().trim()
                                    .equals(sheet.getRow(j).getCell(column).toString().trim())) {
                                System.out.println(sheet.getRow(rowno).getCell(column).toString() + "\n\n i "
                                        + rowno + " j " + j + " column no " + column);
                                commonval = j;
                            } else {
                                if (commonval != 0) {
                                    CellRangeAddress cellRangeAddress = new CellRangeAddress(rowno, commonval,
                                            column, column);
                                    sheet.addMergedRegion(cellRangeAddress);
                                    commonval = 0;
                                }
                                rowno = j;
                                break;
                            }
                        } catch (Exception e) {
                            System.out.println("Exception at i: " + rowno + " j:" + j + e.getMessage());
                        }
                    }
                }
            }

0 个答案:

没有答案