强大的AsposeWords for java不仅支持创建表格,还支持合并单元格。今天就简明扼要记录下如何实现合并单元格。
大家可以完全套用本文提供的代码,只需要提供开始和结束的单元格即可实现合并,无需理解复杂的过程,真是简洁好用啊。
此代码来自官方网站,运行无误。
先看看合并的效果:
简单三步实现:
(1)设计一个模板,并确定要合并的开始和结束单元格
(2)调用合并单元格mergeCells()函数
(3)输出合并后word文档。
代码如下:
public static void main(String[] args) {
// TODO code application logic here
try{
Document doc = new Document(“template.docx”);
Table table = (Table) doc.getChild(NodeType.TABLE, 1, true); //第2个表格
// We want to merge the range of cells found in between these two cells.
Cell cellStartRange = table.getRows().get(1).getCells().get(0); //第2行第1列