poi 合并word中的表格的单元格时,跨行可以正确合并,跨列合并则失败

poi 合并word中的表格的单元格时,跨行可以正确合并,跨列合并则失败, 以下是合并列的代码


 public  void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {  
        for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {  
            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);  
            }  
        }  
    }  

有知道为什么的吗?

阅读 9.4k
3 个回答

跨列用到的是addNewHMerge(),跨行用到的是addNewVMerge()。我最近也在用poi导出word,仔细观察发现其实是两个不同的方法,要仔细啊兄dei

新手上路,请多包涵

楼主解决了吗?我也遇到同样的问题!!!

新手上路,请多包涵

合并后的列需要设置相应的宽度。
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
后面加上
//cellCount为表格总列数
Integer width = (toCell-fromCell+1)/cellCount*table.getCTTbl().getTblPr().getTblW().getW().intValue();
cell.getCTTc().getTcPr().addNewTcW().setW(BigInteger.valueOf(width));

推荐问题