我是靠谱客的博主 酷炫香菇,这篇文章主要介绍poi word操作之XWPFTable合并单元格,现在分享给大家,希望可以做个参考。

poi word操作之XWPFTable合并单元格

需要注意,要合并的单元格最好不要有其他的合并样式。

跨列合并单元格

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/** * @Description: 跨列合并 * table要合并单元格的表格 * row要合并哪一行的单元格 * fromCell开始合并的单元格 * toCell合并到哪一个单元格 */ 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); } } }

跨行合并单元格

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/** * @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); } } }

备注:合并office可以,wps可能不行。
拓展

最后

以上就是酷炫香菇最近收集整理的关于poi word操作之XWPFTable合并单元格的全部内容,更多相关poi内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部