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方法也差很少。

相关文章
相关标签/搜索