创建ftl模版
https://css.dandelioncloud.cn/article/details/1504007077642203138
工具类
package com.zh.users.utils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;
public class WordUtil {
/**
* 生成word文件
* @param dataMap word中需要展示的动态数据,用map集合来保存
* @param templateName word模板名称,例如:test.ftl
* @param filePath 文件生成的目标路径,例如:D:/wordFile/
* @param fileName 生成的文件名称,例如:test.doc
*/
@SuppressWarnings("unchecked")
public static File createWord(Map dataMap,String templateName,String filePath,String fileName) {
try {
//创建配置实例
Configuration configuration = new Configuration();
//决定解决下如果不配置,变量则显示为空
configuration.setClassicCompatible(true);//kong
//设置编码
configuration.setDefaultEncoding("UTF-8");
//ftl模板文件
configuration.setClassForTemplateLoading(WordUtil.class, "/templates");
//获取模板
Template template = configuration.getTemplate(templateName);
//输出文件
File outFile = new File(filePath + File.separator + fileName);
//如果输出目标文件夹不存在,则创建
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
}
//将模板和数据模型合并生成文件
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
//生成文件
template.process(dataMap, out);
//关闭流
out.flush();
out.close();
return outFile;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String createFileName() {
Random random = new Random();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(sdf.format(new Date()));
stringBuffer.append("_");
stringBuffer.append(random.nextInt(100));
String fileOnlyName = "导出word文档" + stringBuffer + ".doc";
return fileOnlyName;
}
public static void exportMillCertificateWord(HttpServletResponse response, Map map, String filePath, String templateName) throws IOException {
File file = null;
InputStream fin = null;
ServletOutputStream out = null;
try {
String fileName = WordUtil.createFileName();
file = WordUtil.createWord(map, templateName, filePath, fileName);
fin = new FileInputStream(file);
response.setCharacterEncoding("utf-8");
response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment;filename*=utf-8''".concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
out = response.getOutputStream();
byte[] buffer = new byte[512];
int bytesToRead = -1;
while ((bytesToRead = fin.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fin != null) {
fin.close();
}
if (out != null) {
out.close();
}
if (file != null) {
file.delete();
}
}
}
}
参考
我的测试
可以导出,没毛病
/**
* 我的测试
*
* @param response
*/
@GetMapping("/port")
public void port(String bh, String xr, HttpServletResponse response) {
try {
Map<Object, Object> dataMap = new HashMap<>();
dataMap.put("bh", bh);
dataMap.put("xr", xr);
WordUtil.exportMillCertificateWord(response, dataMap, "E:", "mb2.ftl");
} catch (IOException e) {
e.printStackTrace();
}
}