When poi merges the cells of a table in word, cross-row merging is correct and cross-column merging fails.

when poi merges the cells of a table in word, cross-row merging can be done correctly, while cross-column merging fails. The following is the code for merging columns


 public  void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {  
        for (int cellIndex = fromCell; cellIndex <= toCell; cellIndexPP) {  
            XWPFTableCell cell = table.getRow(row).getCell(cellIndex);  
            if ( cellIndex == fromCell ) {  
                // The first merged cell is set with RESTART merge value  
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);  
            } else {  
                // Cells which join (merge) the first one, are set with CONTINUE  
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);  
            }  
        }  
    }  

do you know why?

Feb.28,2021

use addNewHMerge (), across columns and addNewVMerge () across rows. Recently, I have also been using poi to export word, and found that it is actually two different methods. To be careful, brother dei


has the landlord solved it? I have the same problem!


the merged column needs to be set to the appropriate width.
cell.getCTTc (). AddNewTcPr (). AddNewHMerge (). SetVal (STMerge.RESTART);
followed by
/ / cellCount is the total number of columns in the table
Integer width = (toCell-fromCell+1) / cellCount*table.getCTTbl (). GetTblPr (). GetTblW (). GetW (). IntValue ();
cell.getCTTc (). GetTcPr (). AddNewTcW (). SetW (BigInteger.valueOf (width));

Menu