Java dynamically generates complex. doc files

Source: Internet
Author: User

Read Catalogue

    • 1.word adjust the layout, including the dynamic parts you want to generate, and some regular text that you don't need to generate
    • 2. Save the Word document as XML
    • 3. Open with Firstobject free XML Edito, mark the fields you need to dynamically generate, ${} so you can
    • 4. Change the XML file to a suffix named. FTL, and then refer to your project

The project needs to use Java program to generate DOC file, Baidu, Freemarker evaluation is relatively high, Freemarker is a Java language written template engine, it is based on the template to generate text output, for more information about Freemarker please ask Baidu .....

This blog post is mainly about the pits that you encounter when using online examples. The draft does not forget to dig well people, or to thank the sharing technology predecessors.

Original link:

http://www.360doc.com/content/13/0731/10/13247663_303740756.shtml [Blogger Blog is very beautiful, praise a]

Referenced example Links:

1.http://www.360doc.com/content/13/0731/10/13247663_303740756.shtml

2.http://blog.csdn.net/zhanwentao2/article/details/7255432

Freemarker.jar:

1.http://download.csdn.net/detail/pc159321/7077059

2.http://download.csdn.net/detail/zhaoshe/3153176

The main ideas are as follows:

Back to top 1.word adjust the layout, including the dynamic parts you want to generate, and some regular text that you don't need to generate

Back to top of 2. Save a Word document as XML

Back to top 3. Open with Firstobject free XML Edito, mark the fields you need to dynamically generate, ${} so you can

Freemarker also supports the need for rich tagging, which can be implemented if you want to show more complex and rich content;

Firstobject free XML edito friendly download Link:

Http://www.cnblogs.com/know-life-death/archive/2012/02/01/2334742.html

Back to top 4. Change the XML file with the suffix named. FTL, and then refer to your project

Issues to be aware of:

A. The word version cannot be less than 2003, because 2003 only starts supporting XML;

B. When opening an XML file to be edited with Firstobject free XML Edito, remember that XML is not placed in a directory that contains Chinese paths "the editor will not respond, and then you know ...".

The implementation code is as follows:

 1 Import java.io.BufferedWriter; 2 Import Java.io.File; 3 Import Java.io.FileOutputStream; 4 Import Java.io.OutputStreamWriter; 5 Import Java.io.Writer; 6 Import java.sql.Connection; 7 Import Java.sql.ResultSet; 8 Import Java.util.HashMap; 9 Import java.util.map;10 Import cn.sina.ttjava_13.database.     DB;12 Import freemarker.template.configuration;13 Import freemarker.template.template;14 public class Wordtest {16 17 Private configuration Configuration = null;18 Private Connection conn;19 private ResultSet res;20 Publi C Wordtest () {configuration = new configuration (); Configuration.setdefaultencoding ("UTF-8"); 24}         Createword public void () {map<string, object> dataMap = new hashmap<string, object> (); 28              try {selectsql String = "Select Id,name,normalprice,memberprice from t_product WHERE 1 LIMIT 10"; 30     conn = Db.getconn (); res = DB.GETRS (conn, selectsql); 32        while (Res.next ()) {datamap.put ("id", res.getstring ("id"). Trim ()); Datamap.put ( "Name", Res.getstring ("name"). Trim ()); Datamap.put ("Normalprice", Res.getstring ("Normalprice"). Trim ()); 3 6 Datamap.put ("Memberprice", Res.getstring ("Memberprice"). Trim ()); Panax Notoginseng Co Nfiguration.setclassfortemplateloading (This.getclass (), "/template"); The location of the FTL file exists in the configuration.gettemplate template template = "PRODUCT.FTL"; Utfile = new File ("d:/temp/" + res.getstring ("name"). Trim (). ReplaceAll ("/", "") + ". Doc"); W BufferedWriter (New OutputStreamWriter (New FileOutputStream (OutFile), "UTF-8")); Template.process (Datama         P, out); Out.close ();}46 db.close (res); Db.close (conn); 48 } catch (Exception e) {e.printstacktrace (); 50}51    }52-public static void main (string[] args) {wordtest test = new Wordtest (); test.createwor D (); 56}57}

A. Code database for MySQL, the query to the data, dynamic fill into the WOD;

B.template.process (), accept a map and input stream as parameters, map is the data you need to dynamically generate to Doc, the field name must be consistent with what you define in the. FTL;

C. I think the source of the data can be many, the results of the program calculation, the data stored in the database, the page click on the data ....

D. If you want to use this code, you need to have an. ftl file and create a new template directory under your project SRC directory;

E. I do not know how to upload the. ftl file, if you need to do an example, please leave a message below, I send you.

Java dynamically generates complex. doc files

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.