自定义模板
修改xml变成ftl格式 ,将ftl格式文件放入templates下,用idea打开在ctrl +A 然后ctrl +alt+L格式化
折叠起来看到有多个pkg
主要是修改document.xml这个pkg
自定义文字模板
自定义表格
改变为
自定义图片的模板
编码
@ApiOperation(value = "导出方式三", notes = "导出文件")
@PostMapping("/exportMThree")
public void exportMThree(HttpServletRequest request, HttpServletResponse response) {
Calendar calendar = Calendar.getInstance();// 取当前日期。
String imagePath = "src/main/resources/images/fenmian.png";
//获得数据
List<ItemInfo> itemInfos = new ArrayList<>();
List<Map<String, Object>>itemMaps = new ArrayList<>();
for (int i=0;i<10;i++){
ItemInfo itemInfo=new ItemInfo();
itemInfo.setName("电脑"+i);
itemInfo.setCount(i);
itemInfo.setPrice((i+1)*1000);
Map<String, Object> itemMap = new HashMap<String, Object>();
itemMap.put("name", "电脑"+i);
itemMap.put("count", i);
itemMap.put("price", (i+1)*1000);
itemMaps.add(itemMap);
}
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "李明");
map.put("testValue", "freeMarker");
map.put("date", new Date().toString());
map.put("listIn", itemMaps);
map.put("image",this.getImageBase(imagePath));
try {
WordUtils wordUtils=new WordUtils();
wordUtils.exportToWordMTree(map,"templates/tup.ftl","用freemarker生成Word文档.doc",request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
//获得图片的base64码
@SuppressWarnings("deprecation")
public String getImageBase(String src) {
if(src==null||src==""){
return "";
}
File file = new File(src);
if(!file.exists()) {
return "";
}
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(file);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
WordUtils
/**
* @Author: created by yeluo
* @Description:
* @param: [dataMap, ftlPath, request, response]
* @return: org.springframework.core.io.InputStreamSource
* @Date: 13:01 2022/3/10
*/
public InputStreamSource exportToWordMTree(Map<String, Object> dataMap, String ftlPath,String outFileName, HttpServletRequest request, HttpServletResponse response) throws Exception {
// 纠正编码
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("utf-8");
// ** 初始化配置文件**//*
Configuration configuration = new Configuration();
// ** 设置编码 **//*
configuration.setDefaultEncoding("utf-8");
//********
configuration.setClassForTemplateLoading(WordUtils.class, "/");
// 加载模板,通过Word转XML文件转换过来的
Template template = configuration.getTemplate(ftlPath);
//方式三
//文件名称
InputStreamSource file = createDocAndDonwload(dataMap, template, "D:/doc_freeM/", outFileName);
InputStream fin = file.getInputStream();
ServletOutputStream out;
response.setContentType("application/msword");
// 设置浏览器以下载的方式处理该文件
response.setHeader("content-disposition", "attachment;filename=document.doc");
out = response.getOutputStream();
// 缓冲区
byte[] buffer = new byte[512];
int bytesToRead;
// 通过循环将读入的Word文件的内容输出到浏览器中
while ((bytesToRead = fin.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
fin.close();
if (out != null) {
out.close();
}
return file;
}
@SuppressWarnings("unchecked")
public InputStreamSource createDocAndDonwload(Map dataMap,Template template,String filePath,String fileName)
throws IOException, TemplateException {
//输出文件
File outFile = new File(filePath+File.separator+fileName);
//如果输出目标文件夹不存在,则创建
if (!outFile.getParentFile().exists()){
outFile.getParentFile().mkdirs();
}
//将模板和数据模型合并生成文件
Writer outD = new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8");
Writer out = new BufferedWriter(outD);
//生成文件
template.process(dataMap, out);
//生成随机的名称
StringWriter outW = new StringWriter();
Writer outBody = new BufferedWriter(outW, 10240);
//将数据输出到模板
template.process(dataMap, outBody);
//关闭流
out.flush();
out.close();
outBody.flush();
outBody.close();
return new ByteArrayResource(outW.toString().getBytes(StandardCharsets.UTF_8));
}
测试