FreeMarker实现Word导出
来源:互联网 发布:怎么查淘宝是不是黑号 编辑:程序博客网 时间:2024/06/06 20:02
1、freemarker版本:
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.9</version></dependency>
3、将word文件,另存为 xml文件(我用的是wps)
此时需注意:很有可能这个动作会将 文件中写好的 ${AGE} 解析出错 变成:
xxxxxxx${xxxxx
xxxAGExxxx
xxx}xxxxxxxxxx……
的样子(文件已经修改,没有截图。。。)
这个时候,你要做的就是 将 中间多余的字符 删除即可,还原 ${AGE}
4、更改xml文件后缀 为:.ftl
5、将模板文件 放入项目中特定位置
6、代码中:我只使用到了Map,来存放数据
7、生成代码:
/** * 生成word文件 * @param dataMap 原始数据 * @param templateName 文件名称 * @param filePath 文件路径 */public static void createWord(Map dataMap,String templateName,String filePath){ Writer out = null; try { //创建配置实例 Configuration configuration = new Configuration(); //设置编码 configuration.setDefaultEncoding("UTF-8"); //ftl模板文件统一放至/ftl/createCode configuration.setClassForTemplateLoading(ExcelUtil.class,"/ftl/createCode/"); //获取模板 Template template = configuration.getTemplate(templateName); //输出文件 File outFile = new File(filePath); //如果输出目标文件夹不存在,则创建 if (!outFile.getParentFile().exists()){ outFile.getParentFile().mkdirs(); } //将模板和数据模型合并生成文件 out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8")); //生成文件 template.process(dataMap, out); //关闭流 out.flush(); } catch (Exception e) { e.printStackTrace(); }finally {if(out != null ){ try {out.close();} catch (IOException e) {e.printStackTrace();}}} }
8、再提供 下载的repsonse:
response.setContentType("application/octet-stream"); response.setHeader("Content-disposition", "attachment; filename=demo.doc");
ServletOutputStream os = response.getOutputStream();通过 response的os输出流,将文件写回客户端,实现下载。
9、测试。
另:想实现Word导出时单元格内容换行:加标签 <w:br />
阅读全文
0 0
- FreeMarker实现Word导出
- 使用freemarker实现word导出
- java freemarker模板 实现word文件导出
- 【java】模板freemarker实现word生成导出
- 使用freemarker 导出word
- FreeMarker + xml 导出word
- FreeMarker + xml 导出word
- freemarker 导出word
- FreeMarker 导出word步骤
- 使用FreeMarker导出Word
- FreeMarker + xml 导出word
- 使用freemarker导出Word
- 使用freemarker导出Word
- freemarker 导出word
- 使用 freemarker 导出 word
- 使用freemarker导出word
- JAVA FreeMarker导出word
- java导出word(Freemarker)
- matplotlib基础——add_subplot()
- Spring那些事(1)-eclipse+maven开发web项目环境配置详解
- 微信开发者 工具快捷键大全
- [生成函数+容斥+FFT] BZOJ3771: Triple
- Spring boot扫描不到controller注解
- FreeMarker实现Word导出
- vue全家桶项目
- 作业调度之先来先服务算法C语言实现
- WEB开发学习网站地址
- json字符串、json对象、数组 三者之间的转换
- Windows7连接VPN(PPTP类型)
- Mac下使用iTerm2让SSH记录远程服务器账号和密码
- 欢迎使用CSDN-markdown编辑器
- 过滤器(Filter)和监听器(Listener)