Java uses Freemarker to generate Word documents

Source: Internet
Author: User

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

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.