Java中的Word文档创建API [英] Word document creation API in Java

查看:27
本文介绍了Java中的Word文档创建API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用模板创建一个word文档,替换一些变量(字段)并将其另存为一个新的word文档.

I would like to create a word document using a template, replace some variables (fields) and save it as a new word document.

我正在考虑使用 Apache POI,http://poi.apache.org/ 是最好的以此目的?你能分享一下你对它的印象吗?

I was thinking using Apache POI, http://poi.apache.org/ is it the best for this purpose? can you share your impression from it?

推荐答案

我以前用过 POI,它当然能够生成 Word 文档.但细节决定成败.

I've worked with POI before and it's certainly able to generate Word documents. But the devil is in the details.

Word 有数以千计的功能:您可以将编号为 #13 且负缩进的列表放入另一个表格中包含的表格的两个连接单元格中,该表格本身也是项目符号列表的一部分……您明白了.当 POI 文档说它们是一项正在进行的工作时,这反映了尝试赶上(对我们而言,未记录的)Word 规范可能是一种永恒的状态.

Word has thousands of features: You can put numbered lists starting at #13 with negative indents into two joined cells of a table included in another table that is itself part of a bullet list... you get the idea. When the POI documentation says they are a work in progress, that reflects what will probably be an eternal state of trying to catch up to the (to us, undocumented) specification of Word.

POI 很好地支持了具有合理正常"使用功能集的文档,其接口和方法合理且一致,但有时需要一些工作.但正如 Pascal 所说,RTF 也支持具有不太昂贵的功能集的文档.我几乎没有做"RTF 的经验,但它可能比使用 POI 简单一些.

Documents with a reasonably "normal" set of used features are well supported by POI, whose interfaces and methods are reasonable and consistent but sometimes require a bit of work. But as Pascal says, documents with a not too exorbitant set of features are also supported by RTF. I have almost no experience "doing" RTF but it's probably a bit simpler than working with POI.

如果您在一个环境中工作或为坚持将您生成的文档为 .DOC 而不是 .RTF 的客户工作,那么 POI 几乎是您唯一的选择选择,除非您可以引入一个步骤,您可以使用一些 Office 自动化将 RTF 转换为 DOC.

If you're working in an environment or for a customer who insists that your produced documents be .DOC rather than .RTF, then POI is pretty much your only choice, unless you can introduce a step where you use a bit of Office automation to convert RTF into DOC.

更新:在此期间我有了更多的想法.

Update: I've had a couple more ideas in the meantime.

使用 POI 或创建 RTF 文档几乎可以在任何平台上执行.例如,在我的工作中,所有执行此类处理的服务器碰巧都在运行 Linux.

Using POI or creating RTF documents is something that you could do on practically any platform. At my job, all servers doing processing like this happen to be running Linux, for example.

但是,如果您的程序可能在 Windows 下运行,还有另一种选择:Jacob http://www.land-of-kain.de/docs/jacob/

However, in the likely case that your programs will run under Windows, there is another alternative: Jacob http://www.land-of-kain.de/docs/jacob/

Jacob 是 Java 的 COM 接口;它本质上允许您远程控制"Windows 程序,例如 Word 和 Excel.我上面链接的文档不是 Jacob 自己的网站,而是带有使用 Jacob 的千篇一律"食谱的单个页面.项目本身在 SourceForge 上:http://sourceforge.net/projects/jacob-project/ 但是人们声称文档有点缺乏,这是正确的.

Jacob is a COM interface for Java; it essentially allows you to "remote control" Windows programs such as Word and Excel. The document I linked to above is not to Jacob's own site but to a single page with "cookie cutter" recipes for using Jacob. The project itself is on SourceForge: http://sourceforge.net/projects/jacob-project/ But people claim, and rightly so, that the documentation is a bit lacking.

Jacob 比您处理真实"Word 的所有其他解决方案更具优势,因此您可以使用 Word 的所有功能.如果您的文档的细节方面无法通过 POI 或 RTF 格式处理,这将是一种替代方法.

Jacob has the advantage over all other solutions that you're dealing with the "real" Word and therefore all capabilities of Word are available to you. This would be an alternative if there are detail aspects of your document that just can't be handled with POI or via the RTF format.

这篇关于Java中的Word文档创建API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