2

I get to do a vertically merged using this function:

private static void mergeCellsVertically(XWPFTable table, int col, int      fromRow, int toRow) {
    for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
        XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
        if ( rowIndex == fromRow ) {
            // The first merged cell is set with RESTART merge value
                cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
        } else {
            // Cells which join (merge) the first one, are set with CONTINUE
            cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
        }
    }
}

but I can't do the horizontal merge with similar function:

private static void mergeCellsHorizontally(XWPFTable table, int col, int fromCol, int toCol) {

    for (int colIndex = fromCol; colIndex <= toCol; colIndex++) {

        XWPFTableCell cell = table.getRow(0).getCell(colIndex);

        if ( colIndex == fromCol ) {
            // The first merged cell is set with RESTART merge value
            cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
            cell.setText("hola");
        } else {
            // Cells which join (merge) the first one, are set with CONTINUE
            cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
            cell.setText("adios");
        }
    }
}

Thanks!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
PABLO PEREZ
  • 41
  • 1
  • 7
  • Have you tried doing it at a sheet-level? See this question: https://stackoverflow.com/questions/18716032/merging-cells-in-excel-using-apache-poi#18717238 – Jan Vladimir Mostert Feb 22 '16 at 13:05
  • 1
    Please describe what is the problem. Is an error thrown? If not, what occurs otherwise? This approach does not check if the `CTTc` objects already contain a `TcPr` element. So probably multiple `TcPr` elements will be appended. This will not work. See http://stackoverflow.com/questions/34647624/how-to-colspan-a-table-in-word-with-apache-poi/34663420#34663420 for another approach. – Axel Richter Feb 22 '16 at 13:37
  • You probably need to check how Word stores this in the XML and then do the same with the low-level API of POI – centic Feb 22 '16 at 19:29
  • Currently, the solution with HMerge is working fine, see https://stackoverflow.com/questions/27209863/apache-poi-merge-cells-from-a-table-in-a-word-document – Ermintar Dec 11 '20 at 09:43

1 Answers1

1

The solution is:

  setCellSpan(row.getCell(2), 5);
   CTRow ctRow = row.getCtRow(); 
    CTTc[] ctTcs = new CTTc[4]; // las colunas que quedan
    for(int y = 0; y < ctRow.sizeOfTcArray(); y++) { 
       if(y != 4 && y != 5  && y != 6 && y != 7){ //Las columnas que desaparecen
    ctTcs[y] = ctRow.getTcArray(y);
} }ctRow.setTcArray(ctTcs);  

where:

public static void setCellSpan(XWPFTableCell cell, int span) {
    if (cell.getCTTc().getTcPr() == null) {
        cell.getCTTc().addNewTcPr();
    }
    if (cell.getCTTc().getTcPr().getGridSpan() == null) {
        cell.getCTTc().getTcPr().addNewGridSpan();
    }
    cell.getCTTc().getTcPr().getGridSpan().setVal(BigInteger.valueOf((long) span));
}
PABLO PEREZ
  • 41
  • 1
  • 7