有没有可以创建丰富Word文档的Java API?

发布于 2024-07-07 10:58:53 字数 1560 浏览 17 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

驱逐舰岛风号 2024-07-14 10:58:54

iText 非常易于使用。

如果您需要文档文件,您可以从命令行调用abiword(免费的轻量级多操作系统文本处理器),它有几个转换格式选项。

iText is really easy to use.

If you requiere doc files you can call abiword (free lightweigh multi-os text procesor) from the command line, it has several conversion format convert options.

筱果果 2024-07-14 10:58:54

经过更多研究后,我发现了 iText,一个 PDF 和 RTF 文件创建 API。 我想我可以使用 RTF 生成来创建一个 Doc 可读的文件,然后可以使用 Doc 进行编辑并重新保存。

有人有以这种方式使用 iText 的经验吗?

Bill,从编程角度来看,POI 和 iText API 非常相似。 我过去曾使用过两者,发现它们既易于使用又具有良好的文档记录。

使用 iText,您只需对代码进行少量更改即可在格式(RTF 和 PDF)之间切换。 如果我没记错的话,内容是使用相同的调用进行布局的,然后使用几行代码将其设置为 PDF 或 RTF。

不过我认为 RTF 的格式与 DOC 相比是有限的。 我不知道您是否能够实现您正在寻找的高级功能(表格、内联图像),而不会遇到相当多的麻烦(如果有的话)。

鉴于您所说的 HWPF 没有足够的功能来满足您的需求(我只处理过 POI 的 Excel 部分),您最好的选择可能是说服当局相信 PDF 是这项工作的最佳技术

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.

Anyone have any experience with iText, used in this fashion?

Bill, the POI and iText API are very similar from a programming perspective. I've worked with both in the past and found them both easy to use and well documented.

With iText you gain the advantage of being able to switch between formats (RTF and PDF) with minor change to the code. If I remember correctly the content is laid out using the same calls and then set as PDF or RTF using a few lines of code.

However I believe the formatting in RTF is limited compared to DOC. I don't know if you'll be able to implement the advanced features you are looking for (tables, inline images) without a decent amount of hassle, if at all.

Given what you said that about HWPF not having enough functionality for your needs (I've only dealt with the Excel side of POI) your best bet may be to convince the powers that be that PDF is the best technology for the job.

人生百味 2024-07-14 10:58:54

我过去开发过纯基于 XML 的 Word 文件。 我使用了 .NET,但语言并不重要,因为它是真正的 XML。 这不是最容易做的事情(几年前有一个项目需要它。)这些只能在 Word 2007 或更高版本中工作 - 但您所需要的只是 Microsoft 的白皮书,其中描述了每个标签的用途。 您可以使用标签完成您想要的一切,就像使用 Word 一样(当然一开始会更痛苦一些。)

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.)

风月客 2024-07-14 10:58:54

还有另一种可能性,因为这是一个网络应用程序。

我能够渲染一个 MIME 类型设置为“application/msword”的 HTML 页面,这导致浏览器生成 Word,它可以很好地导入 html,允许编辑和保存,就像我输出真正的 Word 文档一样。

表格工作正常,但图像我还没有工作。 它可能就像 HTML 中的标签一样简单,或者我可能必须传输包含二进制图像数据的响应的单独部分,或者我还没有想到的其他方法。 :)

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.

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. :)

感受沵的脚步 2024-07-14 10:58:54

尽管这比请求晚得多,但它可能会对其他人有所帮助。 Docmosis 提供了一个 Java API,用于使用文档作为模板创建 doc、pdf、odt 格式的文档。 它使用 OpenOffice 作为引擎来执行格式转换。 文档操作和填充由 Docmosis 本身执行。

Even though this is much later than the request, it might help others. Docmosis provides a Java API for creating documents in doc,pdf,odt format using documents as templates. It uses OpenOffice as the engine to perform the format conversions. Document manipulation and population is performed by Docmosis itself.

梦在深巷 2024-07-14 10:58:54

经过更多研究后,我发现了 iText,一个 PDF 和 RTF 文件创建 API。 我想我可以使用 RTF 生成来创建一个 Doc 可读的文件,然后可以使用 Doc 进行编辑并重新保存。

有人有过以这种方式使用 iText 的经验吗?

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.

