使用来自 java 对象的值从模板动态创建 word 文档

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

Creating a word document from a template dynamically using values from java objects

javams-wordjsoupdocx4j

提问by Sunmit Girme

I want to create a word document from an HTML page. I am planning to get the values on the HTML page and then pass these values to a document template. I have used JSOUPto parse the contents of the HTML page and I get the values in my java program. I now want to pass these values to a word document template. I want to know what are the best techniques I can use to create the document template and pass the values to the template to create the word document.

我想从 HTML 页面创建一个 word 文档。我计划获取 HTML 页面上的值,然后将这些值传递给文档模板。我使用JSOUP来解析 HTML 页面的内容,并在我的 java 程序中获取值。我现在想将这些值传递给 Word 文档模板。我想知道我可以用来创建文档模板并将值传递给模板以创建 word 文档的最佳技术是什么。

Thank You.

谢谢你。

采纳答案by Sunmit Girme

I found something very Interesting and simple. We just need to create a simple .xml template for the document we want to create and then programmatically change the contents of the xml file and save it as a ms word document.

我发现了一些非常有趣和简单的东西。我们只需要为我们想要创建的文档创建一个简单的 .xml 模板,然后以编程方式更改 xml 文件的内容并将其保存为 ms word 文档。

You can find the xml template and the code here.

您可以在此处找到 xml 模板和代码。

回答by Andreas

i suggest you use xslt, because your data is already in xml-format and there are well defined xml-formats from microsoft.

我建议你使用 xslt,因为你的数据已经是 xml 格式的,而且微软有明确定义的 xml 格式。

You could write a document template with word and save it in xml-format. Then you can convert the word-xml to a xsl-template with your html-xml as input. After the xslt-transformation you have a valid word-xml with your dynamic values from the html-xml.

你可以用word写一个文档模板,然后以xml格式保存。然后,您可以将 html-xml 作为输入将 word-xml 转换为 xsl-template。在 xslt-transformation 之后,您有一个有效的 word-xml,其中包含来自 html-xml 的动态值。

XSLT example for excel

excel 的 XSLT 示例

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="no" />
<xsl:template match="/">
    <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office"
        xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
        xmlns:html="http://www.w3.org/TR/REC-html40">
        ...
        <xsl:for-each
            select="/yourroot/person">
        ...
        <Cell ss:StyleID="uf">
                            <Data ss:Type="String">
                                <xsl:value-of
                                    select="@Name" />
                            </Data>
                        </Cell>
        ..
        </xsl:for-each>

...
</xsl:template>
</xsl:stylesheet>

回答by Paul Jowett

JODReportsand Docmosismight also be useful options for you since there is template populate and Doc output. If DOCX is your real target, then you can write out the document yourself since the XML is published - but that is a lot of work.

JODReportsDocmosis也可能对您有用,因为有模板填充和 Doc 输出。如果 DOCX 是您的真正目标,那么您可以在 XML 发布后自己写出文档——但这需要大量工作。