Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Peter Cong
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Cong
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Łukasz Stypka
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!