第一步:创建word模板文件
word中新建word文档 设计如下
把上面的文件用word打开,然后另存为word xml文件,
用notepad++打开xml文件
使用 thymeleaf语法修改文件 修改后放到templates下
第二步使用 thymeleaf 生成word 文档
String url = "D:/";
Context context = new Context();
context.setVariable("company", "卓越软件");
byte[] bytes1 = FileUtil.readFileByBytes("C:\\d.gif");
String image1 = Base64Util.encode(bytes1);
context.setVariable("image", image1);
Map<String,String> map1 = new HashMap<String,String>();
map1.put("name", "张三");
map1.put("age", "123123");
map1.put("sex", "男");
Map<String,String> map2 = new HashMap<String,String>();
map2.put("name", "李四");
map2.put("age", "123123");
map2.put("sex", "男");
List<Map<String,String>> newlist = new ArrayList<>();
newlist.add(map1);
newlist.add(map2);
Map<String, Object> dataMap = new HashMap<>();
context.setVariable("userList", newlist);
response.setContentType("multipart/form-data");
//2.设置文件头:最后一个参数是设置下载文件名(假如我们叫a.pdf)
response.setHeader("Content-Disposition", "attachment;fileName=" + "xx.doc");
Writer out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "utf-8"),10240);
TemplateUtils.process("company.html", context, out);
out.close();
相关函数:
package com.ruoyi;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import org.springframework.beans.factory.annotation.Autowired;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
public class TemplateUtils {
@Autowired
public static final TemplateEngine engine = ApplicationContextHolder.getBean(TemplateEngine.class);
/**
* 生成静态文件
* @param freeTempName 模板名称
* @param context 数据内容
* @param outFilePath 输出路径
* @return
*/
public static boolean process(String freeTempName,Context context,String outFilePath) {
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(outFilePath);
engine.process(freeTempName, context,fileWriter);
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
public static boolean process(String freeTempName,Context context,Writer writer) {
engine.process(freeTempName, context,writer);
return true;
}
}
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
applicationContext = ctx;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
return (T) applicationContext.getBean(name);
}
}
另一篇文章
Java用freemarker导出word
https://www.cnblogs.com/demodashi/p/8458097.html