Java操作word时单元格合并
1、如下图首先拿到渲染过数据之后的XWPFDocument对象
2、调用方法将第二行到最后一行的第一列进行合并,代码如下:
/** * @Description: 跨行合并 * table要合并单元格的表格 * col要合并哪一列的单元格 * fromRow从哪一行开始合并单元格 * toRow合并到哪一个行 */ public 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); } } XWPFTableCell cell = table.getRow(fromRow).getCell(col); CTTc cttc = cell.getCTTc(); CTTcPr ctPr = cttc.addNewTcPr(); ctPr.addNewVAlign().setVal(STVerticalJc.CENTER); cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER); }