【问题标题】:How to modify the cell value of a table in an pptx file with apache-poi 3.9?如何使用 apache-poi 3.9 修改 pptx 文件中表格的单元格值?
【发布时间】:2013-11-14 10:34:35
【问题描述】:

我正在尝试修改 pptx 文件中表格的单元格值。 保存文件,修改不生效。

这里是使用的代码:

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

文件C:/Report.pptx 不包含字符串“20131028152343”,而是包含“#ID”。 有人可以帮我吗?

【问题讨论】:

  • 你确定你的 if 语句被触发了吗?如果您在代码运行时打印出 text 的值会发生什么情况,您看到预期的文本了吗?
  • @Gagravarr 我面临同样的问题。是的,if 语句被触发并在 setText() 之后调用 getText() 返回正确的值。
  • 如果将文件写出,然后用 Apache POI 再次读回,POI 会看到更改后的文本吗?即问题是 POI 没有将文本写入文件,还是 POI 以 PowerPoint 没有注意到的方式写入?
  • 不,它没有,所以看起来它没有写入更改。

标签: java apache-poi


【解决方案1】:

我在使用表时遇到了同样的问题(使用 POI 3.10):我无法修改它们,有时文件已损坏(我无法使用 LibreOffice 打开它)。

我刚刚在我的构建路径中将 jar poi-ooxml-schemas-*.jar 替换为 ooxml-schemas-1.1.jar(你可以在 Maven Central 上找到它),它现在可以工作了。

【讨论】:

  • 谢谢!用 ooxml-schemas-1.1.jar 替换 poi-ooxml-schemas-*.jar 有效。
猜你喜欢
  • 1970-01-01
  • 2016-04-26
  • 2023-03-12
  • 1970-01-01
  • 2010-11-10
  • 2012-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多