首页 文章

在Apache POI中,有没有办法通过id id来访问XWPF元素?

提问于
浏览
0

我有word文档(它是基于docx和xml),我想找到一个表并以编程方式填充它 . 我正在使用Apache POI,XWPF API .

Is there a way to access XWPF elements by their id?

如何在XWPF元素之间创建唯一性然后使用java进行更改?

谢谢

1 回答

  • 1

    我实现的是一个查找替换功能(from here);

    在我的模板docx文件中,我正在使用“id like texts”,__ heading1 _ subjectname__,然后使用下面的代码替换它们 . 对于表@ axel-richters解决方案可能是合适的 .

    private void findReplace(String a, String b, CustomXWPFDocument document){
        for (XWPFParagraph p : document.getParagraphs()) {
            List<XWPFRun> runs = p.getRuns();
            if (runs != null) {
                for (XWPFRun r : runs) {
                    String text = r.getText(0);
                    if (text != null && text.contains(a)) {
                        text = text.replace(a, b);
                        r.setText(text, 0);
                    }
                }
            }
        }
    }
    

相关问题