首页 > 编程语言 > 详细

Java导出Word利用freemarker(含图片)

时间:2016-01-12 21:10:10      阅读:199      评论:0      收藏:0      [点我收藏+]

制作Word模版

建议使用高版本的office做,尽量不要用WPS做,生成xml会出现乱码

编码要统一,推荐UTF-8

建好模板,将模板另存为xml格式,建议原来模板不要删,xml的如果后期打不开,还有原版参考

(编辑器网上有人说firstobject XML Editor这个好用,本次没用到直接用的是Editplus)

需要freemarker-2.3.13.jar包

  1 package com.xu.word.export;
  2 
  3 import java.io.BufferedWriter;
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.FileOutputStream;
  7 import java.io.IOException;
  8 import java.io.InputStream;
  9 import java.io.OutputStreamWriter;
 10 import java.io.Writer;
 11 import java.util.ArrayList;
 12 import java.util.HashMap;
 13 import java.util.List;
 14 import java.util.Map;
 15 
 16 import org.junit.Test;
 17 
 18 import sun.misc.BASE64Encoder;
 19 import freemarker.template.Configuration;
 20 import freemarker.template.DefaultObjectWrapper;
 21 import freemarker.template.Template;
 22 import freemarker.template.TemplateException;
 23 import freemarker.template.TemplateExceptionHandler;
 24 
 25 public class DocumentHandler {
 26     private Configuration configuration = null;
 27 
 28     public DocumentHandler() {
 29         configuration = new Configuration();
 30         // 设置默认编码为UTF-8
 31         configuration.setDefaultEncoding("UTF-8");
 32     }
 33     
 34     /**
 35      * 
 36      * @param dir 目录名称
 37      * @param fileName 文件名
 38      * @param savePath 要保存的路径
 39      * @param sDate 数据(键值对形式,Map形式最好)
 40      */
 41     public void createDocForMap(String dir, String fileName, String savePath, Map dataMap) {
 42         // 要填入模本的数据文件 dataMap
 43         // 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
 44         // 这里我们的模板是放在com.xu.word.export.template包下面???好像加不加这句话都一样
 45         configuration.setClassForTemplateLoading(this.getClass(), "/template");
 46         Template t = null;
 47         try {
 48             // 从什么地方加载freemarker模板文件
 49             configuration.setDirectoryForTemplateLoading(new File(dir));
 50 
 51             // 设置对象包装器
 52             configuration.setObjectWrapper(new DefaultObjectWrapper());
 53             // 设置异常处理器
 54             configuration
 55                     .setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
 56             // 装载的模板
 57             t = configuration.getTemplate(fileName, "UTF-8");
 58         } catch (IOException e) {
 59             e.printStackTrace();
 60         }
 61         // 输出文档路径及名称
 62         File outFile = new File(savePath);
 63         Writer out = null;
 64         try {
 65             out = new BufferedWriter(new OutputStreamWriter(
 66                     new FileOutputStream(outFile), "utf-8"));
 67         } catch (Exception e1) {
 68             e1.printStackTrace();
 69         }
 70 
 71         try {
 72             t.process(dataMap, out);
 73         } catch (TemplateException e) {
 74             e.printStackTrace();
 75         } catch (IOException e) {
 76             e.printStackTrace();
 77         }
 78     }
 79     
 80     /**
 81      * 将图片进行BASE64编码
 82      * @param imgFilePath 图片路径
 83      * @return
 84      */
 85     private static String getImageStr(String imgFilePath) {
 86         String imgFile = imgFilePath;
 87         InputStream in = null;
 88         byte[] data = null;
 89         try {
 90             in = new FileInputStream(imgFile);
 91             data = new byte[in.available()];
 92             in.read(data);
 93             in.close();
 94         } catch (IOException e) {
 95             e.printStackTrace();
 96         }
 97         BASE64Encoder encoder = new BASE64Encoder();
 98         return encoder.encode(data);
 99     }
100     
101     /**
102      * 测试方法
103      */
104     @Test
105     public void testImportWord(){
106         long start = System.currentTimeMillis();
107         
108         Map data = new HashMap();
109         data.put("abcd1", "尼玛");
110         // 集合数据
111         List l = new ArrayList();
112         l.add("aaaaa"); // 此处可以进行换行加到模板中 <w:br />
113         l.add("abbbb");
114         l.add("cccc");
115         l.add("ddd");
116         data.put("abcd1", l);
117         
118         data.put("abcd2", "尼玛1");
119         data.put("abcd3", "尼玛2");
120         data.put("abcd4", "尼玛3");
121         data.put("abcd5", "尼玛4");
122         data.put("abcd6", "尼玛5");
123         data.put("abcd7", "尼玛6");
124         data.put("abcd8", "尼玛7");
125         data.put("abcd9", "尼玛8");
126         // 目录获取有点繁琐了
127         String root = DocumentHandler.class.getClassLoader().getResource("").getPath() + "com/xu/word/export/template/";
128         
129         data.put("image", getImageStr(root+"/1.jpg"));
130         DocumentHandler dh = new DocumentHandler();
131         dh.createDocForMap(root, "tpl.ftl", "E:/outFile4.doc", data);
132         
133         long end = System.currentTimeMillis();
134         System.out.println("导出成功,用时" + (end - start));
135     }
136 
137 }

如果你希望在Word文档中插入图片,可以把Word另存为的XML文件中代表图片的那个很长的字符串( BASE64编码 的字符串)换成一个占位符,在将要插入Word文档的图片对象转换成BASE64编码的字符串,用该字符串替换掉占位符就可以了

Java导出Word利用freemarker(含图片)

原文:http://www.cnblogs.com/zhuolu/p/5125580.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!