Apache的POI在Word文档中合并单元格从一个表

问题描述:

我需要与细胞的表上的第一和第二行合并。

I need to have a table with the cells on the first and second row merged.

事情是这样的:

表图像(我不能发布图片) HTTP://i.stack.imgur .COM / dAO6j.png

Image of table (I can't post pics) http://i.stack.imgur.com/dAO6j.png

我一直在审查所有与此主题有关的问题,我已经找到了应用网格跨度细胞一些答案,但我无法找到真正解决问题。

I have been reviewing all the questions related to this topic and I have found some answers for applying grid span to the cells, but I couldn't find a real solution.

下面是code我从谷歌和从该网站获得的例子有:

Here is the code I have from examples obtained from google and from this site:

    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();

我从这个code得到的是以下内容:

What I get from this code is the following:

创建(不能张贴图片对不起)表 HTTP形象://i.stack。 imgur.com/HvTpC.png

Image of table created (can't post pics sorry) http://i.stack.imgur.com/HvTpC.png

我试图用 table.getRow(0).removeCell(1)删除额外的细胞; 但它没有工作,我做错了什么?

I tried to remove the "extra" cells with table.getRow(0).removeCell(1); but it didn't work, am I doing something wrong?

这似乎XML已经被同时删除:

It seems xml has to be removed as well:

 XWPFTableCell removed = tableRow.getCell(idx);
 removed.getCTTc().newCursor().removeXml();
 tableRow.removeCell(idx);