Java生成word 并导出简历

先创建一个word文件 内容改成你要生成的格式 再生成.ftl文件
话不多说 上代码!
@Override
public void exportword(HttpServletResponse response, HttpServletRequest request, Long id) throws IOException {
String conf_filename = this.getClass().getClassLoader().getResource("").getPath()
+ System.getProperty(“file.separator”) + “fdfs_client.conf”;
//** 初始化配置文件 //
Configuration configuration = new Configuration();
//
* 设置编码 //
configuration.setDefaultEncoding(“utf-8”);
//
* 我的ftl文件是放在D盘的**//*
String fileDirectory = Tools.getvalue(“project_maindir”);
//String fileDirectory = “D:\”;
//** 加载文件 //
configuration.setDirectoryForTemplateLoading(new File(fileDirectory));
//
* 加载模板 //
Template template = configuration.getTemplate(“AA.ftl”);
//
* 准备数据 //
Map<String,Object> dataMap = new HashMap<String, Object>();
Map<String,Object> map = new HashMap<String, Object>();
//根据会员ID查询出简历基本信息
ResumeInformation resumeVo = resumeInformationMapper.findResumePreviewByMid(id);
if(resumeVo.getPostnature()!=null&&!"".equals(resumeVo.getPostnature())){
String[] ids = resumeVo.getPostnature().split(",");
//根据IDS查询出参数名称并列显示
String postnature = paramDictionaryMapper.findParamById(ids);
resumeVo.setPostnatureString(postnature);
}
if(resumeVo.getUpdateTime()!=null){
resumeVo.setUpdateHours(DateUtils.DateToString(resumeVo.getUpdateTime(), DateStyle.YYYY_MM_DD));
}
//根据会员ID查询出工作经验`
List workExperienceList = workExperienceMapper.findWorkExperienceListByMid(id);
//根据会员ID查询出项目经验
List projectExperienceList = projectExperienceMapper.findProjectExperienceListByMid(id);
//根据会员ID查询出教育经历
List educationExperienceList = educationExperienceMapper.findEducationExperienceListByMid(id);
//根据会员ID查询出培训经历
List trainExperienceList = trainExperienceMapper.findTrainExperienceListByMid(id);
//根据会员ID查询出作品网站
List worksWebsiteList = worksWebsiteMapper.findWorksWebsiteListByMid(id);
map.put(“resumeVo”, resumeVo);
if(resumeVo.getPhotoUrl()!=null){
String fileData = resumeVo.getPhotoUrl();
fileData = fileData.substring(7, fileData.length());
String firstName=fileData.substring(fileData.lastIndexOf(’/’)+1);
int indexOf = fileData.indexOf("/");
String url2 = fileData.substring(indexOf+1);
int indexOf2 = url2.indexOf("/");
String group = url2.substring(0, indexOf2);
String path = url2.substring(indexOf2+1);
//循环下载路径
String urlshared = App.downloadFiles(group, path, conf_filename, request,firstName);
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(urlshared);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(in != null){
in.close();
}
}
//
* 进行base64位编码 //
BASE64Encoder encoder = new BASE64Encoder();
dataMap.put(“img”,encoder.encode(data));
}else{
//
* 图片路径 //
String imagePath = request.getSession().getServletContext().getRealPath("/resources/images/defaultHeadimg.png");//获取图像在项目中的路径
//
* 将图片转化为**//*
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(imagePath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(in != null){
in.close();
}
}
//** 进行base64位编码 //
BASE64Encoder encoder = new BASE64Encoder();
dataMap.put(“img”,encoder.encode(data));
}
//名称
dataMap.put(“name”,resumeVo.getRealName());
//性别
dataMap.put(“sex”,resumeVo.getSexs());
//年龄
dataMap.put(“age”,resumeVo.getAge());
//工作年限
if(resumeVo.getWorkExe()!=null){
if(resumeVo.getWorkExe()==0){
dataMap.put(“workExe”,“一年以下工作经验”);
}else{
dataMap.put(“workExe”,resumeVo.getWorkExe()+“年工作经验”);
}
}else{
dataMap.put(“workExe”,“无工作经验”);
}
//现居住市级
dataMap.put(“nowCity”,resumeVo.getNowCity());
//隐藏中间四位手机号
String s = resumeVo.getPhone();
String replaceAll = s.replaceAll("(\d{3})\d{4}(\d{4})", "$1
***$2");
//手机号
dataMap.put(“phone”, replaceAll);
//邮箱
if(resumeVo.getEmail()!=null && resumeVo.getEmail().length()>0){
StringBuilder email = new StringBuilder();
int num;
String str = “”;
email.append(resumeVo.getEmail());
num = email.indexOf("@");
for (int i = 0; i < num; i++) {
str = str + “*”;
}
email = email.replace(3,num,str);
System.out.println(email);
dataMap.put(“email”, email);
}else{
dataMap.put(“email”, “”);
}
//工作性质
dataMap.put(“postnature”,resumeVo.getPostnatureString());
//期望工作地点
dataMap.put(“workCity”,resumeVo.getWorkCity());
//期望职业
dataMap.put(“posiName”, resumeVo.getPosiName());
//期望行业
dataMap.put(“industryName”,resumeVo.getIndustryName());
//期望月薪
dataMap.put(“offers”,resumeVo.getOffers());
//自我评价
dataMap.put(“explored”,resumeVo.getExplored());
//分割个人标签
if(resumeVo.getPersonTag()!=null){
String personTag = resumeVo.getPersonTag().replaceAll(","," “);
dataMap.put(“personTag”,personTag);
}else{
dataMap.put(“personTag”,”");
}
//工作经验
dataMap.put(“workExperienceList”, workExperienceList);
//根据会员ID查询出项目经验
dataMap.put(“projectExperienceList”, projectExperienceList);
//根据会员ID查询出教育经历
dataMap.put(“educationExperienceList”, educationExperienceList);
//根据会员ID查询出培训经历
dataMap.put(“trainExperienceList”, trainExperienceList);
//根据会员ID查询出作品网站
dataMap.put(“worksWebsiteList”, worksWebsiteList);
//Map<String,Map<String,Object>> dataMaps = new LinkedHashMap<String,Map<String,Object>>();
//List list=studymetarialdetailService.findstudymetarials(sId,docId);
//循环遍历list
/*for (Studymetarialdetail studymetarialdetail : list) {
String fileData = studymetarialdetail.getFirstPath();
String description = studymetarialdetail.getDescrip()==null?"":studymetarialdetail.getDescrip();

		String firstName=fileData.substring(fileData.lastIndexOf('/')+1);
		int indexOf = fileData.indexOf("/");
		String url2 = fileData.substring(indexOf+1);
		int indexOf2 = url2.indexOf("/");
		String group = url2.substring(0, indexOf2);
		String path = url2.substring(indexOf2+1);
		//循环下载路径
		String urlshared = App.downloadFiles(group, path, conf_filename, request,firstName);
	    InputStream in = null;
        byte[] data = null;
	        try {
	            in = new FileInputStream(urlshared);
	            data = new byte[in.available()];
	            in.read(data);
	            in.close();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }finally {
	            if(in != null){
	                in.close();
	            }
	        }
	        //** 进行base64位编码 **//*
	        BASE64Encoder encoder = new BASE64Encoder();
	        //** 在ftl文件中有${textDeal}这个标签**//*
	        //dataMap.put("textDeal","一下省略一万字");
	        //** 图片数据**//*
	        dataMap.put("描述"+i+":"+description,encoder.encode(data));
	        i++;
	}
	dataMaps.put("dataMaps", dataMap);*/
	//dataMap.put("images", images);
	//dataMap.put("titles", titles);
      //** 指定输出word文件的路径 **//*
	long currentTime = System.currentTimeMillis();
	//SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
	//Date date = new Date(currentTime);
		String filename = "JM"+currentTime+".doc";
        String outFilePath = Tools.getPropertie("upload.down")+filename;
      //  String downpath = Tools.getvalue("upload.down");
        File docFile = new File(outFilePath);
        FileOutputStream fos = new FileOutputStream(docFile);
        Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"),10240);
        try {
			template.process(dataMap, out);;
		} catch (TemplateException e) {
			e.printStackTrace();
		}

        if(out != null){
            out.close();
        }
        Tools.fileDown(response, outFilePath, filename);
	}

最后一步是文件下载 贴上代码
/**
* 文件流下载
* @throws IOException
*/
public static void fileDown(HttpServletResponse resp,String filepath,String filename) throws IOException{
resp.setContentType(“application/force-download”);
InputStream in = new FileInputStream(filepath);
resp.setHeader(“Content-Disposition”, “attachment;filename=”" + filename + “”");
resp.setContentLength(in.available());
OutputStream out = resp.getOutputStream();
byte[] b = new byte[1024];
int len = 0;
while((len = in.read(b))!=-1){
out.write(b, 0, len);
}
out.flush();
out.close();
in.close();
}

发布了2 篇原创文章 · 获赞 0 · 访问量 267

猜你喜欢

转载自blog.csdn.net/weixin_43682921/article/details/94637150
今日推荐