我正在使用 Apache POI 使用 java 处理 docx 文档。原理很简单:
- 我有一个显示数据的基本模板(带有文本、标题和表格)
- 我有将填充模板的数据
- 我需要生成一个最终文档,其中包含为每个数据行重复的模板
我已经成功加载了模板文档,并且我还创建了最终的文档对象,它什么都不包含(来自一个包含我想要的样式的空文档,所以我不会丢失它们)。我可以通过浏览段落和表格来更改模板中的数据字段,但我不能将已更改的内容复制到最终文档中。
基本上,这就是我所拥有的:
for (int i=0; i<10; i++) {
finalDoc.removeBodyElement(i);
}
for (int i=0; i<10; i++) {
//EditableDocX is a subclass of XWPFDocument, for purpose of my project
EditableDocX document = new EditableDocX("template.docx");
replacementMap.put("[field1]", "Content1_"+i);
replacementMap.put("[field2]", "Content2_"+i);
document.replaceWithMap(replacementMap); //I've have this working
finalDoc.addContentOfDoc(document); //I don't have this working :(
}
finalDoc.saveFile("result.docx");
有没有办法将对象的内容复制到document
对象的末尾finalDoc
?(有段落和表格,有样式)