项目场景:
有5个病例模板,模板是固定的,只需要更改其中的参数并且下载
直接上代码
<!--导出word--> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId> <version>2.0.2</version> </dependency>
public ResultUtil<Map<String, String>> export1(MedicalRecordIdReq recordIdReq,HttpServletResponse response) throws Exception {
HashMap<String, String> map = MapUtil.newHashMap(StatusCodeUtil.NUMBER_ONE);
recordIdReq = AESUtil.decryptToObj(recordIdReq.getData(), MedicalRecordIdReq.class);
MedicalRecord medicalRecord = medicalRecordService.getById(recordIdReq.getId());
//模板位置
String fileName = System.getProperty("os.name").toLowerCase().contains("win") ? "D:/" : "/www/usr/words/";
//生成文件位置
String newFileName = System.getProperty("os.name").toLowerCase().contains("win") ?"D:/words/"+recordIdReq.getId()+".docx" : "/www/usr/words/words/"+recordIdReq.getId()+".docx";
try {
//输出文档路径及名称
File outFile = new File(newFileName);
//Configuration 用于读取ftl文件
Configuration configuration = new Configuration(new Version("2.3.0"));
configuration.setDefaultEncoding("utf-8");
configuration.setDirectoryForTemplateLoading(new File(fileName));//ftl文件目录
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("dentalPosition", medicalRecord.getDentalPosition());
dataMap.put("implantBrandAndType", medicalRecord.getImplantBrandAndType());
dataMap.put("mCode", medicalRecord.getMCode());
dataMap.put("dCode", medicalRecord.getDCode());
dataMap.put("bCode", medicalRecord.getBCode());
dataMap.put("lCode", medicalRecord.getLCode());
dataMap.put("nCode", medicalRecord.getNCode());
dataMap.put("model", medicalRecord.getMCode());
//以utf-8的编码读取ftl文件
Template template = configuration.getTemplate("model5.ftl", "utf-8");
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
template.process(dataMap, out);
out.close();
File file = new File(newFileName);
MultipartFile multipartFile = getMultipartFile(file);
//这里将本地文件上传到了七牛云获取外链
String path = QiNiuUtil.customUpload1(2, QiNiuUtil.accessKey, QiNiuUtil.secretKey, QiNiuUtil.bucket, QiNiuUtil.domain, multipartFile);
map.put("path", path);
System.out.println(path);
} catch (Exception e) {
e.printStackTrace();
}
return ResultUtil.setEncryptData(map);
}
总结
方法是很多的,希望大家多多分享