1

I'm working with Apache POI and I'm trying to automate some tasks with Powerpoint reports. More precisely, I would like to update the data inside a .pptx presentation from code, including tables.

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.

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. There is a addRow method, but I didn't find anything to delete/remove rows.

Do you know how I can achieve that?

Thanks a lot, and best regards!

Community
  • 1
  • 1

1 Answers1

4

Get 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.
Gilsha
  • 14,431
  • 3
  • 32
  • 47
  • Thanks a lot! I'm not familiar with the underlying CT* objects, but your code is clear :) – Romain André-Lovichi Jan 20 '16 at 16:25
  • By the way, could you please tell me more about the `shape.getAnchor();` line? Why do we need it? – Romain André-Lovichi Feb 04 '16 at 10:37
  • 1
    Sorry. That line was added accidentally. Not required in this scenario. – Gilsha Feb 04 '16 at 10:41
  • Hi Gilsha, I'm come here at 2021 and has some question related to this. How can I add a XSLFTable row to the end and KEEP THE FORMAT as the last row? When I call `table.addRow()`, it will give me a new row with fresh format with no fill color, font, font size. In MS Powerpoint, I can just click on 'Insert Row', and a new row will be added with the same format as the last row. – Ryker Tyler Aug 04 '21 at 04:09