Using Freemarker to generate word usage (Java)

Source: Internet
Author: User

First, you get a copy of the Word template you normally need, using the letters that you can use to distinguish the data that you want to fill with the program, such as XXXXX.

The second step is to convert the Word template into an XML file (Word can be saved as XML), open with tools such as Editorplus, note the character encoding, word character encoding is utf-8, so open is best not to change, first look at the encoding is correct (incorrect change character encoding ), then format the XML file, format the aspect of reading (formatted, there are many online formatting tools), and then look for the tags you just left in Word, replace them with the freemarker tag, whose syntax is basically consistent with the struts syntax.

Like what:

1, pass an object to Freemarker, extract the property value of an object can be written as ${report.planname}.

2. Pass a collection to Freemarker and iterate over a collection to:

< #list table1_tr as table1_td> // traversal start

${TABLE1_TD} // output value

</#list > // Traversal End

3, if traversing a two-dimensional collection


4. Define a variable < #assignx =0/>

5. Determine if the object is empty

<#ifQA. Question?? >

${qa.question}

</#if >


The third step outputs word, such as:


Example

The Word template is like this


The XML is this:


Then we use Freemarker Markup language to process dynamic data, from simple to difficult, we first replace the data outside the list data, each tag is not the same line. Such as:



Next we replace the list data with the < #list > tag to replace

Write < #list at the beginning of list data >


End Write </#list >


Replace middle section


Then Save:

The save file for my XML is called Test2.xml. I'll use this for a while.

Then start writing the program:

First you have to have these, is freemarker open source package, Baidu One, import is good,

import freemarker.template.Configuration;

import freemarker.template.Template;

import freemarker.template.TemplateException;


Thefreemarker class:

Import Java.io.bufferedwriter;import java.io.file;import java.io.fileoutputstream;import java.io.IOException; Import Java.io.outputstreamwriter;import java.io.writer;import Java.util.arraylist;import Java.util.HashMap;import Java.util.list;import Java.util.map;import Freemarker.template.configuration;import freemarker.template.Template; Import Freemarker.template.templateexception;public class Thefreemarker {private configuration configuration = null; Public Thefreemarker () {configuration = new configuration (); Configuration.setdefaultencoding ("UTF-8");} public void Createdoc () {//data file to be filled in modeled map DataMap = new HashMap (); GetData (DATAMAP);//Set Modeled device method and path// Here our template is placed under the Src.model package configuration.setclassfortemplateloading (This.getclass (), "model"); Template T = null;try {t = configuration.gettemplate ("Test2.xml");//load Test2.xml template} catch (IOException e) {e.printstacktr Ace ();} Output document path and name file OutFile = new file ("D:/outfiledoc.doc"); Writer out = null;try {out = new BufferedWriter (New OutputStreamWriter (New FileOutputStream (OutFile), "Utf-8");} catch (Exception E1) {e1.printstacktrace ();} try {t.process (DataMap, Out),} catch (Templateexception e) {e.printstacktrace ();} catch (IOException e) { E.printstacktrace ();}} /** * Note that the data stored in the DATAMAP key value should correspond to the parameters in the template * @param dataMap */private void GetData (Map dataMap) {datamap.put ("name", "small new and Small white"); /name XML marked ${name}datamap.put ("Tdate", "2011-12-02");//Time XML marked ${tdate}datamap.put ("Address", "Haidian District, Beijing");//Time The markup in XML is ${address}list table2 = new ArrayList (), for (int i = 0; i < 5; i++) {Table2 t = new Table2 (); T.setapplyno ("BBBB Bbbb-bb "), T.setcustname (" Xiao Xin "), T.setloandate (" 2012-12-12 "); T.setregion (" 999-999 "); Table2.add (t);} Datamap.put ("Table2", table2);}

Table2 is:

public class Table2 {private String applyno;private string custname;private string loandate;private string Region;public S Tring Getapplyno () {return applyno;} public void Setapplyno (String applyno) {this.applyno = Applyno;} Public String Getcustname () {return custname;} public void Setcustname (String custname) {this.custname = CustName;} Public String Getloandate () {return loandate;} public void Setloandate (String loandate) {this.loandate = loandate;} Public String getregion () {return region;} public void Setregion (String region) {this.region = region;}

Test class:

public class Pptest {public static void main (string[] args) {thefreemarker tf=new thefreemarker (); Tf.createdoc ();}}

Execution Result:



Description

Public Utility Call Center's Quality report template production, need to ensure that the name of each data unchanged, if you want to change the need to modify the background code and the corresponding entity class

Entity class: Com.haiyisoft.vo.entity.cc.repo.QCBriefReport

Com.haiyisoft.vo.entity.cc.repo.QCBriefReportQATable


Related articles freemarker export word--to automatically change the number of table data rows
Freemarker export word--with fusioncharts to create a diagram, text word



Using Freemarker to generate word usage (Java)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.