Springboot中使用freemarker动态生成word文档
文章目录
- freemarker模板动态生成word文档
- 前言
- 准备
- 简单模板准备
- <一> `word 2003` 新建`.doc` 模板
- <二> 另存为`.xml` 文件,格式化代码,并检查是否存在变量分离问题,如图
- <三> 重命名为`.ftl`模板`freemarker`文件
- Springboot导出简单word
- 使用`freemarker`模板引擎
- 配置`freemarker`
- 将模板`UserInfo.flt `文件放入项目
- 测试`Controller`代码
- `Swagger`测试
- 默认保存在项目根目录
- 数据成功导出得到`word`
- 复杂模板word导出
- 模板准备
- Controller测试
- `freemarker` 遍历
- 导出效果
- 导出带图片Word
- 模板准备
- Controller
- `Swagger`测试
- 导出效果
- `demo`源码
freemarker模板动态生成word文档
前言
使用freemarker
模板动态导出word
文件
准备
环境
- IntellJ IDEA 2018.2
- SringBoot 2.1.9
版本
- Word
2003
.doc
格式 - spring-boot-starter-freemarker
2.1.9
- Word
简单模板准备
<一> word 2003
新建.doc
模板
<二> 另存为.xml
文件,格式化代码,并检查是否存在变量分离问题,如图
调整后
<三> 重命名为.ftl
模板freemarker
文件
Springboot导出简单word
使用freemarker
模板引擎
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency>
配置freemarker
# 设置freemarkerfreemarker:allow-request-override: false# 开发过程建议关闭缓存cache: truecheck-template-location: falsecharset: UTF-8content-type: text/html; charset=utf-8expose-request-attributes: falseexpose-session-attributes: falseexpose-spring-macro-helpers: falserequest-context-attribute:# 默认后缀就是.ftlsuffix: .ftltemplate-loader-path: classPath:/templates/code/
将模板UserInfo.flt
文件放入项目
测试Controller
代码
@PostMapping("user/doc")@ResponseBody@ApiOperation(value="导出用户doc", httpMethod = "POST",produces="application/json",notes = "导出用户doc")public ResultBean exportDoc() throws IOException{Configuration configuration = new Configuration();configuration.setDefaultEncoding("utf-8");configuration.setClassForTemplateLoading(this.getClass(), "/templates/code");Template template = configuration.getTemplate("UserInfo.ftl");Map<String, Object> dataMap = new HashMap<>();dataMap.put("name","gaolei");dataMap.put("id","02201");dataMap.put("code","251525v");dataMap.put("pwd","root");dataMap.put("tel","08583552");File outFile = new File("UserInfoTest.doc");Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));try {template.process(dataMap,out);out.flush();out.close();} catch (TemplateException e) {e.printStackTrace();}return ResultBean.success();}
Swagger
测试
默认保存在项目根目录
数据成功导出得到word
复杂模板word导出
模板准备
操作同上,模板如下
Controller测试
@PostMapping("user/requireInfo")@ResponseBody@ApiOperation(value="导出用户确认信息表doc", httpMethod = "POST",produces="application/json",notes = "导出用户确认信息表doc")public ResultBean userRequireInfo() throws IOException{Configuration configuration = new Configuration();configuration.setDefaultEncoding("utf-8");configuration.setClassForTemplateLoading(this.getClass(), "/templates/code");Template template = configuration.getTemplate("need.ftl");Map<String , Object> resultMap = new HashMap<>();List<UserInfo> userInfoList = new ArrayList<>();userInfoList.add(new UserInfo("2019","安全环保处质量安全科2608室","风险研判","9:30","10:30","风险研判","风险研判原型设计","参照甘肃分公司提交的分析研判表,各个二级单位维护自己的风险研判信息,需要一个简单的风险上报流程,各个二级单位可以看到所有的分析研判信息作为一个知识成果共享。","张三","李四"));resultMap.put("userInfoList",userInfoList);File outFile = new File("userRequireInfo.doc");Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));try {template.process(resultMap,out);out.flush();out.close();return null;} catch (TemplateException e) {e.printStackTrace();}return ResultBean.success();}
freemarker
遍历
<#list userInfoList as user>获取值:${user.name} ...</#list>
导出效果
导出带图片Word
模板准备
Controller
@PostMapping("user/exportPic")@ResponseBody@ApiOperation(value="导出带图片的Word", httpMethod = "POST",produces="application/json",notes = "导出带图片的Word")public ResultBean exportPic() throws IOException {Configuration configuration = new Configuration();configuration.setDefaultEncoding("utf-8");configuration.setClassForTemplateLoading(this.getClass(), "/templates/code");Template template = configuration.getTemplate("userPic.ftl");Map<String,Object> map = new HashMap<>();map.put("name","gaolei");map.put("date","2015-10-12");map.put("imgCode",imageToString());File outFile = new File("userWithPicture.doc");Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));try {template.process(map,out);out.flush();out.close();return null;} catch (TemplateException e) {e.printStackTrace();}return ResultBean.success();}public static String imageToString() {String imgFile = "E:\\gitee\\excel-poi\\src\\main\\resources\\static\\img\\a.png";InputStream in = null;byte[] data = null;try {in = new FileInputStream(imgFile);data = new byte[in.available()];in.read(data);in.close();} catch (IOException e) {e.printStackTrace();}String imageCodeBase64 = Base64Utils.encodeToString(data);return imageCodeBase64;}
Swagger
测试
导出效果
demo
源码
详情见github 仓库
Springboot中使用freemarker动态生成word文档相关推荐
- java利用freemarker动态生成word文档及动态表格
这里写自定义目录标题 目标 准备 实现 处理模板 java实现 验证 目标 根据给定word模板,动态填充指定内容,并输出为新的word文档. 准备 1.模板: 准备一份目标格式的word文档,如:w ...
- Freemarker - 根据模板动态生成word文档
文章目录 Freemarker 根据模板动态生成word文档 Freemarker 介绍: Freemarker 使用: freemarker加载模板目录的方法 参考资料 Freemarker 根据模 ...
- 使用FreeMarker自动生成Word文档
使用FreeMarker自动生成Word文档 获取文件路径 创建一个word文档,然后再文档中在文档中写标识,对应代码中的map数据 2.将word转为xml后缀文件,再转为.ftl文件 3.保存路径 ...
- Java使用FreeMarker自动生成Word文档(带图片和表单)
Java使用FreeMarker自动生成Word文档(带图片和表单) 1 背景 2 目标效果 3 创建Word模板 3.1 创建模板文档 3.2 转换模板文档 3.3 处理模板文档中的占位符 3.4 ...
- Java-POI替换Word模板文档中的变量,生成Word文档
第一步:在项目中导入POI的jar包,以及相关的jar包. 所需的jar包列表: 下载地址:http://download.csdn.net/download/qq_34908167/10046670 ...
- 用Aspose.Words for .NET动态生成word文档中的图片或水印
1.概述 在项目中生成word文档,这个功能很普遍的,一般生成都是纯文字或是列表的比较多,便于客户打印,而要把图片也生成到word文档中的需求有些客户也是需要的,例如产品图片.这次我们介绍的是如何利用 ...
- 【.NET】用Aspose.Words for .NET动态生成word文档中的数据表格
1.概述 最近项目中有一个这样的需求:导出word 文档,要求这个文档的格式不是固定的,用户可以随便的调整,导出内容中的数据表格列是动态的,例如要求导出姓名和性别,你就要导出这两列的数据,而且这个文档 ...
- freemarker+Jfreechart生成Word文档(含图片)
这几天再弄一个报表,要统计一些信息最终的部分展示结果如下: 基本工具freemarker,jfreechart 工程的部分结构如下 与生成Word有关的类主要有FreemarkerConfigurat ...
- 动态生成word文档的靠谱方式
需求:将数据生成到word模板文件 实现方式:使用ftl做模板文件,将数据写到word文档中 1.导包 <dependency><groupId>org.freemarker& ...
最新文章
- mybatis常见错误
- Kinect 骨骼追踪数据的处理方法
- DevOps \u0026 SRE 必备技能清单
- HDU3634(矩形切割)
- 马云在《赢在中国》对创业者的经典点评
- Linux 命令(93)—— updatedb 命令
- 利用SQL语句查询数据库中具体某个字段的重复行
- pandas—显示行索引与列索引(数组或者列表)
- jmeter 登录用户并发压力测试案例_用jmeter进行多用户并发压力测试 [转]
- IDEA集成SVN代码管理常用功能
- 三点坐标确定圆心坐标
- js原生简单生成卡密例子
- Linux基础知识点总结
- j2sdk jdk jre jvm的联系与区别
- linux共用home分区,安装UOS和Deepin双系统并且共用/home分区的测试报告
- python爬虫抖音音乐_Python爬虫帮你打包下载所有抖音好听的背景音乐,还不快收藏一起听歌-后台/架构/数据库-敏捷大拇指-一个敢保留真话的IT精英社区...
- 一台台式计算机应该具有哪些设备,电脑硬件有哪些?组装一台电脑需要哪些配件详解...
- 联通在线信息科技有限公司社招!
- 【LOESS局部加权非参数回归】
- 修改材质球面板shader属性数值
热门文章
- 常用图标(icon)css下载
- 随机梯度下降算法 入门介绍(最通俗易懂)
- log是什么文件可以删除吗?log文件被删怎么恢复?
- 【Office】Visio无响应问题(打开形状样式功能区即卡死)的解决方案
- AI 智能皮影机器人
- HarmonyOS开发详解(二)——鸿蒙开发体系详解及入门实例演示运行
- mysql中的/、div的区别
- 阿里云服务器ECS接入多IP_ECS绑定多个弹性公网IP搭建教程
- s5pv210 linux,S5PV210-零基础体验uboot
- pubmedy安装不聊了_Pubmedy:显示影响因子+引用数、Sci-hub全文下载的浏览器扩展...