Java dynamically generates complex. doc files

Source: Internet
Author: User

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:

1.word It adjusts the layout, including the dynamic parts you want to generate, as well as some regular text that you don't need to generate:

2. Save the Word document as XML;

3. Open with Firstobject free XML Edito, marking 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

4. Change the XML file to a 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 ImportJava.io.BufferedWriter;2 ImportJava.io.File;3 ImportJava.io.FileOutputStream;4 ImportJava.io.OutputStreamWriter;5 ImportJava.io.Writer;6 Importjava.sql.Connection;7 ImportJava.sql.ResultSet;8 ImportJava.util.HashMap;9 ImportJava.util.Map;Ten  One ImportCn.sina.ttjava_13.database. DB; A Importfreemarker.template.Configuration; - Importfreemarker.template.Template; -  the  Public classWordtest { -  -     PrivateConfiguration Configuration =NULL; -     PrivateConnection Conn; +     PrivateResultSet Res; -  +      Publicwordtest () { AConfiguration =NewConfiguration (); atConfiguration.setdefaultencoding ("UTF-8"); -     } -  -      Public voidCreateword () { -map<string, object> DataMap =NewHashmap<string, object>(); -         Try { inString selectsql = "Select Id,name,normalprice,memberprice from t_product WHERE 1 LIMIT 10"; -conn =db.getconn (); tores =DB.GETRS (conn, selectsql); +              while(Res.next ()) { -Datamap.put ("id", res.getstring ("id")). Trim ()); theDatamap.put ("Name", Res.getstring ("name")). Trim ()); *Datamap.put ("Normalprice", Res.getstring ("Normalprice"). Trim ()); $Datamap.put ("Memberprice", Res.getstring ("Memberprice"). Trim ());Panax Notoginseng                  -Configuration.setclassfortemplateloading ( This. GetClass (), "/template");//where the FTL file exists theTemplate template = Configuration.gettemplate ("PRODUCT.FTL"); +  AFile OutFile =NewFile ("d:/temp/" + res.getstring ("name"). Trim (). ReplaceAll ("/", "") + ". Doc"); theWriter out =NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream (OutFile), "UTF-8")); + template.process (DataMap, out); - out.close (); $             } $ Db.close (res); - Db.close (conn); -}Catch(Exception e) { the e.printstacktrace (); -         }Wuyi     } the  -      Public Static voidMain (string[] args) { WuWordtest test =Newwordtest (); - Test.createword (); About     } $}

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.