Freemarker (XML template) export Word

Source: Internet
Author: User

The function that is used in the project is to export the data to word according to a fixed template. such as the Taiwan account. After the completion of the processing process according to the requirements of the account to export, sometimes the program needs to implement the production of standard Word documents, required to be able to print, and keep the page style unchanged.

This function is implemented by Xml+freemarder, Word supports XML format from 2003, the approximate steps: Edit word style with office2003 or above, then save as XML, translate XML into Freemarker template, Finally, we use Java to parse the Freemarker template and output doc.

First, the production of templates

First use Word to make a template, such as:

Then save as an XML file, you can use the highlighted notepad++ to open the XML, mainly highlighted, and elements automatically paired

You can also use Foxe_chs Open XML, is a concise, fast XML editor, and also provides F8-aligned typesetting functionality.

After saving, modify the suffix named FTL, so that the template is finished.

Second, the realization of programming

First introduce Freemarker's Open source package

1 Import freemarker.template.Configuration; 2 Import freemarker.template.Template; 3 import freemarker.template.TemplateException;

Write Java classes, primarily to implement assignment of templates and generate streams for export.

1  PackageCom.test.freemarker.report;2 ImportJava.io.BufferedWriter;3 ImportJava.io.File;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 ImportJava.io.OutputStreamWriter;7 ImportJava.io.Writer;8 ImportJava.util.HashMap;9 ImportJava.util.Map;Ten Importfreemarker.template.Configuration; One Importfreemarker.template.Template; A Importfreemarker.template.TemplateException; -  -  Public classDocumenthandler { the    PrivateConfiguration Configuration =NULL; -  -     PublicDocumenthandler () { -Configuration =NewConfiguration (); +Configuration.setdefaultencoding ("Utf-8"); -    } +  A     Public voidCreatedoc () { at       //to fill in the Modeled data file -Map DataMap =NewHashMap (); - GetData (DATAMAP); -       //set the Modeled appliance method and path, Freemarker supports a variety of template loading methods. Can be re-servlet,classpath, database loading, -       //here Our template is placed under the Com.havenliu.document.template package -Configuration.setclassfortemplateloading ( This. GetClass (), "/com/test/freemarker/report"); inTemplate T =NULL; -       Try { to          //TEST.FTL for the template to be loaded +t = configuration.gettemplate ("TEST.FTL"); -T.setencoding ("Utf-8"); the}Catch(IOException e) { * e.printstacktrace (); $       }Panax Notoginseng       //output document path and name -File OutFile =NewFile ("D:/test.doc"); theWriter out =NULL; +       Try { AFileOutputStream fos =NewFileOutputStream (outFile); theOutputStreamWriter Owriter =NewOutputStreamWriter ( +FOS, "UTF-8");//this place is indispensable for the encoding of convection, which should be possible when used with main (), but the Word document will not open when the Web request is exported and the package XML file is incorrect. The encoding format is not correct and cannot be parsed.  -out =NewBufferedWriter (owriter); $          /**out = new BufferedWriter (new OutputStreamWriter (New FileOutputStream (OutFile), "Utf-8"));*/ $}Catch(Exception E1) { - e1.printstacktrace (); -       } the       Try { - t.process (DataMap, out);Wuyi out.close (); the}Catch(templateexception e) { - e.printstacktrace (); Wu}Catch(IOException e) { - e.printstacktrace (); About       } $    } -  -    /** - * Note that the data stored in the Datamap key value should correspond to the parameters in the template A     * @paramDataMap +     */ the    Private voidGetData (Map dataMap) { -Datamap.put ("Title_name", "User Information"); $Datamap.put ("user_name", "Zhang San"); theDatamap.put ("Org_name", "Microsoft Corporation")); theDatamap.put ("Dept_name", "Business unit")); the    } the}

Third, the resulting word results are as follows:

For complex report styles you can save after editing in Word, and if you need output list type data, you can refer to Freemarker loops or logical controls.

Iv. Summary

This tool is really handy for generating word, but there are still some deficiencies, freemarker three deadly Sins->http://www.iteye.com/topic/17468

Reprinted from: http://blog.csdn.net/xiexl/article/details/6747767

Freemarker (XML template) export Word

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.