freeMarker图片导出word的demo

来源:互联网 发布:淘宝的限时秒杀在哪里 编辑:程序博客网 时间:2024/06/11 17:12

最近有一个需求,导出图片到word,在网上查了好多资料,根据别人的进行修改以后写了一个简单的demo。

步骤:

1,使用office新建一个word文档,加入图片,排好版。然后保存为word2003xml,刚开始保存为word xml图片没有解析出来。

2,登录http://tool.oschina.net/codeformat/xml/将xml文档格式化。

3,修改后缀为ftl.

4,新建工程,导入freemarker-2.3.22.jar(也可以是其他版本的),导入模板ftl.

5,新建类:ExportWordUtil,内容如下,

package com.ftl;import java.awt.image.BufferedImage;import java.io.*;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.imageio.ImageIO;import freemarker.template.Configuration;import freemarker.template.Template;import freemarker.template.TemplateException;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;public class ExportWordUtil {private Configuration configuration = null;public ExportWordUtil() {configuration = new Configuration();configuration.setDefaultEncoding("utf-8");}@SuppressWarnings("deprecation")public void exPortLog() { // 要填入模本的数据文件Map dataMap = new HashMap();getData(dataMap);//获取模板configuration.setClassForTemplateLoading(this.getClass(),"/template");Template t = null;try { // export_log.ftl为要装载的模板 t = configuration.getTemplate("export_log.ftl");t.setEncoding("utf-8");} catch (IOException e) {e.printStackTrace();}// 输出文档路径及名称File outFile = new File("D:/test.doc");Writer out = null;try {out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));} catch (Exception e1) {e1.printStackTrace();}try {t.process(dataMap, out);out.close();} catch (TemplateException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}/** * 注意dataMap里存放的数据Key值要与模板中的参数相对应  * @param dataMap *  */@SuppressWarnings("unchecked")private void getData(Map dataMap) { dataMap.put("image", getImageStr()); System.out.println(getImageStr()); dataMap.put("title", "这是一个测试");  } public String getImageStr() { String imgFile = "D:/test4.png"; InputStream in = null; byte[] data = null; try { in = new FileInputStream(imgFile); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);// BASE64Encoder encoder = new sun.misc.BASE64Encoder();      //    BASE64Decoder decoder = new sun.misc.BASE64Decoder();    //   File f = new File("D:/test2.jpg");             //        BufferedImage bi;      //        try {      //            bi = ImageIO.read(f);      //            ByteArrayOutputStream baos = new ByteArrayOutputStream();      //            ImageIO.write(bi, "jpg", baos);      //            byte[] bytes = baos.toByteArray();      //                  //            return encoder.encodeBuffer(bytes).trim();      //        } catch (IOException e) {      //            e.printStackTrace();      //        }      //        return null;   } public void base64StringToImage(String base64String){            try {            BASE64Encoder encoder = new sun.misc.BASE64Encoder();          BASE64Decoder decoder = new sun.misc.BASE64Decoder();             byte[] bytes1 = decoder.decodeBuffer(base64String);                                ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);                BufferedImage bi1 =ImageIO.read(bais);                File w2 = new File("test4.png");//可以是jpg,png,gif格式                ImageIO.write(bi1, "jpg", w2);//不管输出什么格式图片,此处不需改动            } catch (IOException e) {                e.printStackTrace();            }        }    }

6,新建测试类,

TestTemplate,内容如下:

package com.ftl;public class TestTemplate {public static void main(String[] args) {ExportWordUtil ewu = new ExportWordUtil();ewu.exPortLog();System.out.println("success");}}

运行测试类,可以看到效果。貌似这样做的图片大小是固定的

0 0
原创粉丝点击