我正在使用 Apache POI 3.13 并尝试从给定的模板文件中搜索和替换文本,然后保存新生成的 .docx。这是我的代码:
public static void main(String[] args) throws InvalidFormatException, IOException {
String filePath = "Sample.docx";
File outputfile = new File("SampleProcessed.docx");
XWPFDocument doc = new XWPFDocument(OPCPackage.open(filePath));
for (XWPFParagraph p : doc.getParagraphs()) {
List<XWPFRun> runs = p.getRuns();
if (runs != null) {
for (XWPFRun r : runs) {
String text = r.getText(0);
if (text != null && text.contains("$VAR")) {
text = text.replace("$VAR", "JohnDoe");
r.setText(text, 0);
}
}
}
}
doc.write(new FileOutputStream(outputfile));
doc.close();
System.out.println("Done");
Desktop.getDesktop().open(outputfile);
}
这看起来很简单,但是当我运行此代码时,文档“Sample.docx”也会被替换。最后,我有两个内容相同的文件。
这是 POI 的正常行为吗?我以为打开文档只会将其加载到内存中,然后执行 'doc.write(OutputStream);' 会将其刷新到磁盘。
我尝试写入相同的“filePath”,但正如预期的那样,它会引发异常,因为我正在尝试写入当前打开的文件。
唯一有效的是当我首先复制模板文件并使用该副本时。但是现在,我有 3 个文件,第一个是原始模板“Sample.docx”,其余 2 个具有相同的内容(SampleProcessed.docx 和 SampleProcessedOut.docx)。
它有效,但它非常浪费。有什么办法吗?我做错了什么,也许我打开word文档错了?