Spencer Bharath
Spencer Bharath

Reputation: 567

Create docx file from a template file in java

I need to create docx files based on a templates. The template should contain the place holders and I should be able to fill the the place holders from java . Is it possible to do it , If so suggest me the good and efficient way to do it .

Upvotes: 2

Views: 13081

Answers (4)

Tom
Tom

Reputation: 4093

A little late for the original question, but if anyone else needs to dynamically create docx documents from templates, you might want to have a look at the DocxStamper Java library which I created on top of docx4j.

It allows to use the Spring Expression Language in docx templates and you can create a document out of a template with a couple lines like this:

MyData data = ...;           // your own POJO containing the data      
InputStream template = ...;  // InputStream to the template file
OutputStream out = ...;      // OutputStream to the resulting document
DocxStamper stamper = new DocxStamperConfiguration()
    .build();
stamper.stamp(template, context, out);
out.close();

Upvotes: 4

Tahir Manzoor
Tahir Manzoor

Reputation: 597

You can use Word template with following syntax of LINQ Reporting to achieve your requirements using Aspose.Words for Java.

<< tag_name [expression] -switch1 -switch2 ...>>

A tag body typically consists of the following elements:

  • A tag name
  • An expression surrounded by brackets
  • A set of switches available for the tag, each of which is preceded by the “-“ character

Assume, that you have the Sender class defined in your application as follows:

public class Sender {
    public Sender(String name, String message) {
        _name = name;
        _message = message;
    }

    public String getName() {
        return _name;
    }

    public String getMessage() {
        return _message;
    }

    private String _name;
    private String _message;
}

To produce a report containing a message of a concrete sender on its behalf, you can use a template document with the following content.

<<[s.getName()]>> says: "<<[s.getMessage()]>>."

To build a report from the template, you can use the following source code.

Document doc = new Document(getMyDir() + "temp_HelloWorld.docx");

Sender sender = new Sender("LINQ Reporting Engine", "Hello World");
ReportingEngine engine = new ReportingEngine();
engine.buildReport(doc, sender, "s");

doc.save(getMyDir() + "out.docx");

I work with Aspose as Developer evangelist.

Upvotes: 1

centic
centic

Reputation: 15872

See https://github.com/centic9/poi-mail-merge for a simple "Variable replacement" method. It does not work if one replacement-string has multiple formats applied, but does work well for simple cases where the template is carefully crafted.

Basically it reads the template and data from CSV or an Excel file and then merges it into multiple result files, one for each line of data.

It works on the DOCX XML content, so is not fully using Apache POI XWPF support, but this way formatting and other things from the template are used as expected without the need for full support for everything in Apache POI (which has DOCX support still as part of the "scratchpad" component as support is not considered fully done yet).

Upvotes: 1

JasonPlutext
JasonPlutext

Reputation: 15863

As discussed elsewhere before, there are 3 basic approaches:

  1. BEST: content control data binding

  2. cheap/cheerful: Variable replacement (ie magic strings on the document surface), but brittle (the split run problem)

  3. LEGACY: MERGEFIELD with or without other field codes.

Docx4j supports all three approaches, but we generally recommend content control databinding, since it aligns with Microsoft's direction (as best can be ascertained), and is most powerful.

You'll want to consider the technical skills of your template authors.

Upvotes: 1

Related Questions