最近一個項目要導出word文檔,折騰老半天,發現還是用freemarker的模板來搞比較方便省事,現總結一下關鍵步驟,供大家參考,這里是一個簡單的試卷生成例子。
一、模板的制作
先用Word做一個模板,如下圖:
(注意,上面是有表格的,我設置了邊框不可見)然后另存為XML文件,之后用工具打開這個xml文件,有人用firstobject XML Editor感覺還不如notepad++,我這里用notepad++,主要是有高亮顯示,和元素自動配對,效果如下:
上面黑色的地方基本是我們之后要替換的地方,比如xytitle替換為${xytitle},對已表格要十分注意,比如選擇題下面的表格,我們可以通過<w:tr>查找來定位,一對<w:tr></w:tr>代表一行,也就是一條記錄(一道題),我們這里要用一對<#list></#list>來將其包括,以便后續填充數據,具體可參照Freemarker頁面語法,例如這里選擇題,我們是兩行為一條記錄,所以要<#list></#list>要包括兩行,形如:<#list table1 as plan1><w:tr>題號 題目</w:tr><w:tr>選項</w:tr></#list>,然后在這其中找著對應的xzn,xztest,ans1,ans2,ans3,ans4替換為${plan1.xzn},${plan1.xztest},${plan1.ans1},${plan1.ans2},${plan1.ans3},${plan1.ans4},注意這里的table1及plan1命名,table1后續填充數據要用到,其他的替換同理操作,得到效果如下:
保存后,修改后綴名為ftl,至此模板制作完畢。
二、編程實現
這里用到了freemarker-2.3.13.jar包,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
package common; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; public class DocumentHandler { private Configuration configuration = null ; public DocumentHandler() { configuration = new Configuration(); configuration.setDefaultEncoding( "utf-8" ); } public void createDoc(Map<String,Object> dataMap,String fileName) throws UnsupportedEncodingException { //dataMap 要填入模本的數據文件 //設置模本裝置方法和路徑,FreeMarker支持多種模板裝載方法。可以重servlet,classpath,數據庫裝載, //這里我們的模板是放在template包下面 configuration.setClassForTemplateLoading( this .getClass(), "/template" ); Template t= null ; try { //test.ftl為要裝載的模板 t = configuration.getTemplate( "fctestpaper.ftl" ); } catch (IOException e) { e.printStackTrace(); } //輸出文檔路徑及名稱 File outFile = new File(fileName); Writer out = null ; FileOutputStream fos= null ; try { fos = new FileOutputStream(outFile); OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8" ); //這個地方對流的編碼不可或缺,使用main()單獨調用時,應該可以,但是如果是web請求導出時導出后word文檔就會打不開,并且包XML文件錯誤。主要是編碼格式不正確,無法解析。 //out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile))); out = new BufferedWriter(oWriter); } catch (FileNotFoundException e1) { e1.printStackTrace(); } try { t.process(dataMap, out); out.close(); fos.close(); } catch (TemplateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //System.out.println("---------------------------"); } } |
然后是準備數據調用就行,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
package com.havenliu.document; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { /** * @param args * @throws UnsupportedEncodingException */ public static void main(String[] args) throws UnsupportedEncodingException {; Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put( "xytitle" , "試卷" ); int index = 1 ; // 選擇題 List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>(); //題目 List<Map<String, Object>> list11 = new ArrayList<Map<String, Object>>(); //答案 index = 1 ; for ( int i = 0 ; i < 5 ; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put( "xzn" , index + "." ); map.put( "xztest" , "( )操作系統允許在一臺主機上同時連接多臺終端,多個用戶可以通過各自的終端同時交互地使用計算機。" ); map.put( "ans1" , "A" + index); map.put( "ans2" , "B" + index); map.put( "ans3" , "C" + index); map.put( "ans4" , "D" + index); list1.add(map); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put( "fuck" , index + "." ); map1.put( "abc" , "A" + index); list11.add(map1); index++; } dataMap.put( "table1" , list1); dataMap.put( "table11" , list11); // 填空題 List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> list12 = new ArrayList<Map<String, Object>>(); index = 1 ; for ( int i = 0 ; i < 5 ; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put( "tkn" , index + "." ); map.put( "tktest" , "操作系統是計算機系統中的一個___系統軟件_______,它管理和控制計算機系統中的___資源_________." ); list2.add(map); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put( "fill" , index + "." ); map1.put( "def" , "中級調度" + index); list12.add(map1); index++; } dataMap.put( "table2" , list2); dataMap.put( "table12" , list12); // 判斷題 List<Map<String, Object>> list3 = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> list13 = new ArrayList<Map<String, Object>>(); index = 1 ; for ( int i = 0 ; i < 5 ; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put( "pdn" , index + "." ); map.put( "pdtest" , "復合型防火墻防火墻是內部網與外部網的隔離點,起著監視和隔絕應用層通信流的作用,同時也常結合過濾器的功能。" ); list3.add(map); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put( "judge" , index + "." ); map1.put( "hij" , "對" + index); list13.add(map1); index++; } dataMap.put( "table3" , list3); dataMap.put( "table13" , list13); // 簡答題 List<Map<String, Object>> list4 = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> list14 = new ArrayList<Map<String, Object>>(); index = 1 ; for ( int i = 0 ; i < 5 ; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put( "jdn" , index + "." ); map.put( "jdtest" , "說明作業調度,中級調度和進程調度的區別,并分析下述問題應由哪一級調度程序負責。" ); list4.add(map); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put( "answer" , index + "." ); map1.put( "xyz" , "說明作業調度,中級調度和進程調度的區別,并分析下述問題應由哪一級調度程序負責。" ); list14.add(map1); index++; } dataMap.put( "table4" , list4); dataMap.put( "table14" , list14); MDoc mdoc = new MDoc(); mdoc.createDoc(dataMap, "E:/outFile.doc" ); } } |
注意上面map中的key必須和模板中的對應,否則會報錯。效果如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。