1. 简介
Word从2003开始支持XML格式,操作流程
- 先用office2003或者2007编辑好word的样式,然后另存为xml,
- 将xml翻译为FreeMarker模板,
- 最后用java来解析FreeMarker模板并输出Doc。
经测试这样方式生成的word文档完全符合office标准,样式、内容控制非常便利,打印也不会变形,生成的文档和office中编辑文档完全一样。
2. 集成使用
-
新建项目
-
引入相关pom依赖
FreeMarker
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
-
在application.propertes中添加相应配置
## Freemarker 配置 ##模版存放路径(默认为 classpath:/templates/) spring.freemarker.template-loader-path=classpath:/templates/ ##是否生成缓存,生成环境建议开启(默认为true) spring.freemarker.cache=false ##编码 spring.freemarker.charset=UTF-8 spring.freemarker.check-template-location=true ##content-type类型(默认为test/ht