最近做一个关于文档导出功能, 顺便学习了下freemarker,做了个关于导出带图片的word文档,模板并没有写全,只是验证代码的正确性
这只是做一个小功能,故只做了后台代码关于导出的代码,并未与前台关联,可酌情处理
首先将需要导出的word文档做处理,关于word文档最好是后缀为.doc,应为有些软件可能无法打开导出的文档,将需要修改的数据修改成${xxx}
类型的内容,例如下面的文档
修改后则变为如下类型:
后将文档另存为.xml文档,将需要修改的然后再打开xml文档,找到图片的位置,是一大段base64编码后的
代码,形如<w:binData w:name="wordml://自定义.png" xml:space="preserve">UUUA......FFFFA</w:binData>
将中间编码后的代码修改为${xxx}的类型,关于xxx的内容可以根据自己的需求修改,而后将文件关闭。将其后
缀转为 .ftl 的格式。而后上后台代码:
final static String separator = File.separator; // 系统路径分割符 public void exportSellPlan(HttpServletRequest request, HttpServletResponse response){
String evaluation = "四年的大学生涯是我人生的一大转折点。"
+ "我深深地懂得“责任,荣誉,国家”这六个字的含义。作为大学生我最基本的责任是学习。"
+ "在大学期间我认真学习,发挥自己的特长,挖掘自身的潜力,从而提高了"
+ "自身的学习能力和分析处理问题的能力,也获得了大家的认同。";
//获得数据
Map<String, Object> map = new HashMap<String, Object>();
map.put("profession", "xxx");
map.put("name", "xxx");
map.put("address","山西省xx县");
map.put("email", "xxx@163.com");
map.put("tel", "173xxx9927");
map.put("skill1", "面对不同客人,用不同的语气、不同的态度,与客户要谈得来,处处为客户着想");
map.put("skill2", "面对不同客人,用不同的语气、不同的态度,与客户要谈得来,处处为客户着想");
map.put("skill3", "面对不同客人,用不同的语气、不同的态度,与客户要谈得来,处处为客户着想");
map.put("skill4", "面对不同客人,用不同的语气、不同的态度,与客户要谈得来,处处为客户着想");
map.put("hobby", "运动,看书,上网聊天,看电影。");
map.put("evaluation", evaluation);
map.put("graduateInstitutions", "南昌航空大学");
map.put("degree", "本科");
//map.put("image", getImageBase("E:"+ separator +"photo.jpg"));
try {
WordUtils.exportMillCertificateWord(request,response,map,"简历","jianli.ftl");
} catch (IOException e) {
e.printStackTrace();
}
} //获得图片的base64码
public static String getImageBase(String src) {
if(src==null||src==""){
return "";
}
File file = new File(src);
//File file = new File(getRequest().getRealPath("/")+src.replace(getRequest().getContextPath(), ""));
if(!file.exists()) {
return "";
}
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(file);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
关于excel导出:
package com.haiyisoft.iecp.crm.web.struts; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView; import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException; public class WordUtils extends BaseAction{
//配置信息,代码本身写的还是很可读的,就不过多注解了
private static Configuration configuration = null;
//这里注意的是利用WordUtils的类加载器动态获得模板文件的位置
// private static final String templateFolder = WordUtils.class.getClassLoader().getResource("../../").getPath() + "WEB-INF/templetes/";
private static final String templateFolder = "D:/uepstudio_uep/workspace/iecp/src/com/haiyisoft/iecp/crm/web/struts";
static {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
try {
configuration.setDirectoryForTemplateLoading(new File(templateFolder));
} catch (IOException e) {
e.printStackTrace();
}
} private WordUtils() {
throw new AssertionError();
} @SuppressWarnings("rawtypes")
public static void exportMillCertificateWord(HttpServletRequest request, HttpServletResponse response, Map map,String title,String ftlFile) throws IOException {
//Template freemarkerTemplate = configuration.getTemplate(ftlFile);
Template t = null;
try {
// temp.ftl为要装载的模板
t = configuration.getTemplate(ftlFile);
} catch (IOException e) {
e.printStackTrace();
}
// 获取桌面路径
File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
String desktopPath = desktopDir.getAbsolutePath();
desktopPath = desktopPath.replace("\\", "/");
// 输出文档路径及名称
java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
File outFile = new File(desktopPath + "/" + sdf.format(new Date()) + title);
Writer out = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outFile);
OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8");
out = new BufferedWriter(oWriter);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
t.process(map, out);
out.close();
fos.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter outW = response.getWriter();
outW.write("导出成功!</br>" + outFile.getPath());
outW.close();
}
}
导出后的文件为20180322135608简历.doc桌面文件: