How to modify the cell value of a table in an pptx

2019-06-07 13:51发布

问题:

I'm trying to modifify the cell value of a table within a pptx file. Saving the file, the modification is not applied.

Here is the used code:

FileInputStream is = new FileInputStream("C:/Report_Template.pptx");
XMLSlideShow ppt = new XMLSlideShow(is);
is.close();

ppt.getPageSize();
for(XSLFSlide slide : ppt.getSlides()) {
    for(XSLFShape shape : slide){
        shape.getAnchor();
        if (shape instanceof XSLFTable){
            XSLFTable t = (XSLFTable) shape;
            List<XSLFTableRow> r = t.getRows();
            for (int i = 1; i < r.size(); i++) {
                String text = r.get(i).getCells().get(1).getText();
                if(text.contains("#ID")) {
                    r.get(i).getCells().get(1).setText("20131028152343");
                }
            }
        }
    }
}
FileOutputStream out = new FileOutputStream("C:/Report.pptx");
ppt.write(out);
out.close();

The file C:/Report.pptx does not contain the string "20131028152343" but "#ID". Could someone help me? Thanks in advanced, Meg

回答1:

I had the same issue with tables (with POI 3.10): I could not modify them, and sometimes, the file was corrupted (I could not open it with LibreOffice).

I have just replaced the jar poi-ooxml-schemas-*.jar with ooxml-schemas-1.1.jar (you can find it on Maven Central) in my build path, and it works now.