java word導出

須要導出的word文檔是事先準備好的一個模板,文檔數據所在的位置則必須按FreeMarker模板語法的佔位符(如:${xxx})填充,而後將word文檔保存(最好另存爲,原模板也最後保留,便於之後修改)爲xml格式的文件,而後使用文本編輯器打開檢查並修改不合法或書寫更好的FreeMarker語法。最後在後臺服務端使用FreeMarker相關的包和類讀取模板,返回模板所需的數據變量,輸出word文件便可。
導出word功能示例

一、建立word模板

先建立你須要導出具體格式和樣式的模板,數據用FreeMarker語法的佔位符佔據word模板
二、另存並編輯xml文件

建立好word模板以後,須要將word另存爲xml格式的文件,這樣易於查看和編輯爲符合FreeMarker模板語法的xml文件,保證導出word數據的準確性,具體FreeMaker語法網上有不少,這裏就不細說了。
import freemarker.template.Configuration;  
import freemarker.template.Template;  
import java.io.File;  
import java.io.IOException;  
import java.io.Writer;  
import java.util.Map;

/**
 * 
 */
public class WordUtil {

    private Configuration configuration = null;

    /**
     * 構造方法
     */
    public WordUtil() {
        try {
            configuration = new Configuration();
            configuration.setDefaultEncoding("UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根據類路徑獲取模板
     * @param templatePath
     * @param templateName
     * @return
     * @throws IOException
     */
    private Template getTemplate(String templatePath, String templateName) throws IOException {
        configuration.setDirectoryForTemplateLoading(new File(templatePath));
        Template t = configuration.getTemplate(templateName);
        t.setEncoding("UTF-8");
        return t;
    }

    /**
     * 生成word文檔
     * @param templatePath
     * @param templateName
     * @param dataMap
     * @param out
     */
    public void write(String templatePath, String templateName,
        Map<String, Object> dataMap, Writer out) {
        try {
            Template t = getTemplate(templatePath, templateName);
            t.process(dataMap, out);
            out.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

}



import com.blinkfox.util.WordUtil;  
import com.jfinal.kit.PathKit;  
import java.io.FileOutputStream;  
import java.io.OutputStreamWriter;  
import java.io.Writer;  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;

/**
 * Created by 
 */
public class ExportWordTest {

    /**
     * 構造測試數據
     * @return
     */
    public static Map<String, Object> createDatas() {
        Map<String, Object> testMap = new HashMap<String, Object>();
//      構造散數據
        testMap.put("author", "浙江火焰");
        testMap.put("date", "2015-11-20");

//      構造列表循環數據存放在ArrayList集合中
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        for (int i = 0; i < 5; i++) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("xh", (i + 1) + "");
            map.put("name", "張三" + i);
            map.put("phone", "1381111222" + i);
            list.add(map);
        }
        testMap.put("datas", list);

        return testMap;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        Map<String, Object> testMap = createDatas();
        WordUtil handler = new WordUtil();
        Writer out = null;
        try {
//          生成test.doc的word文件到某文件路徑下
            FileOutputStream fos = new FileOutputStream("/home/blinkfox/文檔/test.doc");
            out = new OutputStreamWriter(fos, "UTF-8");
            String templatePath = PathKit.getRootClassPath() + "/template/";
            handler.write(templatePath, "test.xml", testMap, out);
            System.out.println("導出成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
相關文章
相關標籤/搜索