最近做的招聘小程序,要在后台生成word简历,做了个demo简单记录一下。
word模板
先找一个word简历模板,另存为Word 2003 XML文档,然后用文本编辑器打开
替换对应的字段名称,例如:jobStatus --> ${jobStatus},保存。然后在项目用创建一个freemarker模板,将整个XML复制过去。
模板完成。
依赖
org.springframework.boot
spring-boot-starter-freemarker
核心代码
package com.hlhlo.web.admin.service.impl;
import com.hlhlo.cloud.api.job.dto.CareerObjectiveDto;
import com.hlhlo.cloud.api.job.dto.EducationDto;
import com.hlhlo.cloud.api.job.dto.ResumeDto;
import com.hlhlo.cloud.api.job.dto.WorkExperienceDto;
import com.hlhlo.cloud.api.job.entity.Projects;
import com.hlhlo.cloud.api.job.mapper.ResumeMapper;
import com.hlhlo.web.admin.service.ResumeService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
@Service(value = "adminResumeService")
public class ResumeServiceImpl implements ResumeService {
@Autowired
private ResumeMapper resumeMapper;
@Override
public void downloadResume(Long userId, HttpServletResponse response) {
ResumeDto resumeDto = resumeMapper.findUserResume(userId);
Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
configuration.setDefaultEncoding("UTF-8");
Map dataMap = new HashMap<>();
dataMap.put("name", ifNull(resumeDto.getRealName()));
dataMap.put("sex", ifNull(resumeDto.getSex()));
dataMap.put("jobStatus", ifNull(resumeDto.getJobStatus()));
dataMap.put("mobile", ifNull(resumeDto.getMobile()));
if (resumeDto.getBirthday() != null) {
dataMap.put("birthday", ifNull(DateFormatUtils.format(resumeDto.getBirthday(), "yyyy-MM-dd")));
} else {
dataMap.put("birthday", "");
}
dataMap.put("email", ifNull(resumeDto.getEmail()));
if (resumeDto.getStartWorkTime() != null) {
dataMap.put("startWorkTime", ifNull(DateFormatUtils.format(resumeDto.getStartWorkTime(), "yyyy-MM-dd")));
} else {
dataMap.put("startWorkTime", "");
}
dataMap.put("city", ifNull(resumeDto.getCity()));
dataMap.put("info", ifNull(resumeDto.getInfo()));
List