freeMarker图片导出word的demo

最近有一个需求,导出图片到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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值