Most of the time, you need to generate WORD Documents in the project. Java language and Freemaker technology are used to export Word documents. I think this method is good.
1. Check the code first.
The main program code is as follows:
Package com; 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 Word {private Configuration configuration = null; public Word () {configuration = new Configuration (); configuration. setDefaultEncoding ("UTF-8");} public void createDoc () {// Map dataMap = new HashMap (); getData (dataMap ); // set the method and path of the module. FreeMarker supports multiple template loading methods. It can be used to repeat servlet, classpath, database load, and // ftl file storage path configuration. setClassForTemplateLoading (this. getClass (), "/com/ftl"); Template t = null; try {// test. ftl is the template t = configuration to be loaded. getTemplate ("QQ. ftl "); t. setEncoding ("UTF-8");} catch (IOException e) {e. printStackTrace ();} // output File path and name File outFile = new File ("F:/2013/test.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); out. close ();} catch (TemplateException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}}/*** note that the Key value of the data stored in dataMap must correspond to the parameter in the template * @ param dataMap */private void getData (Map dataMap) {dataMap. put ("title", "Test"); dataMap. put ("nian", "2012"); dataMap. put ("danweiming", "Shaanxi"); List lists = new ArrayList (); WordBean w1 = new WordBean (); w1.setPaixu ("1 "); w1.setBiaoduan ("section 1"); WordBean w2 = new WordBean (); w2.setPaixu ("2"); w2.setBiaoduan ("section 2"); lists. add (w1); lists. add (w2); dataMap. put ("wordBeans", lists); dataMap. put ("paiming", "1"); dataMap. put ("biaoduan", "section 1");} public static void main (String [] args) {new Word (). createDoc ();}}
The JavaBean code is as follows:
package com;public class WordBean {public String paixu;public String biaoduan;public String getPaixu() {return paixu;}public void setPaixu(String paixu) {this.paixu = paixu;}public String getBiaoduan() {return biaoduan;}public void setBiaoduan(String biaoduan) {this.biaoduan = biaoduan;}}
The above code is easy to understand. The biggest question is how the QQ. ftl file came from?
2. Template File
(A): Save the edited wordtemplate.qq.doc as a QQ. xml file, create a freemaker file, QQ. ftl, and copy the QQ. xml file to the QQ. ftl file.
(B): Modify the ftl template file and replace the content to be replaced with tags such as $ {biaoduan.
(The ftl file is too long to be listed)
3. Run the program. The word file is generated to the specified location.
I personally think this generation method is much more convenient than jacob.
Resources are as follows:
Http://download.csdn.net/detail/xiaocha2008/5074285