Java dynamically generates complex. docfiles, java.doc
The project needs to use a java program to generate a doc file. Baidu times, FreeMarker has a high rating. FreeMarker is a template engine written in Java language, which generates text output based on templates, for details about FreeMarker, please go to Baidu .....
This blog post mainly summarizes the pitfalls you encounter when using online examples. I would like to thank the elders who shared the technology.
Original article:
Http://www.360doc.com/content/13/0731/10/13247663_303740756.shtml [blog is very beautiful, like a]
Reference 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 idea is as follows:
1. Adjust the layout in the word, including the dynamic part you want to generate, and some unnecessary regular text.
2. Save the word document as xml
3. Open it with Firstobject free XML edito and mark the fields that you need to dynamically generate. In this way, you can
FreeMarker also supports rich tags. If you want to display more complex and rich content, you can implement it;
Firstobject free XML edito links:
Http://www.cnblogs.com/know-life-death/archive/2012/02/01/2334742.html
4. Change the suffix of the xml file to. ftl and reference it to your project.
Notes:
A. the word version cannot be lower than 2003, because 2003 supports xml;
B. when you use Firstobject free XML edito to open the xml file to be edited, remember not to place the xml file in the directory containing the Chinese path. [the editor will not respond, and then you will 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 11 import cn. sina. ttjava_13.database.DB; 12 import freemarker. template. configuration; 13 import freemarker. template. template; 14 15 public class WordTest {16 17 private Configuration configuration = null; 18 private Connection conn; 19 private ResultSet res; 20 21 public WordTest () {22 configuration = new Configuration (); 23 configuration. setDefaultEncoding ("UTF-8"); 24} 25 26 public void createWord () {27 Map <String, Object> dataMap = new HashMap <String, Object> (); 28 try {29 String selectSql = "select id, NAME, NORMALPRICE, memberprice from T_PRODUCT WHERE 1 LIMIT 10"; 30 conn = DB. getConn (); 31 res = DB. getRs (conn, selectSql); 32 while (res. next () {33 dataMap. put ("id", res. getString ("id "). trim (); 34 dataMap. put ("name", res. getString ("name "). trim (); 35 dataMap. put ("normalprice", res. getString ("normalprice "). trim (); 36 dataMap. put ("memberprice", res. getString ("memberprice "). trim (); 37 38 configuration. setClassForTemplateLoading (this. getClass (), "/template"); // location of the FTL file 39 Template template = configuration. getTemplate ("Product. ftl "); 40 41 File outFile = new File (" D:/temp/"+ res. getString ("name "). trim (). replaceAll ("/", "") + ". doc "); 42 Writer out = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (outFile)," UTF-8 "); 43 template. process (dataMap, out); 44 out. close (); 45} 46 DB. close (res); 47 DB. close (conn); 48} catch (Exception e) {49 e. printStackTrace (); 50} 51} 52 53 public static void main (String [] args) {54 WordTest test = new WordTest (); 55 test. createWord (); 56} 57}
A. The code database is mysql, And the queried data is dynamically entered into wod;
B. template. process () accepts a Map and input stream as input parameters. Map is the data that needs to be dynamically generated into the doc, and the field name must be in. the definitions in ftl are consistent;
C. I think there are many data sources, such as Program Computing results, data stored in the database, and data clicked on the page .........
D. If you want to use this code, you need a. ftl file and create a template directory under the src directory of your project;
E. I don't know how to upload the. ftl file. If you need an example, please leave a message below and I will send it to you.