Anyone have any experience with iText, used in this fashion?

撩发小公举 2024-07-14 10:58:53

2007 年,我的项目成功使用了 OpenOffice.org 的 通用网络对象 (UNO)用于从 Java Web 应用程序(Struts/JSP 框架)以编程方式生成 MS-Word 兼容文档 (*.doc) 以及相应的 PDF 文档的接口。

OpenOffice UNO 还允许您构建与 MS-Office 兼容的图表、电子表格、演示文稿等。我们能够动态构建复杂的 Word 文档,包括图表和表格。

我们通过使用带有书签插入的模板 MS-Word 文档来简化流程,软件会在其中插入内容,但是,您可以完全从头开始构建文档。 目标是让软件生成可供最终用户共享和进一步调整的报告文档,然后将其转换为 PDF 以供最终交付和存档。

如果您希望用户使用 OpenOffice 而不是 MS-Office,您可以选择以 OpenOffice 格式生成文档。 在我们的例子中,用户想要使用 MS-Office 工具。

UNO 包含在 OpenOffice 套件中。 我们只是将 Java 应用程序链接到套件中与 UNO 相关的库。 提供了 OpenOffice 软件开发工具包 (SDK),其中包含示例应用程序和 UNO 开发人员指南。

我没有调查过最新的OpenOffice UNO是否可以生成MS-Office 2007 Open XML文档格式。

OpenOffice UNO 的重要特点是:

  1. 它是免费软件
  2. 它支持多种语言(例如 Visual Basic、Java、C++ 等)。
  3. 它与平台无关(Windows、Linux、Unix 等)。

以下是一些有用的网站:

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).

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.

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.

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.

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.

I have not investigated whether the latest OpenOffice UNO can generate MS-Office 2007 Open XML document formats.

The important things about OpenOffice UNO are:

  1. It is freeware
  2. It supports multiple languages (e.g. Visual Basic, Java, C++, and others).
  3. It is platform-independent (Windows, Linux, Unix, etc.).

Here are some useful web sites:

撩人痒 2024-07-14 10:58:53

我认为 Apache POI 可以完成这项工作。 根据您的目标用途,可能出现的问题可能是由 HWPF 仍处于早期开发阶段引起的。

HWPF
是一组用于读取和的 API
编写 Microsoft Word 97(-XP)
使用(仅)Java 的文档。

I think Apache POI can 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.

HWPF
is the set of APIs for reading and
writing Microsoft Word 97(-XP)
documents using (only) Java.

陈年往事 2024-07-14 10:58:53

你可以使用这个:
http://code.google.com/p/java2word

我实现了这个名为 Java2Word 的 API。 只需几行代码,您就可以生成一个 Microsoft Word 文档。

例如:

