1. Raw Materials
Open source jar Package Freemarker, Eclipse, a copy of the template Word document
2. First design the template Word document
In general, the format of the Word document that is output by the program is fixed, for example, to create a table, to write the table's title, the contents of the table are labeled with different marks, and after word is designed, save the Word document as an XML file (Note: Only word2003 versions are available), Open the file using the XML tool, replace the content you want to output to Word using ${xx}, and change the file directly to the TDL suffix after saving
3. Write a program using eclipse
1) Get a configuration instance and set the character sets for him
Configuration = new configuration ();
Configuration.setdefaultencoding ("UTF-8");
2) Resolve the Word template
Get the class parser, parse the address TemplatePath, the address that holds the template I put the template under the package of that class, that is/com/welv/this
Configuration.setclassfortemplateloading (This.getclass (), TemplatePath);
Template t = configuration.gettemplate (templatename);
T.setencoding ("UTF-8");
3) Output the content to Word and generate the file
Write out = new Outputstreamwrite (New FileOutputStream ("Address of the output"), "Utf-8");
Datamap for the map container to store < output to Word location, output content >
T.process (DataMap, out);
Out.close ();
Java uses Freemarker to generate Word documents