是否有可以创建丰富的 Word 文档的 Java API?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/203174/
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
Is there a Java API that can create rich Word documents?
提问by billjamesdev
I have a new app I'll be working on where I have to generate a Word document that contains tables, graphs, a table of contents and text. What's a good API to use for this? How sure are you that it supports graphs, ToCs, and tables? What are some hidden gotcha's in using them?
我有一个新的应用程序,我将在其中生成一个包含表格、图形、目录和文本的 Word 文档。用于此目的的好的 API 是什么?您有多确定它支持图形、ToC 和表格?使用它们时有哪些隐藏的陷阱?
Some clarifications:
一些澄清:
- I can't output a PDF, they want a Word doc.
- They're using MS Word 2003 (or 2007), not OpenOffice
- Application is running on *nix app-server
- 我不能输出 PDF,他们想要一个 Word 文档。
- 他们使用的是 MS Word 2003(或 2007),而不是 OpenOffice
- 应用程序在 *nix app-server 上运行
It'd be nice if I could start with a template doc and just fill in some spaces with tables, graphs, etc.
如果我可以从模板文档开始,然后用表格、图表等填充一些空间,那就太好了。
Edit: Several good answers below, each with their own faults as far as my current situation. Hard to pick a "final answer" from them. Think I'll leave it open, and hope for better solutions to be created.
编辑:下面有几个很好的答案,就我目前的情况而言,每个答案都有自己的缺点。很难从他们那里挑选出一个“最终答案”。认为我会保持开放,并希望创建更好的解决方案。
Edit: The OpenOffice UNO project does seem to be closest to what I asked for. While POI is certainly more mainstream, it's too immature for what I want.
编辑:OpenOffice UNO 项目似乎与我要求的最接近。虽然 POI 肯定更主流,但它对于我想要的东西来说太不成熟了。
采纳答案by billjamesdev
In 2007 my project successfully used OpenOffice.org's Universal Network Objects(UNO) interface to programmatically generate MS-Word compatible documents (*.doc), as well as corresponding PDF documents, from a Java Web application (a Struts/JSP framework).
2007 年,我的项目成功地使用 OpenOffice.org 的通用网络对象(UNO) 接口从 Java Web 应用程序(Struts/JSP 框架)以编程方式生成 MS-Word 兼容文档 (*.doc) 以及相应的 PDF 文档。
OpenOffice UNO also lets you build MS-Office-compatible charts, spreadsheets, presentations, etc. We were able to dynamically build sophisticated Word documents, including charts and tables.
OpenOffice UNO 还允许您构建与 MS-Office 兼容的图表、电子表格、演示文稿等。我们能够动态构建复杂的 Word 文档,包括图表和表格。
We simplified the process by using template MS-Word documents with bookmark inserts into which the software inserted content, however, you can build documents completely from scratch. The goal was to have the software generate report documents that could be shared and further tweaked by end-users before converting them to PDF for final delivery and archival.
我们通过使用带有书签插入的模板 MS-Word 文档简化了该过程,软件将内容插入其中,但是,您可以完全从头开始构建文档。目标是让软件生成报告文档,最终用户可以共享和进一步调整,然后将它们转换为 PDF 以进行最终交付和存档。
You can optionally produce documents in OpenOffice formats if you want users to use OpenOffice instead of MS-Office. In our case the users want to use MS-Office tools.
如果您希望用户使用 OpenOffice 而不是 MS-Office,您可以选择生成 OpenOffice 格式的文档。在我们的例子中,用户想要使用 MS-Office 工具。
UNO is included within the OpenOffice suite. We simply linked our Java app to UNO-related libraries within the suite. An OpenOffice Software Development Kit(SDK) is available containing example applications and the UNO Developer's Guide.
UNO 包含在 OpenOffice 套件中。我们只是将我们的 Java 应用程序链接到套件中与 UNO 相关的库。可以使用OpenOffice 软件开发工具包(SDK),其中包含示例应用程序和 UNO 开发人员指南。
I have not investigated whether the latest OpenOffice UNO can generate MS-Office 2007 Open XML document formats.
我没有研究过最新的 OpenOffice UNO 是否可以生成 MS-Office 2007 Open XML 文档格式。
The important things about OpenOffice UNO are:
OpenOffice UNO 的重要事项是:
- It is freeware
- It supports multiple languages (e.g. Visual Basic, Java, C++, and others).
- It is platform-independent (Windows, Linux, Unix, etc.).
- 它是免费软件
- 它支持多种语言(例如 Visual Basic、Java、C++ 等)。
- 它与平台无关(Windows、Linux、Unix 等)。
Here are some useful web sites:
以下是一些有用的网站:
- Open Office home
- Open Office UNO Developer's Guide
- OpenOffice Developer's Forum(especially the "Macros and API" and "Code Snippets" forums).
- 开放式办公室主页
- Open Office UNO 开发人员指南
- OpenOffice 开发者论坛(尤其是“宏和 API”和“代码片段”论坛)。
回答by Jorge Ferreira
I think Apache POIcan do the job. A possible problem depending on the usage your aiming to may be caused by the fact that HWPF is still in early development.
我认为Apache POI可以完成这项工作。根据您的目标用途,可能存在的问题可能是由于 HWPF 仍处于早期开发阶段。
HWPFis the set of APIs for reading and writing Microsoft Word 97(-XP) documents using (only) Java.
HWPF是一组 API,用于使用(仅)Java 读取和写入 Microsoft Word 97(-XP) 文档。
回答by Gulzar Nazim
回答by tvanfosson
I've used Aspose.Wordsto do mail merge in .NET. I believe that they also have a Java version.
我已经使用Aspose.Words在 .NET 中进行邮件合并。我相信他们也有 Java 版本。
回答by schmoopy
I have developed pure XML based word files in the past. I used .NET, but the language should not matter since it's truely XML. It was not the easiest thing to do (had a project that required it a couple years ago.) These do only work in Word 2007 or above - but all you need is Microsoft's white paper that describe what each tag does. You can accomplish all you want with the tags the same way as if you were using Word (of course a little more painful initially.)
我过去曾开发过纯基于 XML 的 word 文件。我使用了 .NET,但语言应该无关紧要,因为它是真正的 XML。这不是最容易做的事情(几年前有一个需要它的项目。)这些只能在 Word 2007 或更高版本中使用 - 但您所需要的只是 Microsoft 的白皮书,其中描述了每个标签的作用。您可以使用标签完成您想要的一切,就像使用 Word 一样(当然最初会更痛苦一些。)
回答by billjamesdev
After a little more research, I came across iText, a PDF and RTF-file creation API. I think I can use the RTF generation to create a Doc-readable file that can then be edited using Doc and re-saved.
经过更多的研究,我遇到了iText,一个 PDF 和 RTF 文件创建 API。我想我可以使用 RTF 生成来创建一个 Doc 可读的文件,然后可以使用 Doc 进行编辑并重新保存。
Anyone have any experience with iText, used in this fashion?
任何人都对以这种方式使用的 iText 有任何经验?
回答by billjamesdev
Yet another possibility, since this is a web app.
另一种可能性,因为这是一个网络应用程序。
I was able to render an HTML page with the MIME type set to "application/msword", which caused the browser to spawn Word which imported the html just fine, allowing edits and saving just as if I'd output a real Word doc.
我能够渲染一个 MIME 类型设置为“application/msword”的 HTML 页面,这导致浏览器生成 Word,它可以很好地导入 html,允许编辑和保存,就像我输出一个真正的 Word 文档一样。
Tables work fine, but images I hadn't gotten working yet. It may be as easy as just an tag in the HTML, or I may have to stream a separate part of the response containing the image data in binary, or some other method I haven't come up with yet. :)
表格工作正常,但我还没有开始工作的图像。它可能就像 HTML 中的标记一样简单,或者我可能必须以二进制形式传输包含图像数据的响应的单独部分,或者我还没有想出的其他一些方法。:)
回答by billjamesdev
Try Aspose.Wordsfor java.
试试Aspose.Wordsfor java。
Aspose.Words for Java is an advanced (commercial) class library for Java that enables you to perform a great range of document processing tasks directly within your Java applications.
Aspose.Words for Java supports DOC, OOXML, RTF, HTML and OpenDocument formats. With Aspose.Words you can generate, modify, and convert documents without using Microsoft Word.
Aspose.Words for Java 是 Java 的高级(商业)类库,它使您能够直接在 Java 应用程序中执行大量的文档处理任务。
Aspose.Words for Java 支持 DOC、OOXML、RTF、HTML 和 OpenDocument 格式。使用 Aspose.Words,您可以在不使用 Microsoft Word 的情况下生成、修改和转换文档。
回答by Andrew Hancox
There's a tool called JODConverter which hooks into open office to expose it's file format converters, there's versions available as a webapp (sits in tomcat) which you post to and a command line tool. I've been firing html at it and converting to .doc and pdf succesfully it's in a fairly big project, haven't gone live yet but I think I'm going to be using it. http://sourceforge.net/projects/jodconverter/
有一个名为 JODConverter 的工具,它连接到开放式办公室以公开它的文件格式转换器,有一些版本可用作您发布到的 web 应用程序(位于 tomcat 中)和一个命令行工具。我一直在使用 html 并成功地转换为 .doc 和 pdf 它在一个相当大的项目中,还没有上线,但我想我会使用它。 http://sourceforge.net/projects/jodconverter/