IDocument myDoc = new Document2004();
myDoc.getBody().addEle(new Heading1("Heading01"));
myDoc.getBody().addEle(new Paragraph("This is a paragraph...")

有一些如何使用的例子。 基本上你将需要一个 jar 文件。
如果您需要有关如何设置的更多信息,请告诉我。

*我写这篇文章是因为我们在一个项目中有一个真正的必需品。 更多内容在我的博客中:

http://leonardo-pinho.blogspot.com/2010/07/java2word-word-document-generator-from.html
*

干杯
Leonardo

编辑:链接中的项目已移至 https://github.com/leonardoanalista/java2word

You could use this:
http://code.google.com/p/java2word

I implemented this API called Java2Word. with a few lines of code, you can generate one Microsoft Word Document.

Eg.:

IDocument myDoc = new Document2004();
myDoc.getBody().addEle(new Heading1("Heading01"));
myDoc.getBody().addEle(new Paragraph("This is a paragraph...")

There is some examples how to use. Basically you will need one jar file.
Let me know if you need any further information how to set it up.

*I wrote this because we had one real necessity in a project. More in my blog:

http ://leonardo-pinho.blogspot.com/2010/07/java2word-word-document-generator-from.html
*

cheers
Leonardo

Edit : Project in link moved to https://github.com/leonardoanalista/java2word

才能让你更想念 2024-07-14 10:58:53

尝试 Aspose.Words for Java,它可以在任何安装了 Java 的操作系统上运行。

如果您需要 MS Word 输出格式,它将把文档输出为 DOC、DOCX 或 RTF。 所有这些都得到同样好的支持。

使用此 API,您可以从头开始创建文档(实际上是从节点)并设置其格式属性。 您还可以使用 DocumentBuilder,它提供了更高级别的方法,例如创建表行、插入字段等。或者您可以在现有预先创建的文档之间复制/连接/移动部分,假设您想要组装合同,只需抓取并复制来自多个文档的片段和 Aspose.Words 将在生成的文档中正确合并样式、列表格式等。

您将能够使用 Aspose.Words 插入 TOC 字段,但从今天开始,在 Microsoft Word 中打开文档时,TOC 字段将需要字段更新。 然而,我们将在 2010 年初发布对 TOC 字段的全面支持。例如,它将像 MS Word 一样构建完整的 TOC。

我是 Aspose.Words 团队的成员。

Try Aspose.Words for Java, it runs on any OS where Java is installed.

It will output the document to DOC, DOCX or RTF if you need an MS Word output format. All are supported equally well.

Using this API you can create a document from scratch, literally from nodes and set their formatting properties. You can also use a DocumentBuilder which provides higher level methods such as create a table row, insert a field etc. Or you can copy/join/move portions between existing pre created document, say you want to assemble a contract, just grab and copy pieces from several documents and Aspose.Words will merge styles, list formatting etc properly in the resulting document.

You will be able to insert a TOC field using Aspose.Words, but as of today, the TOC field will require a field update when the document is opened in Microsoft Word. However, we are going to release full support for TOC fields early in 2010. E.g. it will build complete TOC as MS Word does it.

I'm on the Aspose.Words team.

恬淡成诗 2024-07-14 10:58:53

它只被简单地提到过一次,所以我想提一下 docx4j 库,因为我在 docx4j 上取得的成功比其他任何东西都多。 Apache POI 对Word 文档的支持不是很好。 此外,与 Aspose.Words 不同,docx4j 是一个开源库。

docx4j 的唯一缺点是您必须创建 Office Open XML (docx) 格式文档,而不是基于 OLE2 (doc) 格式的文档。 这是Word 2007 的默认格式,但Word 2003 及更早版本的用户需要安装兼容包。

It was mentioned only briefly once, so I'd like to call out the docx4j library, as I've had more success with docx4j than anything else. Apache POI's support for Word documents isn't very good. Also, unlike Aspose.Words, docx4j is an open source library.

The only drawback is with docx4j you have to create Office Open XML (docx) format documents rather than OLE2-based (doc) format documents. This is the default format for Word 2007, but Word 2003 and earlier users will need to install a compatibility pack.

软甜啾 2024-07-14 10:58:53

尝试 Aspose.Words for java 。

Aspose.Words for Java 是一个高级(商业)Java 类库,使您能够直接在 Java 应用程序中执行大量文档处理任务。

Aspose.Words for Java 支持 DOC、OOXML、RTF、HTML 和 OpenDocument 格式。 使用 Aspose.Words,您可以生成、修改和转换文档,而无需使用 Microsoft Word。

Try Aspose.Words for 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.

半寸时光 2024-07-14 10:58:53

我使用 Aspose.Words 在 .NET 中进行邮件合并。 我相信他们也有Java版本。

I've used Aspose.Words to do mail merge in .NET. I believe that they also have a Java version.

喜爱皱眉﹌ 2024-07-14 10:58:53

您可以使用 Java COM 桥,例如 JACOB。 如果是来自客户端,另一种选择是使用 Javascript。

You can use a Java COM bridge like JACOB. If it is from client side, another option would be to use Javascript.

泡沫很甜 2024-07-14 10:58:53

有一个名为 JODConverter 的工具,它可以连接到开放式办公室以公开其文件格式转换器,有一个可发布到的 Web 应用程序(位于 tomcat 中)的版本和一个命令行工具。 我一直在它上发射 html 并成功转换为 .doc 和 pdf,它是一个相当大的项目,还没有上线,但我想我会使用它。
http://sourceforge.net/projects/jodconverter/

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/

安静 2024-07-14 10:58:53

docx4j 或 poi,两者都是 ASL v2

@wondersofcomputing:iText 实际上是免费且开源的

docx4j or poi, both of which are ASL v2

@wondersofcomputing: iText is actually free and open source

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文