Java中的Word文档创建API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1991030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:10:45  来源:igfitidea点击:

Word document creation API in Java

javams-wordapache-poi

提问by Eran Medan

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

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

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

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

采纳答案by Carl Smotricz

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

我以前用过 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.

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

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.

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

If you're working in an environment or for a customer who insists that your produced documents be .DOCrather 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.

如果您在某个环境中工作或为坚持将您生成的文档.DOC而不是.RTF. .

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

更新:在此期间,我还有一些想法。

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.

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

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

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

Jacob is a COMinterface 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 是COMJava的接口;它本质上允许您“远程控制”Windows 程序,例如 Word 和 Excel。我上面链接的文档不是 Jacob 自己的网站,而是带有使用 Jacob 的“千篇一律”食谱的单个页面。项目本身在 SourceForge 上:http: //sourceforge.net/projects/jacob-project/但人们声称,而且确实如此,文档有点缺乏。

Jacob has the advantage over all other solutions that you're dealing with the "real" Word and therefore allcapabilities 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.

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

回答by Pascal Thivent

I'm not sure of the exact status of the Word documents support in POI but, according to the POI website, work is still in progress (can't say what this mean exactly). So, at this time, I would not use POI but rather try to generate a RTF document. For this, you could :

我不确定 POI 中 Word 文档支持的确切状态,但根据 POI 网站,工作仍在进行中(无法确切说明这意味着什么)。所以,此时,我不会使用 POI,而是尝试生成一个 RTF 文档。为此,您可以:

  • Use RTFTemplatewhich is a RTF to RTF Engine that can generate RTF document as the result of the merge of a RTF model and data.
  • Use iTextwhich is primarly a PDF generator but can also generate RTF.
  • Build your own custom solution (but I wouldn't do that).
  • 使用RTFTemplate,它是一个 RTF 到 RTF 引擎,可以生成 RTF 文档作为合并 RTF 模型和数据的结果。
  • 使用iText,它主要是一个 PDF 生成器,但也可以生成 RTF
  • 构建您自己的自定义解决方案(但我不会这样做)。

I'd go for iText.

我会去 iText。

回答by Valentin Rocher

If you use a template, and do not want to create the word document from scratch, for what I know, POI is a pretty good solution. You open the template and select the zones you want to replace.

如果您使用模板,并且不想从头开始创建 word 文档,据我所知,POI 是一个非常好的解决方案。您打开模板并选择要替换的区域。

They say POI is still is developpement, but I've been using it in production environnement and it works pretty good at the moment.

他们说 POI 仍处于开发阶段,但我一直在生产环境中使用它,目前效果很好。

回答by Brian Scott

You should look into the Aspose.Words components. They have recently begun providing a Java version of the component.

您应该查看 Aspose.Words 组件。他们最近开始提供该组件的 Java 版本。

See the following link: Aspose.Word for Java

请参阅以下链接:Aspose.Word for Java

This supports Word automation, creation and advanced features such as mail merging without the need for an instance of Microsoft Word on the machine. The real benefits are that you are able to work within the context of an actual word document and not having to compromise by creating RTFs etc.

这支持 Word 自动化、创建和高级功能,例如邮件合并,而无需在机器上安装 Microsoft Word 实例。真正的好处是您能够在实际 Word 文档的上下文中工作,而不必通过创建 RTF 等来妥协。

The Java version is not currently as fully featured as the .Net version but the main core functionality is there and they are pushing very hard to have a feature equivalent version soon.

Java 版本目前的功能不如 .Net 版本完整,但主要的核心功能已经存在,他们正在努力尽快推出功能等效的版本。

Also, if you purchase the Java version you get a years free upgrades / support as the new releases are created.

此外,如果您购买 Java 版本,您将在创建新版本时获得一年的免费升级/支持。

回答by JasonPlutext

If you are working with docx documents, docx4j is an option. Like POI, its open source.

如果您正在处理 docx 文档,则 docx4j 是一个选项。像POI一样,它是开源的。

回答by Leonardo

I created and use this: http://code.google.com/p/java2word

我创建并使用了这个:http: //code.google.com/p/java2word

回答by UniversE

I know this question is a bit old, but I think many people still find this with search engines, so I post another possibility to do what you want right here:

我知道这个问题有点老了,但我认为很多人仍然通过搜索引擎发现这个问题,所以我在这里发布了另一种可能性来做你想做的事:

If the one and only goal is to have a Word Template and to replace some values in it, you might consider saving a Word Template as single xml (not docx) and then processing it with simple Java and without any Framework. If you want to do more (e.g. create lists or tables) you might also consider understanding the xml format and writing your own helpers before loading a Framework like POI.

如果唯一的目标是拥有一个 Word 模板并替换其中的一些值,您可以考虑将 Word 模板保存为单个 xml(而不是 docx),然后使用简单的 Java 处理它,无需任何框架。如果您想做更多(例如创建列表或表格),您还可以考虑在加载像 POI 这样的框架之前了解 xml 格式并编写自己的帮助程序。

Here is an example on how to do that: http://dev-notes.com/code.php?q=10

这是一个如何做到这一点的例子:http: //dev-notes.com/code.php?q=10

This is the fast version, if you want a nice version, you could try using an XML processor.

这是快速版本,如果你想要一个好的版本,你可以尝试使用 XML 处理器。

PS: users might notice that the file extension is not doc but xml and they may blame you for that, but that's ok... just rename it to doc, word will recognize the format and everyone is happy again ;)

PS:用户可能会注意到文件扩展名不是 doc 而是 xml,他们可能会因此而责怪你,但这没关系......只需将其重命名为 doc,word 会识别格式,每个人都会再次高兴;)

回答by Yonkee

This is obviously way too late, But since 2013 there is a much better, more flexible solution to word document creation.

这显然为时已晚,但自 2013 年以来,有一个更好、更灵活的 Word 文档创建解决方案。

http://www.docx4java.org/trac/docx4j

http://www.docx4java.org/trac/docx4j

I have had much more luck with docx4j than I ever did with POI.

我在 docx4j 上的运气比在 POI 上好得多。