更新的文档的MSWord使用Apache POI [英] Updating an MSWord document with Apache POI
问题描述
我试图更新使用Apache POI一个Microsoft Word文档。该MSWORD文件是包含了许多形式$ {} place.holder和所有我需要做的是具体的值替换持有者占位符的模板。我已经得到了迄今
I'm trying to update a Microsoft Word document using Apache POI. The msword document is a template that contains a number of placeholders in the form "${place.holder}" and all I need to do is to replace the holders with specific values. What I've got so far is
private void start() throws FileNotFoundException, IOException {
POIFSFileSystem fsfilesystem = null;
HWPFDocument hwpfdoc = null;
InputStream resourceAsStream = getClass().getResourceAsStream("/path/to/document/templates/RMA FORM.doc");
try {
fsfilesystem = new POIFSFileSystem(resourceAsStream );
hwpfdoc = new HWPFDocument(fsfilesystem);
Range range = hwpfdoc.getRange();
range.replaceText("${rma.number}","08739");
range.replaceText("${customer.name}", "Roger Swann");
FileOutputStream fos = new FileOutputStream(new File("C:\\temp\\updatedTemplate.doc"));
hwpfdoc.write(fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
程序运行没有错误。如果我看在输出文件用十六进制编辑器,我可以看到占位符已被替换的程序。然而,当我尝试打开与的MSWord文档,崩溃的MSWord。
The program runs without errors. If I look in the output file with a Hex editor I can see that the placeholders have been replaced by the program. However, when I try to open the document with MSWord, MSWord crashes.
有没有办法,我的思念,还是我基本上是运气与此步骤(一系列步骤)?我是否需要调整任何计数器,因为替换文本的长度是不一样的替换文本的长度?
Is there a step (series of steps) that I'm missing, or am I basically out of luck with this? Do I need to adjust any counters because the length of the replacement text is not the same as the length of the replaced text?
问候
推荐答案
使用新的FileInputStream()
而不是的getClass()。的getResourceAsStream( /路径/到/文件/模板/ RMA表.doc);
这篇关于更新的文档的MSWord使用Apache POI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!