【问题标题】:How can I replace a string in the footer of a XWPFDocument using Apache POI如何使用 Apache POI 替换 XWPFDocument 的页脚中的字符串
【发布时间】:2017-07-04 20:45:47
【问题描述】:

我正在使用 Apache POI 替换 word 模板中的文本。我可以在段落和表格中搜索和替换,但我找不到如何替换 XWPFDocument 的页脚中的字符串。

【问题讨论】:

  • 获取页脚,获取页脚的段落,像现在这样替换?

标签: apache apache-poi footer


【解决方案1】:

获取页脚,然后获取它们的段落并替换文本,就像 Gagravarr 写的那样。我使用下面的代码替换文档页脚中的文本。

private void replaceTextInFooter(XWPFDocument doc, String findText, String replaceText) {
    for (XWPFFooter footer : doc.getFooterList()) {
        for (XWPFParagraph paragraph : footer.getParagraphs()) {
            for (XWPFRun run : paragraph.getRuns()) {
                String text = run.text();
                if (text.contains(findText)) {
                    run.setText(replaceText, 0);
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2020-09-10
    • 2014-08-25
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多