Xdocreport is primarily about manipulating word, defining variables in Word templates, and substituting variables. (in Word, you can also replace the dynamic picture, you can cycle, judge the operation, can define the instruction extender, can be converted to PDF file, etc.)
1, template variable definition.
New Word,ctrl + F9 edit field select MergeField Edit field code
2, Code
/**
* Export Word files according to the template
*
* @param reportdataThe ReportData object is a data object that stores map data
* @param templatename Template file path
* @param outputfilename Output file path
*/
public static void Reportdoc (ReportData reportdata, String templatename, String outputfilename) {
map<string, object> params = Reportdata.getparameters ();
InputStream in = null;
OutputStream outputstream = null;
Ixdocreport report = null;
try {
1) Load ODT file and set Velocity template engine and cache it to the registry
in = new FileInputStream (New File (Stringutil.formatfilepath (templatename)));
2) Create Java model context
IContext context = Getreportcontext (report, params);
Output file, file exists then delete
File OutputFile = new file (outputfilename);
folder does not exist, create all folders
File parentfile = Outputfile.getparentfile ();
if (!parentfile.exists ()) {
Parentfile.mkdirs ();
}
if (outputfile.exists ()) {
Outputfile.renameto (New File (OutputFileName + "." + New Date (). GetTime ()));
}
Generate a new file
OutputStream = new FileOutputStream (outputfilename);
Report.process (context, outputstream);
} catch (IOException e) {
Log.warn ("File stream acquisition failed", E);
} catch (Xdocreportexception e) {
Log.warn ("Export failed", e);
} finally {
try {
if (outputstream! = null) {
Outputstream.close ();
}
if (in = null) {
In.close ();
}
} catch (IOException e) {
Log.warn ("File stream shutdown failed", e);
}
}
}
private static IContext Getreportcontext (Ixdocreport report, map<string, object> params) throws xdocreportexception {
IContext context = null;
if (report! = null) {
context = Report.createcontext ();
Fieldsmetadata metadata = new Fieldsmetadata ();
for (Iterator Iterator = Params.entryset (). Iterator (); Iterator.hasnext ();) {
Map.entry Entry = (map.entry) iterator.next ();
String name = Stringutil.obj2str (Entry.getkey ());
Object value = Entry.getvalue ();
Context.put (name, value);
}
Report.setfieldsmetadata (metadata);
}
return context;
}
3, test
@Test
public void Testxdocword () throws Exception {
ReportData reportdata = new ReportData ();
Reportdata.addparameters ("name", "Zhang San");
Reportdata.addparameters ("Age", "2016-6-6");
Xdocreport.reportdoc (ReportData, "D:\\tempword\\template.docx", "D:\\tempword\\t.docx");
}
4, results
5,pom
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.itext.extension</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.1</version>
</dependency>
Xdocreport simple use action word substitution variable