标签:
利用freemarker生成word文档步骤:
导入jar包:freemarker-2.3.20.jar
新建word模板,调整好样式,
word模板另存为2003 xml格式,
xml中需要替换的内容用${param}替换,param为参数名称,例如:${name } ,传入参数map.put("name","张三");
编写java代码,替换xml文档,导出。
1 /** 2 *@文件名称: CreateWord.java 3 *@日期: 2016-5-19 4 *@Copyright: 5 */ 6 package com.billionsfinance.contractmanagement.archives.util; 7 8 import java.io.File; 9 import java.io.FileInputStream; 10 import java.io.FileOutputStream; 11 import java.io.IOException; 12 import java.io.InputStream; 13 import java.io.OutputStreamWriter; 14 import java.io.Writer; 15 import java.util.Map; 16 17 import javax.servlet.ServletOutputStream; 18 import javax.servlet.http.HttpServletRequest; 19 import javax.servlet.http.HttpServletResponse; 20 21 import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; 22 23 import freemarker.template.Configuration; 24 import freemarker.template.Template; 25 26 /** 27 * TODO(生成word模板) . 28 * 29 * @author xiaofei.xian 30 * @version v 1.0 31 * @ClassName: CreateWord 32 * @date: 2016年5月17日 下午3:18:10 33 */ 34 public class CreateWord { 35 36 /** The configuration. */ 37 private Configuration configuration = null; 38 39 /** 40 * Instantiates a new CreateWord. 41 */ 42 public CreateWord() { 43 configuration = new Configuration(); 44 configuration.setDefaultEncoding("utf-8"); 45 } 46 47 /** 48 * TODO(生成word模板并下载) . 49 * 50 * @author xiaofei.xian 51 * @param request the request 52 * @param response the response 53 * @param templateName 模板名称 WEB-INF\templates\下 例:contractCertifi.ftl 54 * @param wordName 生成word文档名称 55 * @param dataMap 数据绑定 56 * @throws Exception void 返回值 57 * @date: 2016年5月17日 下午3:18:34 58 * @Title: CreateFile 59 */ 60 public void CreateFile(HttpServletRequest request, HttpServletResponse response, 61 String templateName, String wordName, Map<String, Object> dataMap) 62 throws Exception { 63 String name = "temp" + (int)(Math.random() * 100000) + ".doc"; 64 File f = new File(name); 65 //设置FreeMarker的模版文件位置 66 configuration.setServletContextForTemplateLoading(request.getSession().getServletContext(), 67 "WEB-INF/templates"); 68 Template t = null; 69 //要装载的模板 70 t = configuration.getTemplate(templateName); 71 Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8"); 72 t.process(dataMap, w); 73 InputStream fin = null; 74 ServletOutputStream out = null; 75 f.setReadOnly(); 76 f.setWritable(false); 77 try { 78 // 调用工具类WordGenerator的createDoc方法生成Word文档 79 fin = new FileInputStream(f); 80 81 response.setCharacterEncoding("utf-8"); 82 response.setContentType("application/msword"); 83 // 设置浏览器以下载的方式处理该文件默认名为resume.doc 84 response.addHeader("Content-Disposition", 85 "attachment;filename=" + new String(wordName.getBytes("gb2312"), "ISO8859-1") 86 + ".doc"); 87 88 out = response.getOutputStream(); 89 byte[] buffer = new byte[512]; // 缓冲区 90 int bytesToRead = -1; 91 // 通过循环将读入的Word文件的内容输出到浏览器中 92 while ((bytesToRead = fin.read(buffer)) != -1) { 93 out.write(buffer, 0, bytesToRead); 94 } 95 } 96 finally { 97 if (w != null) { 98 w.close(); 99 } 100 if (fin != null) { 101 fin.close(); 102 } 103 if (out != null) { 104 out.close(); 105 } 106 if (f != null) { 107 f.delete(); // 删除临时文件 108 } 109 } 110 } 111 112 /** 113 * TODO(获取图片的BASE64编码) . 114 * 115 * @author xiaofei.xian 116 * @param filename the filename 117 * @return String 118 * @throws IOException String 返回值 119 * @date: 2016年5月17日 下午4:12:19 120 * @Title: getImageString 121 */ 122 public static String getImageString(String filename) 123 throws IOException { 124 InputStream in = null; 125 byte[] data = null; 126 try { 127 in = new FileInputStream(filename); 128 data = new byte[in.available()]; 129 in.read(data); 130 in.close(); 131 } 132 catch (IOException e) { 133 throw e; 134 } 135 finally { 136 if (in != null) { 137 in.close(); 138 } 139 } 140 return Base64.encode(data); 141 } 142 }
标签:
原文地址:http://www.cnblogs.com/rememberme/p/freemarker_word.html