標籤:
閱讀目錄
- 1.word 裡面調整好排版,包括你想產生的動態部分,還有一些不用產生的規則性的文字
- 2. 將 word 文檔儲存為 xml
- 3.用 Firstobject free XML edito 開啟,將你需要動態產生的欄位打上標記,${} 這樣就可以
- 4.將 xml 檔案更改尾碼名 為 .ftl, 然後引用到你的項目中
項目中需要用 java 程式產生doc 檔案,百度一番,FreeMarker 的評價比較高,FreeMarker是一個用Java語言編寫的模板引擎,它基於模板來產生文本輸出,至於想詳細瞭解 FreeMarker 的請去問百度.....
這篇博文主要是總結自己在用網上例子時遇到的坑。吃水不忘挖井人,還是要感謝分享技術的前輩。
原文連結:
http://www.360doc.com/content/13/0731/10/13247663_303740756.shtml [博主部落格很漂亮,贊一個]
參照過的例子連結:
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
主要思路如下:
回到頂部1.word 裡面調整好排版,包括你想產生的動態部分,還有一些不用產生的規則性的文字
回到頂部2. 將 word 文檔儲存為 xml
回到頂部3.用 Firstobject free XML edito 開啟,將你需要動態產生的欄位打上標記,${} 這樣就可以
FreeMarker 還支援需要豐富的標記,如果你想展示更複雜和豐富的內容,都可以實現;
Firstobject free XML edito 友情下載連結:
http://www.cnblogs.com/know-life-death/archive/2012/02/01/2334742.html
回到頂部4.將 xml 檔案更改尾碼名 為 .ftl, 然後引用到你的項目中
需要注意的問題:
a. word 版本不能低於 2003 ,因為 2003 才開始支援 xml;
b.用 Firstobject free XML edito 開啟 要編輯的 xml 檔案時,記得xml 不要放在含有中文路徑的目錄中【編輯器會無響應,然後你知道的.....】。
實現的代碼如下:
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"); // FTL檔案所存在的位置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. 代碼資料庫為 mysql ,將查詢到的資料,動態填入到wod 中;
b.template.process(),接受一個 Map 和 輸入資料流做為入參,Map 既是你需要動態產生到 doc 裡面的資料,欄位名必須與你在 .ftl 裡面定義的一致;
c. 我覺得資料的來源可以很多,程式計算的結果,資料庫儲存的資料,頁面點擊的資料.........
d. 如果你想利用這段代碼,需要有一個 .ftl 檔案,並且在你的項目 src 目錄下面建立 template 目錄;
e.我不知道怎樣把 .ftl 檔案上傳上來,如果需要做例子的,請在下面留言,我發給你。
Java 動態產生 複雜 .doc檔案