Search code examples
javadocxdocx4j

Java POI/docx4j generate Word file with several repeating entries


I have an example .docx template with text plus placeholder keys as shown below:

I ${Full_Name_n} accept terms and condition stated in ${Signed_Doc_n} and is liable for ...

Is there an elegant approach from docx4j camp that can generate 1 .docx for n number of individuals using the above text block ?

I have a POI class that does this my merging n number of .docx into 1 which is very dirty and not acceptable performance-wise.

P.S. Template text will have paragraphs and paragraphs suspended into tables. Images will not be involved.

For example here is my template saved in 1 word file:

I ${Full_Name_n} accept terms and condition stated in ${Signed_Doc_n} ...

And this is what I expect to be the output, that is several entries in 1 word file:

I Bill smth accept terms and condition stated in article 10 ...

I John smth accept terms and condition stated in article 21 ...

I Sterling smth accept terms and condition stated in article 11 ...

I Joshua smth accept terms and condition stated in article 13 ...

Solution

  • Although Mail Merge as recommeded by Jason is the only clean solution in Java, my approach was POI/docx4j-less PLSQL.

    Dirty but stupidly easy for not so complex .docx reports.

    As I am using PLSQL on weblogic server, my approach was just directly for looping HTML + text from few tables into JSP, before setting content type to docx.

    I will leave this here for record.