使用freemarker
模板動態導出word
文件html
環境java
版本git
2003
.doc
格式2.1.9
word 2003
新建.doc
模板.xml
文件,格式化代碼,並檢查是否存在變量分離問題,如圖調整後github
.ftl
模板freemarker
文件freemarker
模板引擎<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
複製代碼
freemarker
# 設置freemarker
freemarker:
allow-request-override: false
# 開發過程建議關閉緩存
cache: true
check-template-location: false
charset: UTF-8
content-type: text/html; charset=utf-8
expose-request-attributes: false
expose-session-attributes: false
expose-spring-macro-helpers: false
request-context-attribute:
# 默認後綴就是.ftl
suffix: .ftl
template-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
操做同上,模板以下spring
@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>
複製代碼
@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 倉庫json