FreeMark 填充World
FreeMaker模板填充Word下载
- Word工具类
/**
* Word导出工具类.
*
* @author Pang 2020-10-23
*/
public class WordUtil {
/**
* 生成word文档.
*
* @param dataMap 填充数据
* @param templateName 模板名称
* @return 文件
*/
public static File createWord(Map dataMap, String templateName) {
try {
//创建配置实例
Configuration configuration = new Configuration(Configuration.getVersion());
//设置编码
configuration.setDefaultEncoding("UTF-8");
//ftl模板文件
configuration.setTemplateLoader(new ClassTemplateLoader(WordUtil.class, "/template/"));
//获取模板
Template template = configuration.getTemplate(templateName);
//输出临时文件
File outFile = File.createTempFile("temp", ".doc");
//将模板和数据模型合并生成文件
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
//生成文件
template.process(dataMap, out);
//关闭流
out.flush();
out.close();
return outFile;
} catch (Exception exception) {
exception.printStackTrace();
return null;
}
}
/**
* 导出文件.
*
* @param response 响应
* @param map 填充的数据
* @param templateName 模板名称
* @throws IOException 读写异常
*/
public static void exportMillCertificateWord(HttpServletResponse response,
Map map, String templateName) throws IOException {
File file = null;
InputStream fin = null;
ServletOutputStream out = null;
try {
String fileName = "下载文件名" + System.currentTimeMillis() + ".doc";
file = WordUtil.createWord(map, templateName);
fin = new FileInputStream(file);
response.setCharacterEncoding("utf-8");
response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment;filename*=utf-8''"
.concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
out = response.getOutputStream();
// 缓冲区
byte[] buffer = new byte[1024];
int bytesToRead = -1;
// 通过循环将读入的Word文件的内容输出到浏览器中
while ((bytesToRead = fin.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
} finally {
if (fin != null) {
fin.close();
}
if (out != null) {
out.close();
}
if (file != null) {
// 删除临时文件
file.deleteOnExit();
}
}
}
-
Word填充数据工具类
/** * 申请信息word模板填充数据工具类. * * @author Pang 2020-10-24 */ public class WordDataUtil implements Serializable { /** * 向word模板填充数据. * * @param wordVo word导出信息实体类 * @return 数据集 */ public static Map<String, Object> setDataToWord(WordVO wordVo) { converEmptyNullToString(applyWordVO); HashMap<String, Object> dataMap = new HashMap<>(); dataMap.put("companyName", applyWordVO.getCompanyName()); dataMap.put("companyAddress", applyWordVO.getCompanyAddress()); return dataMap; } /** * 对象转换 将null转换成"" . * * @param object 对象 */ public static void converEmptyNullToString(Object object) { try { //利用反射获取类的所有属性 Field[] fs = object.getClass().getDeclaredFields(); for (int i = 0; i < fs.length; i++) { Field f = fs[i]; //设置属性可以访问 f.setAccessible(true); Object val = f.get(object); //得到此属性的类型 String type = f.getType().toString(); if (type.endsWith("String")) { if (!"" .equals(val) && null == val) { f.set(object, ""); } } } } catch (SecurityException exception) { exception.printStackTrace(); } catch (IllegalArgumentException exception) { exception.printStackTrace(); } catch (IllegalAccessException exception) { exception.printStackTrace(); } }

浙公网安备 33010602011771号