Java用freemarker導出word


概述

最近一個項目要導出word文檔,折騰老半天,發現還是用freemarker的模板來搞比較方便省事,現總結一下關鍵步驟,供大家參考,這里是一個簡單的試卷生成例子。

詳細

最近一個項目要導出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包,代碼如下:

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("---------------------------");  
    }  
}

然后是准備數據調用就行,代碼如下:

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必須和模板中的對應,否則會報錯。

三、項目導入

下載附件后,解壓后,看到如下目錄

image.png

將Document項目導入eclipse即可。導入后的效果如下:

image.png

會在e盤生成文件

image.png

四、運行效果

打開生成的outFile.doc則看到如下的樣子:

五、freemarker語法

Freemarker頁面語法:http://blog.csdn.net/thismonth/article/details/5194982

 

注:本文著作權歸作者,由demo大師發表,拒絕轉載,轉載需要作者授權

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM