简体   繁体   English

如何使用Apache POI更新XSLFTable(.pptx演示文稿中的表)?

[英]How to update a XSLFTable (table in .pptx presentation) using Apache POI?

I'm working with Apache POI and I'm trying to automate some tasks with Powerpoint reports. 我正在使用Apache POI,并且正在尝试使用Powerpoint报告自动执行某些任务。 More precisely, I would like to update the data inside a .pptx presentation from code, including tables. 更准确地说,我想通过代码(包括表格)更新.pptx演示文稿中的数据。

I've managed to get the XSLFTable objects (thanks to this page : How to modify the cell value of a table in an pptx file with apache-poi 3.9? ), but now I'm trying to update the table structure. 我已经设法获取XSLFTable对象(感谢此页面: 如何使用apache-poi 3.9修改pptx文件中表的单元格值? ),但是现在我试图更新表结构。

Unfortunately, I don't know how to create or remove rows (or columns) in that table. 不幸的是,我不知道如何在该表中创建或删除行(或列)。 The method getRows returns a list, but it seems unmodifiable. 方法getRows返回一个列表,但是它似乎不可修改。 There is a addRow method, but I didn't find anything to delete/remove rows. 有一个addRow方法,但是我没有找到任何要删除/删除行的方法。

Do you know how I can achieve that? 你知道我怎么能做到吗?

Thanks a lot, and best regards! 非常感谢,并致以最诚挚的问候!

Get XSLFTable 获取XSLFTable

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

Add Row and Cell 添加行和单元格

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

Remove Row 删除行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM