最近做一个关于文档导出功能, 顺便学习了下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 的格式。而后上后台代码:
1 final static String separator = File.separator; // 系统路径分割符 2 3 public void exportSellPlan(HttpServletRequest request, HttpServletResponse response){ 4 String evaluation = "四年的大学生涯是我人生的一大转折点。" 5 + "我深深地懂得“责任,荣誉,国家”这六个字的含义。作为大学生我最基本的责任是学习。" 6 + "在大学期间我认真学习,发挥自己的特长,挖掘自身的潜力,从而提高了" 7 + "自身的学习能力和分析处理问题的能力,也获得了大家的认同。"; 8 //获得数据 9 Map<String, Object> map = new HashMap<String, Object>(); 10 map.put("profession", "xxx"); 11 map.put("name", "xxx"); 12 map.put("address","山西省xx县"); 13 map.put("email", "xxx@163.com"); 14 map.put("tel", "173xxx9927"); 15 map.put("skill1", "面对不同客人,用不同的语气、不同的态度,与客户要谈得来,处处为客户着想"); 16 map.put("skill2", "面对不同客人,用不同的语气、不同的态度,与客户要谈得来,处处为客户着想"); 17 map.put("skill3", "面对不同客人,用不同的语气、不同的态度,与客户要谈得来,处处为客户着想"); 18 map.put("skill4", "面对不同客人,用不同的语气、不同的态度,与客户要谈得来,处处为客户着想"); 19 map.put("hobby", "运动,看书,上网聊天,看电影。"); 20 map.put("evaluation", evaluation); 21 map.put("graduateInstitutions", "南昌航空大学"); 22 map.put("degree", "本科"); 23 //map.put("image", getImageBase("E:"+ separator +"photo.jpg")); 24 try { 25 WordUtils.exportMillCertificateWord(request,response,map,"简历","jianli.ftl"); 26 } catch (IOException e) { 27 e.printStackTrace(); 28 } 29 } 30 31 //获得图片的base64码 32 public static String getImageBase(String src) { 33 if(src==null||src==""){ 34 return ""; 35 } 36 File file = new File(src); 37 //File file = new File(getRequest().getRealPath("/")+src.replace(getRequest().getContextPath(), "")); 38 if(!file.exists()) { 39 return ""; 40 } 41 InputStream in = null; 42 byte[] data = null; 43 try { 44 in = new FileInputStream(file); 45 } catch (FileNotFoundException e1) { 46 e1.printStackTrace(); 47 } 48 try { 49 data = new byte[in.available()]; 50 in.read(data); 51 in.close(); 52 } catch (IOException e) { 53 e.printStackTrace(); 54 } 55 BASE64Encoder encoder = new BASE64Encoder(); 56 return encoder.encode(data); 57 }
关于excel导出:
1 package com.haiyisoft.iecp.crm.web.struts; 2 3 import java.io.BufferedWriter; 4 import java.io.File; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.io.OutputStreamWriter; 9 import java.io.PrintWriter; 10 import java.io.UnsupportedEncodingException; 11 import java.io.Writer; 12 import java.text.SimpleDateFormat; 13 import java.util.Date; 14 import java.util.Map; 15 16 import javax.servlet.http.HttpServletRequest; 17 import javax.servlet.http.HttpServletResponse; 18 import javax.swing.filechooser.FileSystemView; 19 20 import freemarker.template.Configuration; 21 import freemarker.template.Template; 22 import freemarker.template.TemplateException; 23 24 public class WordUtils extends BaseAction{ 25 //配置信息,代码本身写的还是很可读的,就不过多注解了 26 private static Configuration configuration = null; 27 //这里注意的是利用WordUtils的类加载器动态获得模板文件的位置 28 // private static final String templateFolder = WordUtils.class.getClassLoader().getResource("../../").getPath() + "WEB-INF/templetes/"; 29 private static final String templateFolder = "D:/uepstudio_uep/workspace/iecp/src/com/haiyisoft/iecp/crm/web/struts"; 30 static { 31 configuration = new Configuration(); 32 configuration.setDefaultEncoding("utf-8"); 33 try { 34 configuration.setDirectoryForTemplateLoading(new File(templateFolder)); 35 } catch (IOException e) { 36 e.printStackTrace(); 37 } 38 } 39 40 private WordUtils() { 41 throw new AssertionError(); 42 } 43 44 @SuppressWarnings("rawtypes") 45 public static void exportMillCertificateWord(HttpServletRequest request, HttpServletResponse response, Map map,String title,String ftlFile) throws IOException { 46 //Template freemarkerTemplate = configuration.getTemplate(ftlFile); 47 Template t = null; 48 try { 49 // temp.ftl为要装载的模板 50 t = configuration.getTemplate(ftlFile); 51 } catch (IOException e) { 52 e.printStackTrace(); 53 } 54 // 获取桌面路径 55 File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory(); 56 String desktopPath = desktopDir.getAbsolutePath(); 57 desktopPath = desktopPath.replace("\", "/"); 58 // 输出文档路径及名称 59 java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); 60 File outFile = new File(desktopPath + "/" + sdf.format(new Date()) + title); 61 Writer out = null; 62 FileOutputStream fos = null; 63 try { 64 fos = new FileOutputStream(outFile); 65 OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8"); 66 out = new BufferedWriter(oWriter); 67 } catch (FileNotFoundException e1) { 68 e1.printStackTrace(); 69 } catch (UnsupportedEncodingException e) { 70 e.printStackTrace(); 71 } 72 try { 73 t.process(map, out); 74 out.close(); 75 fos.close(); 76 } catch (TemplateException e) { 77 e.printStackTrace(); 78 } catch (IOException e) { 79 e.printStackTrace(); 80 } 81 response.setContentType("text/html;charset=UTF-8"); 82 response.setCharacterEncoding("UTF-8"); 83 PrintWriter outW = response.getWriter(); 84 outW.write("导出成功!</br>" + outFile.getPath()); 85 outW.close(); 86 } 87 }
导出后的文件为20180322135608简历.doc桌面文件: