我需要一个表格,其中第一行和第二行的单元格合并。
像这样:
表格图片(我无法发布图片)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();
我从这段代码中得到的是:
我尝试使用 table.getRow(0).removeCell(1);
删除“额外”单元格,但没有成功,是我做错了什么吗?
最佳答案
看来 xml 也必须删除:
XWPFTableCell removed = tableRow.getCell(idx);
removed.getCTTc().newCursor().removeXml();
tableRow.removeCell(idx);
关于java - Apache POI 合并 Word 文档中表格中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27209863/