如何在 Java 的 POI 中使用 XWPFTable 合并单元格(或应用 colspan)? [英] How to merge cells (or apply colspan) using XWPFTable in POI in Java?

查看:77
本文介绍了如何在 Java 的 POI 中使用 XWPFTable 合并单元格(或应用 colspan)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 poi 中创建表格非常简单,但它的教程非常有限,我找不到可以在生成 docx 文件时在表格中创建简单合并单元格的教程.

Creating a table in poi was quite easy but it has very limited tutorials and I cannot find one that can create a simple merged cell in a table in generating a docx file.

推荐答案

如果您已经创建了表格、表格内的行和行内的单元格,您可以将 gridSpan 添加到单元格属性中:

If you have created table, row inside a table and cell inside a row, you can add gridSpan to cell properties:

if (cell.getCTTc().getTcPr() == null) cell.getCTTc().addNewTcPr();
if (cell.getCTTc().getTcPr().getGridSpan() == null) cell.getCTTc().getTcPr().addNewGridSpan();
cell.getCTTc().getTcPr().getGridSpan().setVal(2);

注意:单元格是 org.apache.poi.xwpf.usermodel.XWPFTableCell.

Note: cell is org.apache.poi.xwpf.usermodel.XWPFTableCell.

这篇关于如何在 Java 的 POI 中使用 XWPFTable 合并单元格(或应用 colspan)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