我可以使用这个函数进行垂直合并:
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);
}
}
}
但我不能用类似的功能进行水平合并:
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");
}
}
}
谢谢!