[英]In Apache POI, Is there a way to access XWPF elements by id their id?
I have word document (it is docx and xml based), I want to find a table and populate it programmatically. 我有word文档(基于docx和xml),我想找到一个表并以编程方式填充它。 I am using Apache POI, XWPF API.
我正在使用Apache POI,XWPF API。
Is there a way to access XWPF elements by their id? 有没有一种方法可以通过其ID访问XWPF元素?
How can I create uniqueness between XWPF elements then alter using java? 如何在XWPF元素之间创建唯一性,然后使用Java进行更改?
Thanks 谢谢
What I have implemented is a find replace feature( from here ); 我实现的是一个查找替换功能( 从此处开始 );
In my template docx file I am using "id like texts", __heading1__, __subjectname__, Then replacing with them using code below. 在我的模板docx文件中,我使用的是“ id like texts”,__ heading1 __,__ subjectname__,然后使用下面的代码将其替换。 For tables @axel-richters solution may be suitable.
对于表格,@ 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);
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.