Java 如何使用 docx4j 将 HTML 转换为 .docx?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20461239/
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:06:15  来源:igfitidea点击:

how to convert HTML to .docx using docx4j?

javahtmlconverterdocxdocx4j

提问by Jalal Sordo

I read some articles about the conversion of htmlto .docxand I found out that docx4jgives pretty decent results. I wonder if anyone could provide me the following info:

我阅读了一些关于htmlto转换的文章.docx,我发现它docx4j给出了相当不错的结果。我想知道是否有人可以向我提供以下信息:

  1. Needed jarsand versions.
  2. Sample code for conversion from htmlto .docx.
  1. 需要jars和版本。
  2. html到 的转换示例代码.docx

Sorry I couldn't post anything I tried because I haven't tried anything on this task yet, although I use Apache POIto convert the bytes[]I get from datatabseto htmlto output in a rich text editoron a jsfapplication. Please enlighten me, I'm lost in stress and confusion...!

抱歉,我无法发布我尝试过的任何内容,因为我还没有在此任务上尝试过任何内容,尽管我曾经在应用程序Apache POI中将bytes[]I 从datatabse到转换html为输出。请赐教,我迷失在压力和困惑中......!rich text editorjsf

采纳答案by JasonPlutext

To import XHTML, use

要导入 XHTML,请使用

<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-ImportXHTML</artifactId>
    <version>3.0.0</version>
</dependency>

See further http://www.docx4java.org/blog/2013/11/docx4j-3-0-and-maven/

进一步查看http://www.docx4java.org/blog/2013/11/docx4j-3-0-and-maven/

For sample code, see https://github.com/plutext/docx4j-ImportXHTML/tree/master/src/samples/java/org/docx4j/samples

示例代码见https://github.com/plutext/docx4j-ImportXHTML/tree/master/src/samples/java/org/docx4j/samples

Note that your input needs to be well-formed XML, so if you have HTML, you'll need to tidy it first (with one of the many java libraries which can do this for you).

请注意,您的输入需要是格式良好的 XML,因此如果您有 HTML,则需要先整理它(使用许多可以为您完成此操作的 Java 库之一)。