freeMarker生成word,excel文檔

    之前導出文檔一直使用poi技術,這個項目使用freemarker技術,而後看了一下,發現比poi簡單多了。因而發表一下。
java

        FreeMarker是一款模板引擎: 即一種基於模板和要改變的數據, 並用來生成輸出文本(HTML網頁、電子郵件配置文件源代碼等)的通用工具。 它不是面向最終用戶的,而是一個Java類庫,是一款程序員能夠嵌入他們所開發產品的組件。程序員

        首,把你要導出的word文檔另存爲xml格式,而後使用記事本將它打開,將動態生成的代碼用el表達式(jstl標籤)替換。apache

示例以下:ide

word文檔工具

姓名:aaathis

性別:bbbspa

另存爲xml後打開,修改以下orm

<w:t>姓名:${name}</w:t></w:r></w:p><w:p><w:pPr><w:jc w:val="left"/><w:rPr><w:rFonts w:ascii="微軟雅黑" w:h-ansi="微軟雅黑" w:fareast="微軟雅黑" w:cs="微軟雅黑" w:hint="fareast"/><w:sz w:val="24"/><w:sz-cs w:val="24"/><w:lang w:val="EN-US" w:fareast="ZH-CN"/></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:ascii="微軟雅黑" w:h-ansi="微軟雅黑" w:fareast="微軟雅黑" w:cs="微軟雅黑" w:hint="fareast"/><w:sz w:val="24"/><w:sz-cs w:val="24"/><w:lang w:val="EN-US" w:fareast="ZH-CN"/></w:rPr><w:t>性別:${sex}</w:t>



用${name}和${sex}代替aaa和bbb。而後在後臺編寫java代碼。xml

package com.freemarkes.word;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class WordHandler {
      private Configuration configuration = null;
      Log logger = LogFactory.getLog(WordHandler.class);
  
      public WordHandler() {
          configuration = new Configuration();
          configuration.setDefaultEncoding("UTF-8");
      }
     /*
      * configuration跟文件路徑有關係,是相對的。
      */
     private Template getTemplate(String templatePath, String templateName) throws IOException {
         configuration.setClassForTemplateLoading(this.getClass(), templatePath);
         /***
         * public void setClassForTemplateLoading(Class clazz, String pathPrefix);
 * public void setDirectoryForTemplateLoading(File dir) throws IOException;
 * public void setServletContextForTemplateLoading(Object servletContext, String path);
  *看名字也就知道了,分別基於類路徑、文件系統以及Servlet Context。
          ***/
         Template t = null;
         t = configuration.getTemplate(templateName);
         t.setEncoding("UTF-8");
         return t;
     }
 
     
     public void write(String templatePath, String templateName, Map dataMap, Writer out) throwsException {
         try {
             Template t = getTemplate(templatePath, templateName);
             t.process(dataMap, out);
         } catch (Exception e) {
             logger.error(e);
         } finally{
         out.close();
         }
     }
public static void main(String[] args) throws Exception {
  Map map=getMap();
  WordHandler handler = new WordHandler();
  Writer out = new OutputStreamWriter(new FileOutputStream("D:\\chaoslee.doc"), "UTF-8");
  handler.write("", "chaoslee.xml", map, out);
}
public static Map getMap(){
Map map = new HashMap();
map.put("name", "chaoslee");
map.put("sex", "男");
return map;
}
 }


運行main方法就能夠導出word文檔了。代碼見附件htm

個人xml文檔是複製的片斷,因此可能不能使用,若是有使用的人,仍是本身另存爲一下。

這個博客不能複製圖片讓我非常爲難啊,仍是 我不會複製。複製的都是空白的。

Excel方法也差很少。