freemarker下载word文档功能的实现以及注意点
来源:互联网 发布:c语言编程简单的贪吃蛇 编辑:程序博客网 时间:2024/05/18 12:36
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; }
阅读全文
0 0
- freemarker下载word文档功能的实现以及注意点
- 用FREEMARKER打印、预览、下载WORD文档
- spring mvc freemarker下载word文档
- URLSession 下载以及注意点
- FreeMarker生成word文档
- freeMarker生成word文档
- Freemarker生成word文档
- freemarker生成Word文档
- freemarker导出word文档
- freemarker导出word文档
- JAVA 数据导出Word文档模板的功能实现
- JSP实现word文档的上传,在线预览,下载
- 实现Freemarker生成word文档,内容可二次写入
- 二分查找的各种情况实现以及一些注意点
- winform实现文档下载功能
- 用 Freemarker 生成 word 文档
- 用 Freemarker 生成 word 文档
- 使用FreeMarker生成word文档
- 指针数组与数组指针、函数指针与函数指针数组
- UEditor
- 树、二叉树、完全二叉树的简单理解
- mongoDB磁盘碎片整理
- js获取当前时间的近12个月,当前日期的未来7天
- freemarker下载word文档功能的实现以及注意点
- ORACLE的两种优化模式
- 相约梧桐,不见凤凰
- Chrome流量监控
- python3按最后访问时间顺序获取文件夹中文件列表
- javamail调用异常,Authentication failed
- Git 分支
- git笔记
- Zend Studio使用教程之在Zend Studio中调试PHP(1/5)