Java利用word模板导出word包含图片循环数组
2023-11-23 23:53:17 905浏览
导出简历,word里有表格,电子照,循环数据,挺麻烦,使用word模板可实现。
先看效果图:
引入依赖:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
制作word模板:
1.制作muban.doc,变量用${xx}代替 ,可以根据实际情况调整表格的大小样式等;
2,将该word另存为muban.xml ,不是修改后缀名哦,我用的wps,如下图
打开muban.xml
修改电子照变量,删除上面的base64字符串(可以先格式化该xml),修改如下:
分情况:如果是wps转的xml直接把base64替换成成${photo}如下:
如果是Microsoft Office Word转成的xml需要修改成如下代码,如下:
<w:binData w:name="wordml://1.jpg">${photo}</w:binData>
红线框多了,注意节点对称
接下来是循环list的需要修改,直接搜索循环的字段,在外层加上<#list eli as t></#list>包裹即可
代码如下: 注意节点对称,图片里打错字了,懒得改了
调用导入word的代码即可createword()
说明图片转base64即可,使用的是word模板转成的xml。我是把xml和电子照直接放到webapp里的某文件夹,获取xml路径电子照路径等文件请以实际情况为准。记住变量要和xml里的对应哦
package com.jf.s.util;
import com.jf.s.bean.Education;
import com.jf.s.bean.ProjectExp;
import com.jf.s.bean.Resume;
import com.jf.s.bean.WorkExp;
import com.sun.DateUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import sun.misc.BASE64Encoder;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class ImportWord {
private Configuration configuration = null;
public ImportWord(){
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
}
public String createWord(HttpServletRequest request, Resume r, List<Education> eli,List<WorkExp> wli,List<ProjectExp> pli) throws IOException {
String path = request.getSession().getServletContext().getRealPath(Sys.Upimg.parentPack);
String path2 = request.getSession().getServletContext().getRealPath("");
Map<String,Object> dataMap=new HashMap<String,Object>();
System.out.println(path2+r.getPhoto());
dataMap.put("photo",imageToBase64Str(path2+r.getPhoto()));
dataMap.put("realname",r.getRealname()==null?"未填写":r.getRealname()+"\n\t");
dataMap.put("sex",r.getSex()==null?"未填写":r.getSex());
dataMap.put("age",r.getAge()==null?"未填写":r.getAge());
dataMap.put("birth",r.getBirth()==null?"未填写":r.getBirth());
dataMap.put("mobile",r.getMobile()==null?"未填写":r.getMobile());
dataMap.put("email",r.getEmail()==null?"未填写":r.getEmail());
dataMap.put("education",r.getEducation()==null?"未填写":r.getEducation());
dataMap.put("schoole",r.getSchool()==null?"未填写":r.getSchool());
dataMap.put("major",r.getMajor()==null?"未填写":r.getMajor());
String wage="未填写";
if(r.getWantWage()!=null){
if(r.getWantWage().equals("-1")) wage="面议";
else wage=r.getWantWage()+"K";
}
dataMap.put("wantWage",wage);
dataMap.put("wantCity",r.getWantCity()==null?"未填写":r.getWantCity());
dataMap.put("workTime",r.getWorkTime()==null?"未填写":r.getWorkTime());
String state="未填写";
if(r.getState()!=null){
if(r.getState().equals("1")) state="在职找工作";
else state="已离职";
}
dataMap.put("state",state);
dataMap.put("wantJob",r.getWantJob()==null?"未填写":r.getWantJob());
dataMap.put("skills",r.getSkills()==null?"未填写":r.getSkills());
dataMap.put("interest",r.getInterest()==null?"未填写":r.getInterest());
dataMap.put("certificate",r.getCertificate()==null?"未填写":r.getCertificate());
dataMap.put("evaluation",r.getEvaluation()==null?"未填写":r.getEvaluation());
dataMap.put("eli",eli);
dataMap.put("wli",wli);
dataMap.put("pli",pli);
configuration.setDirectoryForTemplateLoading(new File(path));//模板文件所在路径
Template t=null;
try {
t = configuration.getTemplate("resume.xml"); //获取模板文件
} catch ( Exception e) {
e.printStackTrace();
}
String fn=UUID.randomUUID().toString().replace("-","").substring(0,10)+".doc";
File outFile = new File(path+"/words/"+fn); //导出文件
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
t.process(dataMap, out); //将填充数据填入模板文件并输出到目标文件
} catch ( Exception e) {
e.printStackTrace();
}
return Sys.Upimg.parentPack+"/words/"+fn;
}
public static String imageToBase64Str(String imgFile) {
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(imgFile);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
// 加密
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
}
好博客就要一起分享哦!分享海报
此处可发布评论
评论(0)展开评论
暂无评论,快来写一下吧
展开评论
他的专栏
他感兴趣的技术