背景:根据模板填充导出word
一.添加依赖
1 <dependency>
2 <groupId>org.freemarker</groupId>
3 <artifactId>freemarker</artifactId>
4 <version>2.3.20</version>
5 </dependency>
二.定义word模板
三.将生成的word另存为xml格式
四.修改内容
1.将对应的内容填写成${}的形式 2.针对列表循环的内容,需要加list标签,如下图所示 这些全部弄好之后,模板就制作完了,修改文件为.ftl即可,然后把模板放入到项目中。
五.编写代码
package com.sinosoft.springbootplus.demandConfirm.application.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sinosoft.springbootplus.schedule.service.JwPtkScheduleCourseDetailByClassIdApiService;
import com.sinosoft.springbootplus.schedule.vo.JwPtkScheduleCourseDetailByClassId;
import com.sinosoft.springbootplus.util.UUIDUtil;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
@Slf4j
public class HqDemandConfirmExportServiceImpl extends BaseServiceImpl<HqDemandConfirmMapper, HqDemandConfirm> implements HqDemandConfirmExportService {
private static final String ENCODING = "UTF-8";
private static Configuration cfg = new Configuration();
static {
cfg.setClassForTemplateLoading(HqDemandConfirmExportServiceImpl.class, "/templates");
cfg.setEncoding(Locale.getDefault(), ENCODING);
cfg.setObjectWrapper(new DefaultObjectWrapper());
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
}
public static Template getTemplate(String templateFileName) throws IOException {
return cfg.getTemplate(templateFileName, ENCODING);
}
public static File crateFile(Map<String, Object> data, String templateFileName, String outFilePath) {
Writer out = null;
File outFile = new File(outFilePath);
try {
Template template = getTemplate(templateFileName);
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
}
out = new OutputStreamWriter(new FileOutputStream(outFile), ENCODING);
template.process(data, out);
out.flush();
log.info("由模板文件" + templateFileName + "生成" + outFilePath + "成功.");
} catch (Exception e) {
log.error("由模板文件" + templateFileName + "生成" + outFilePath + "出错");
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
log.error("关闭Write对象出错", e);
e.printStackTrace();
}
}
return outFile;
}
@Override
public void getHqDemandConfirmPageList(Long confirmId) throws Exception {
Map<String,Object> data = new HashMap<>();
data.put("checkOutStartDate",checkOutStartDate);
data.put("checkOutEndDate",checkOutEndDate);
data.put("teachAssistant",baseInfo.getTeachAssistant());
data.put("phone",hqDemandConfirms.getPhone());
List<HqDemandConfirm> hqDemandConfirmExport = hqDemandConfirmMapper.getHqDemandConfirmExport(queryWrapperDemandConfirms);
List<Long> list = new ArrayList<>();
for (HqDemandConfirm demandConfirm : hqDemandConfirmExport) {
list.add(demandConfirm.getClassId());
}
List<Map<String, Object>> scheduleCourseDetailMaps = new ArrayList<>();
for (Long aLong : list) {
List<JwPtkScheduleCourseDetailByClassId> jwPtkScheduleCourseDetailByClassId = jwPtkScheduleCourseDetailByClassIdApiService.getJwPtkScheduleCourseDetailByClassId(aLong);
for (JwPtkScheduleCourseDetailByClassId ptkScheduleCourseDetailByClassId : jwPtkScheduleCourseDetailByClassId) {
Map<String,Object> scheduleCourseDetail = new HashMap<>();
String courseDate = sdf2.format(ptkScheduleCourseDetailByClassId.getCourseDate());
scheduleCourseDetail.put("courseDate",courseDate);
scheduleCourseDetail.put("courseBeginTime",ptkScheduleCourseDetailByClassId.getCourseBeginTime());
scheduleCourseDetail.put("courseEndTime",ptkScheduleCourseDetailByClassId.getCourseEndTime());
scheduleCourseDetail.put("classroom",ptkScheduleCourseDetailByClassId.getClassroom());
scheduleCourseDetail.put("remark",ptkScheduleCourseDetailByClassId.getRemark());
scheduleCourseDetailMaps.add(scheduleCourseDetail);
}
}
List<Map<String, Object>> detailMaps = new ArrayList<>();
for (HqDormUseInfo hqDormUseInfo : roomInfos) {
Map<String,Object> dormUseInfo = new HashMap<>();
if (hqDormUseInfo.getDormType() == 0){
dormUseInfo.put("dormType","单间");
}else if (hqDormUseInfo.getDormType() == 1){
dormUseInfo.put("dormType","标间");
}else {
dormUseInfo.put("dormType","套间");
}
dormUseInfo.put("dormNum",hqDormUseInfo.getDormNum());
dormUseInfo.put("remarks",hqDormUseInfo.getRemarks());
detailMaps.add(dormUseInfo);
}
data.put("detailMaps",detailMaps);
data.put("detailMealMaps",detailMealMaps);
data.put("accommodationRequirements",hqDemandConfirms.getAccommodationRequirements());
data.put("uenueRequirements",hqDemandConfirms.getUenueRequirements());
data.put("mealRequirements",hqDemandConfirms.getMealRequirements());
data.put("hqConfirmExplain",hqConfirmExplain.getExplainS());
data.put("scheduleCourseDetailMaps",scheduleCourseDetailMaps);
data.put("userName",hqDemandConfirms.getUserName());
data.put("email",hqDemandConfirms.getEmail());
crateFile(data, "后勤保障需求单2.ftl", "D:/后勤保障需求单"+ UUIDUtil.getUUID() +".doc");
}
}
|