I have problem with vertical merging cells if there is multiple merging in same row. For example : I want to merge row(0).getCell(0) with row(1).getCell(0) and row(0).getCell(1) with row(1).getCell(1) its merge only second.
Method which I use
private void mergeCellsV(XWPFTableCell cellStart, XWPFTableCell cellFinish) {
cellStart.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
cellFinish.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
XWPFTableRow row0 = tablePK.getRow(0);
XWPFTableRow row1 = tablePK.getRow(1);
XWPFTableRow row2 = tablePK.getRow(2);
XWPFTableRow row3 = tablePK.getRow(3);
mergeCellsV(row0.getCell(0), row1.getCell(0));
mergeCellsV(row0.getCell(1), row1.getCell(1));
mergeCellsV(row0.getCell(5), row1.getCell(5));
mergeCellsV(row0.getCell(6), row1.getCell(6));
mergeCellsV(row2.getCell(0), row3.getCell(0));
mergeCellsV(row2.getCell(1), row3.getCell(1));
mergeCellsV(row2.getCell(5), row3.getCell(5));
mergeCellsV(row2.getCell(6), row3.getCell(6));
Result :
adding just one merge and works fine mergeCellsV(row2.getCell(6), row3.getCell(6));


The main question while merging table cells, aside the question what merging settings to use as there are multiple possible settings and as there are compatibility issues between different word processing applications, is: What about the cells which get merged? For example if you merge horizontally cell 3 and 4 in a table row having 6 cells, then after that that row only contains 5 cells. And if you then need merging vertically formerly cell 5 of that row with cell 5 of row below, then this will fail, because formerly cell 5 now is cell 4.
Thats why best practice for merging is following:
Following complete code shows this for the example you seems want to create:
Above code is tested using Apache POI version 4.1.1. It produces: