freemarker下载word文档功能的实现以及注意点
2017-11-20 10:51
405 查看
public Object createWord() { String netPath = ""; String fileFullPath = null; //Configuration实例化 Configuration configuration = new Configuration(); configuration.setDefaultEncoding("UTF-8"); Map<String, Object> dataMap = new HashMap<String, Object>(); getData(dataMap); configuration.setClassForTemplateLoading(this.getClass(), "/cn/com/liandisys/policy/common/util/template");//模板文件所在路径 Template t = null; try { t = configuration.getTemplate("Report.ftl"); //获取模板文件 } catch (IOException e) { e.printStackTrace(); } //导出文件 long timeSpan = new Date().getTime(); String fileName = timeSpan + ".doc"; fileFullPath = this.getFileGeneratePath() + FILESEPARATOR + fileName; File file = new File(fileFullPath); Writer out = null; try { try { netPath = commonService.exportWordFile(); } catch (Exception e) { e.printStackTrace(); netPath = "{\"path\":\"\"}"; } out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { t.process(dataMap, out); //将填充数据填入模板文件并输出到目标文件 } catch (TemplateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "{\"path\":\"" + netPath + "\"}"; }
private static final String FILETYPE_WORDSUFFIX = ".doc";
//传递参数
private void getData(Map<String, Object> dataMap) {
String param1 = " 参数一 ";
String param2 = " 参数二 ";
... 参数有几个就创建几个
dataMap.put("param1", param1);
dataMap.put("param2", param2);
... 参数有几个就添加几个
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
for (int i = 0; i < list.size(); i++) {
Map<String,Object> map = new HashMap<String,Object>();
map.put("number", i);
map.put("content", "内容"+i);
list.add(map);
dataMap.put("list", list);
}
}
private static final String FILESEPARATOR = System.getProperty("file.separator");
//导出word
public String exportWordFile() {
String netPath = "";
String fileFullPath = null;
long timeSpan = new Date().getTime();
String fileName = timeSpan + FILETYPE_WORDSUFFIX;
fileFullPath = this.getFileGeneratePath() + FILESEPARATOR + fileName;
ServletContext ctx = ContextLoader.getCurrentWebApplicationContext().getServletContext();
netPath = ctx.getContextPath() + "/fileDownload.do?name=" + timeSpan + "&suffix=" + FILETYPE_WORDSUFFIX;
File file = new File(fileFullPath);
file.getParentFile().mkdirs();
try {
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
}
return netPath;
}
相关文章推荐
- ionic热更新的实现以及引导用户下载新版应用功能实现和注意事项
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- JSP页面实现图片、PDF字节流的显示,Word、Excel、Zip字节流的下载功能的实现
- android开发之集成zxing,二维码,以及扫描二维码的功能实现。带源代码下载
- 使用点聚 weboffice 以及vsto、 web service 实现word 的自动化文档处理
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- spring mvc freemarker下载word文档
- ASP.NET实现word文档在线预览功能代码
- 使用Word的Mail Merge(邮件合并)功能实现批文档\邮件\通知的生成
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- 使用socket套接字通过http协议实现文档下载功能
- springboot下csv文件下载需要注意得一些细节以及功能得完善
- ASP.NET MVC 拓展ViewResult实现word文档下载
- 关于论坛实现pdf,word等文档在线预览的功能探讨
- struts2实现图片的上传以及下载功能
- php实现word文档在线浏览功能,配置安装手记
- 使用poi操作word文档实现套打功能
- asp.net实现word文档在线预览功能代码
- django admin 中实现word文档下载
- [置顶] android开发之集成zxing,二维码,以及扫描二维码的功能实现。带源代码下载