使用freemarker生成word文档
第一步
- 你需要准备一个word模板,就是word文件,.doc或者.docx都可以,如果是图片你需要提前在word文件中插插入图片具体看以下图片, 是一个占位符,实际作用跟 j s p 的 {}是一个占位符,实际作用跟jsp的 是一个占位符,实际作用跟jsp的{}的差不多就是传递数据,里面的变量随便写后期传的时候名字一致就可以
- 把这个word文件另存为xml格式,具体的另存在word中操作,然后使用在线xml工具,推荐使用在线 XML 格式化 | 菜鸟工具 (runoob.com),这一步主要是在xml文件中的是乱码,使用在线工具格式化就能编辑了,其次找到插入照片的地方,将二进制数据删除换成占位符 变量随便写,最好使用 {}变量随便写,最好使用 变量随便写,最好使用{image},最后创建xxx.ftl文件将这个编辑好的内容复制到xxx.ftl文件,注意会出现占位符不在一起的情况手动编辑一下就可以了,将多余的删除
占位符不在一起的情况
第二步
下面两个类直接复制
/**
* 将图片转码为base64
* @author: xxl
* @since: 2023/6/8
* @description: 图片工具类
*/
public class ImageUtil {
/**
* 将图片内容转换成Base64编码的字符串
* @param imageFile 图片文件的全路径名称
* @return 转换成Base64编码的图片内容字符串
*/
public static String getImageBase64String(String imageFile) {
if (ObjectUtil.isNull(imageFile)) {
return "";
}
File file = new File(imageFile);
if (!file.exists()) {
return "";
}
InputStream is = null;
byte[] data = null;
try {
is = new FileInputStream(file);
data = new byte[is.available()];
is.read(data);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return Base64.encodeBase64String(data);
}
}
/**
* word文档工具类
* @author xxl
* @since 2023/6/7
*/
@Component
public class WordUtil {
/**
* 使用FreeMarker自动生成Word文档
* @param dataMap 生成Word文档所需要的数据
* @param fileName 生成Word文档的全路径名称,带文件名
*/
public void generateWord(Map<String, Object> dataMap,String fileName,String basePath,String templateName) throws Exception {
// 设置FreeMarker的版本和编码格式
Configuration configuration = new Configuration(new Version("2.3.30"));
configuration.setDefaultEncoding("UTF-8");
// 设置FreeMarker生成Word文档所需要的模板的路径 C:\\测试word文件夹
configuration.setDirectoryForTemplateLoading(new File(basePath));
// 设置FreeMarker生成Word文档所需要的模板
Template t = configuration.getTemplate(templateName, "UTF-8");
// 创建一个Word文档的输出流
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), StandardCharsets.UTF_8));
//FreeMarker使用Word模板和数据生成Word文档
t.process(dataMap, out);
out.flush();
out.close();
}
}
测试类,下面的map的键对应上面占位符中变量,注意导出的文件的后缀是.doc,否则打不开
/**
* 测试word工具类
* @author xxl
* @since 2023/6/8
*/
@SpringBootTest
class WordUtilTest {
@Resource
WordUtil wordUtil;
public String templateName = "word模板.ftl";
public String basePath = "D:\\Program Files (x86)\\idea\\IDEAproject\\work\\KYDX-Registration-System\\src\\main\\resources\\word\\";
@Test
void generateWord() throws Exception {
//导出的位置+文件名
final String name = "C:\\Users\\xxl\\Desktop\\test\\报名表.doc";
//图片名
final String imagePath = "D:\\Program Files (x86)\\idea\\IDEAproject\\work\\KYDX-Registration-System\\src\\main\\resources\\static\\photos\\gril7.jpeg";
HashMap<String, Object> map = new HashMap<>();
map.put("realName","许小亮");
map.put("gender","男");
map.put("nation","中国矿业大学专职辅导员应聘报名表");
map.put("position","中国矿业大学专职辅导员应聘报名表");
map.put("idCardNumber","中国矿业大学专职辅导员应聘报名表");
map.put("englishProficiency","中国矿业大学专职辅导员应聘报名表");
map.put("politicalOutlook","中国矿业大学专职辅导员应聘报名表");
map.put("partyJoiningTime","中国矿业大学专职辅导员应聘报名表");
map.put("phoneNumber","中国矿业大学专职辅导员应聘报名表");
map.put("email","中国矿业大学专职辅导员应聘报名表");
map.put("homeLocation","中国矿业大学专职辅导员应聘报名表");
map.put("currentLocation","中国矿业大学专职辅导员应聘报名表");
map.put("civilServiceExam","中国矿业大学专职辅导员应聘报名表");
map.put("personalExperience","中国矿业大学专职辅导员应聘报名表");
map.put("studentLeadershipExperience","中国矿业大学专职辅导员应聘报名表");
map.put("academicHonors","中国矿业大学专职辅导员应聘报名表");
map.put("disciplinaryRecord","中国矿业大学专职辅导员应聘报名表");
map.put("image", ImageUtil.getImageBase64String(imagePath));
wordUtil.generateWord(map,name,basePath,templateName);
}
}
导出结果