2016-01-20 77 views
0

我正在使用Apache POI,我试图用Powerpoint报告自动执行一些任务。更确切地说,我想更新来自代码的.pptx演示文稿中的数据,包括表格。如何使用Apache POI更新XSLFTable(在.pptx演示文稿中的表格)?

我已经设法获得XSLFTable对象(感谢此页:How to modify the cell value of a table in an pptx file with apache-poi 3.9?),但现在我试图更新表结构。

不幸的是,我不知道如何在该表中创建或删除行(或列)。方法getRows返回一个列表,但它似乎不可修改。有一个addRow方法,但我没有找到任何删除/删除行。

你知道我该怎么做到吗?

非常感谢,并致以最诚挚的问候!

回答

3

获取XSLFTable

XSLFTable t = null; 
for (XSLFShape shape : slide) { 
    if (shape instanceof XSLFTable) { 
     t = (XSLFTable) shape; 
     r = t.getRows(); 
    } 
} 

添加行和单元格

XSLFTableRow titleRow = tbl.addRow(); 
titleRow.setHeight(50); 
XSLFTableCell titleCell1 = titleRow.addCell(); 
XSLFTextParagraph p1 = titleCell1.addNewTextParagraph(); 
p1.setTextAlign(TextAlign.CENTER); 
XSLFTextRun r1 = p1.addNewTextRun(); 
r1.setText("Column title"); 
r1.setBold(true); 
r1.setFontColor(new Color(0, 104, 145)); 
titleCell1.setFillColor(new Color(190, 230, 245)); 
r1.setFontSize(25.0); 
titleCell1.setVerticalAlignment(VerticalAlignment.MIDDLE); 

删除行

t.getCTTable().getTrList().remove(t.getNumberOfRows()-1); //Remove the last row from table. 
+0

非常感谢!我不熟悉底层的CT *对象,但是你的代码是明确的:) –

+0

顺便说一下,你能告诉我更多关于'shape.getAnchor();'行吗?我们为什么需要它? –

+1

对不起。这条线是偶然添加的。在这种情况下不需要。 – Gilsha