Apache POI:替换段落文本
我正在使用 Apache POI 从模板生成 docx 文件。似乎没有一种明显的方法来替换段落中的所有文本,并且文档也非常稀缺。现在,我可以通过循环遍历文档的段落,然后循环遍历每个段落的运行,然后循环遍历每个运行的文本来读取文档...这工作得很好,我可以替换运行中文本的内容,但是我的模板占位符(例如:<>)可能会分为多次运行,这使得匹配和替换变得非常复杂。有没有办法设置 XWPFParagraph 的内容?或者至少有一种方法可以删除段落中的所有运行并创建我自己的运行?
这是我到目前为止所拥有的:
public static void main(String[] args) {
InputStream fs = null;
try {
fs = new FileInputStream("C:\\sample1.docx");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
XWPFDocument doc = null;
try {
doc = new XWPFDocument(fs);
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < doc.getParagraphs().length; i++) {
XWPFParagraph paragraph = doc.getParagraphs()[i];
paragraph.getCTP().getRArray().
// This will output the paragraph's contents.
System.out.println(paragraph.getParagraphText());
for (int j = 0; j < paragraph.getCTP().getRArray().length; j++) {
CTR run = paragraph.getCTP().getRArray()[j];
for (int k = 0; k < run.getTArray().length; k++) {
CTText text = run.getTArray()[k];
// This will output the text contents
System.out.println(text.getStringValue());
// And this will set its contents
text.setStringValue("Success!");
}
}
}
try {
doc.write(new FileOutputStream("C:\\output.docx"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I am using Apache POI to generate docx files from a template. There doesn't seem to be an obvious way to replace all text in a paragraph and the documentation is pretty scarce. Right now I am able to read a document by looping through its paragraphs, then looping through each paragraph's runs, then looping through each run's text... This works pretty well and I can replace the contents of a text in a run, but my template placeholders (example: <>) may be split into several runs, which makes it really complicated to match and replace. Is there a way to set the contents of a XWPFParagraph? Or at least a way to zap all runs in a paragraph and create my own runs?
Here is what I have so far:
public static void main(String[] args) {
InputStream fs = null;
try {
fs = new FileInputStream("C:\\sample1.docx");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
XWPFDocument doc = null;
try {
doc = new XWPFDocument(fs);
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < doc.getParagraphs().length; i++) {
XWPFParagraph paragraph = doc.getParagraphs()[i];
paragraph.getCTP().getRArray().
// This will output the paragraph's contents.
System.out.println(paragraph.getParagraphText());
for (int j = 0; j < paragraph.getCTP().getRArray().length; j++) {
CTR run = paragraph.getCTP().getRArray()[j];
for (int k = 0; k < run.getTArray().length; k++) {
CTText text = run.getTArray()[k];
// This will output the text contents
System.out.println(text.getStringValue());
// And this will set its contents
text.setStringValue("Success!");
}
}
}
try {
doc.write(new FileOutputStream("C:\\output.docx"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论