Forums Register Login

How to Use Java to generate Word document from a word template

+Pie Number of slices to send: Send
Hi, I am looking for java codes to generate a word document based on a word template, basically, I have a word template created and in my local path, the template has a proper format with some fields which will be filled in after java codes ran. The java codes will fetch one record from a table, and open the word template and then fill the fields in the word template, and created a new word document and save it in another folder.
I found this example: http://dev-notes.com/code.php?q=10
which is similar except it uses xml template instead of word template, can anybody show me how to make it work to change the template from xml to word (docx) template?
Thanks a lot for any help,
+Pie Number of slices to send: Send
This may not help you, but if this was my problem, I'd use RTF files as the templates. I think they'd be easier to manipulate, and can then be opened by any word processor afterwards.
+Pie Number of slices to send: Send
 

Ulf Dittmer wrote:This may not help you, but if this was my problem, I'd use RTF files as the templates. I think they'd be easier to manipulate, and can then be opened by any word processor afterwards.



thanks a lot, if the RTF works for my request, then I also can use it, as long as it works, I appreciate if you can provide any good working examples of using RTF, thanks again.
+Pie Number of slices to send: Send
I don't have example code around, but if you save your templates as RTF and open it in a text editor, you'll see any template variables (like INVOICENUMBER) you have used. Using "sed" or some Java text processing library it would be straightforward to replace those with their actual values.
+Pie Number of slices to send: Send
Hi.
You can use templ4docx library. It is on maven central repository, so you can just add it to your maven dependencies:


Example usage:


More details: blog page
+Pie Number of slices to send: Send
Welcome to the Ranch
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 12838 times.
Similar Threads
To generate Word Documents
How to create MS Word Doc using HWPF POI
Manipulating Microsoft word using java
create xml from xml schema in java
XSL Transformation into Word 2003 XML format handling logic to load XML data
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:03:57.