小编Jua*_*ora的帖子

Apache POI 合并 Word 文档中表格中的单元格

我需要有一个表格,其中第一行和第二行的单元格合并。

像这样的东西:

桌子图片(我不能发图片)http://i.stack.imgur.com/dAO6j.png

我一直在查看与此主题相关的所有问题,并且找到了将网格跨度应用于单元格的一些答案,但我找不到真正的解决方案。

这是我从谷歌和本网站获得的示例中的代码:

    XWPFDocument document = new XWPFDocument();
    XWPFTable table = document.createTable(7, 2);

    fillTable(table);

    XWPFTableCell cellRow1 = table.getRow(0).getCell(0);
    XWPFTableCell cellRow2 = table.getRow(1).getCell(0);

    cellRow1.getCTTc().addNewTcPr();
    cellRow1.getCTTc().getTcPr().addNewGridSpan();
    cellRow1.getCTTc().getTcPr().getGridSpan().setVal(BigInteger.valueOf(2L));

    cellRow2.getCTTc().addNewTcPr();
    cellRow2.getCTTc().getTcPr().addNewGridSpan();
    cellRow2.getCTTc().getTcPr().getGridSpan().setVal(BigInteger.valueOf(2L));

    FileOutputStream out = new FileOutputStream("Table.docx");
    doc.write(out);
    out.close();
Run Code Online (Sandbox Code Playgroud)

我从这段代码中得到的内容如下:

在此处输入图片说明

我试图删除“额外”的单元格,table.getRow(0).removeCell(1);但没有用,我做错了什么吗?

java apache merge cells apache-poi

5
推荐指数
1
解决办法
8507
查看次数

标签 统计

apache ×1

apache-poi ×1

cells ×1

java ×1

merge ×1