Generating text document from template dynamically using values ​​from java objects

I want to create a Word document from an HTML page. I am planning to get the values ​​in an HTML page and then pass those values ​​to the document template. I have used JSOUP to parse HTML page content and I am getting the values ​​in my java program. Now I want to pass these values ​​into a Word document template. I want to know what are the best techniques I can use to create a document template and pass values ​​to the template to create a Word document.



source to share

3 answers

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 modify the contents of the xml file and save it as a word ms document.

You can find the xml template and code here.



I suggest you use xslt because your data is already in xml format and Microsoft has well defined xml formats.

You can write document template with word and save it in xml format. Then you can convert word-xml to 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.

XSLT example for excel

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="" 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"
        <Cell ss:StyleID="uf">
                            <Data ss:Type="String">
                                    select="@Name" />





JODReports and Docmosis might also be helpful for you as there is a fill pattern and Doc output. If DOCX is your real goal, you can write the document yourself, since the XML is published, but that's a lot of work.



All Articles