java导出word并压缩,含有表

原创 2013年12月05日 10:43:20
  1. public void createDocContext(String file,List<Job> jobList,List<Educational> educationalList,List<Family> familyList,Resume resume) throws DocumentException,  
  2.             IOException {  
  3.             // 设置纸张大小  
  4.             Document document = new Document(PageSize.A4);  
  5.             // 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中  
  6.             RtfWriter2.getInstance(document, new FileOutputStream(file));  
  7.             document.open();  
  8.             // 设置中文字体  
  9.             BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);  
  10.             // 标题字体风格  
  11.             Font titleFont = new Font(bfChinese, 12, Font.BOLD);  
  12.             // 正文字体风格  
  13.             Font contextFont = new Font(bfChinese, 10, Font.BOLD);  
  14.               
  15.             Paragraph title = new Paragraph("个人简历");  
  16.             // 设置标题格式对齐方式  
  17.             title.setAlignment(Element.ALIGN_CENTER);  
  18.             title.setFont(titleFont);     
  19.             document.add(title);  
  20.               
  21.             // 设置 Table 表格  
  22.             Table aTable = new Table(7);  
  23.             int width[] = { 10,15,15,15,15,15,15 };  
  24.             aTable.setWidths(width);// 设置每列所占比例  
  25.             aTable.setWidth(90); // 占页面宽度 90%  
  26.             aTable.setAlignment(Element.ALIGN_CENTER);// 居中显示  
  27.             aTable.setAutoFillEmptyCells(true); // 自动填满  
  28.             aTable.setBorderWidth(1); // 边框宽度  
  29.             aTable.setBorderColor(new Color(0125255)); // 边框颜色  
  30.             aTable.setPadding(0);// 衬距  
  31.             aTable.setSpacing(0);// 即单元格之间的间距  
  32.             aTable.setBorder(2);// 边框  
  33.               
  34.             //  设置表头  
  35.             Cell haderCell = new Cell(new Phrase("基本信息",contextFont));  
  36.             haderCell.setHeader(true);  
  37.             haderCell.setColspan(7);  
  38.             aTable.addCell(haderCell);  
  39.             aTable.endHeaders();  
  40.               
  41.             Font fontChinese = new Font(bfChinese, 10, Font.NORMAL);  
  42.               
  43.             aTable.addCell(new Phrase("姓名:", fontChinese));  
  44.             aTable.addCell(new Phrase(resume.getUsername(), fontChinese));  
  45.             aTable.addCell(new Phrase("性别:", fontChinese));  
  46.             aTable.addCell(new Phrase(resume.getGender(), fontChinese));  
  47.             aTable.addCell(new Phrase("出生日期:", fontChinese));  
  48.             aTable.addCell(new Phrase(resume.getDateofbirth() == null ? "":new SimpleDateFormat("yyyy-MM-hh").format(resume.getDateofbirth()), fontChinese));               
  49.               
  50.             //添加图片  
  51.             String path = this.getClass().getResource("/").getPath().replace("%20"" ");  
  52.             String path1=path.substring(1, path.lastIndexOf("/"));;  
  53.             String path2=path1.substring(0,path1.lastIndexOf("/"));  
  54.             String path3=path2.substring(0,path2.lastIndexOf("/"));  
  55.             String url=path3+"/newImg/"+resume.getImagePath();  
  56.             Cell cell1_7  = null;  
  57.             if(new File(url).exists()){       
  58.                 Image img = Image.getInstance(url);  
  59.                 img.setAbsolutePosition(00);  
  60.                 img.setAlignment(Image.MIDDLE);//设置图片显示位置  
  61.                 img.scaleAbsolute(70,90);//直接设定显示尺寸  
  62.                 cell1_7 = new Cell(img);  
  63.             }else{  
  64.                 cell1_7 = new Cell(new Phrase("", fontChinese));  
  65.             }  
  66.             cell1_7.setRowspan(5);  
  67.             aTable.addCell(cell1_7);  
  68.               
  69.             aTable.addCell(new Phrase("民族:", fontChinese));  
  70.             aTable.addCell(new Phrase(resume.getNation(), fontChinese));  
  71.             aTable.addCell(new Phrase("户籍地:", fontChinese));  
  72.             aTable.addCell(new Phrase(resume.getJgd(), fontChinese));  
  73.             aTable.addCell(new Phrase("籍贯:", fontChinese));  
  74.             aTable.addCell(new Phrase(resume.getJg(), fontChinese));      
  75.               
  76.             aTable.addCell(new Phrase("身高:", fontChinese));  
  77.             aTable.addCell(new Phrase(resume.getStature(), fontChinese));  
  78.             aTable.addCell(new Phrase("婚姻状况:", fontChinese));  
  79.             if("0".equals(resume.getWedlock())){  
  80.                 aTable.addCell(new Phrase("未婚", fontChinese));  
  81.             }else if("1".equals(resume.getWedlock())){  
  82.                 aTable.addCell(new Phrase("已婚", fontChinese));  
  83.             }else{  
  84.                 aTable.addCell(new Phrase("", fontChinese));  
  85.             }  
  86.             aTable.addCell(new Phrase("政治面貌:", fontChinese));  
  87.             if("0".equals(resume.getPolitical())){  
  88.                 aTable.addCell(new Phrase("中共党员", fontChinese));  
  89.             }else if("1".equals(resume.getPolitical())){  
  90.                 aTable.addCell(new Phrase("团员", fontChinese));  
  91.             }else if("2".equals(resume.getPolitical())){  
  92.                 aTable.addCell(new Phrase("群众", fontChinese));  
  93.             }else{  
  94.                 aTable.addCell(new Phrase("", fontChinese));  
  95.             }     
  96.             aTable.addCell(new Phrase("最高学历:", fontChinese));  
  97.             if("0".equals(resume.getEducational())){  
  98.                 aTable.addCell(new Phrase("博士", fontChinese));  
  99.             }else if("1".equals(resume.getEducational())){  
  100.                 aTable.addCell(new Phrase("研究生", fontChinese));  
  101.             }else if("2".equals(resume.getEducational())){  
  102.                 aTable.addCell(new Phrase("本科", fontChinese));  
  103.             }else if("3".equals(resume.getEducational())){  
  104.                 aTable.addCell(new Phrase("专科", fontChinese));  
  105.             }else{  
  106.                 aTable.addCell(new Phrase("", fontChinese));  
  107.             }     
  108.             aTable.addCell(new Phrase("外语水平:", fontChinese));  
  109.             aTable.addCell(new Phrase(resume.getLanguage(), fontChinese));  
  110.             aTable.addCell(new Phrase("计算机水平:", fontChinese));  
  111.             aTable.addCell(new Phrase(resume.getComputer(), fontChinese));    
  112.               
  113.             aTable.addCell(new Phrase("本人身份:", fontChinese));  
  114.             aTable.addCell(new Phrase(resume.getIdentification(), fontChinese));  
  115.             aTable.addCell(new Phrase("是否服从调剂:", fontChinese));  
  116.             if("0".equals(resume.getDistribution())){  
  117.                 aTable.addCell(new Phrase("否", fontChinese));  
  118.             }else if("1".equals(resume.getDistribution())){  
  119.                 aTable.addCell(new Phrase("是", fontChinese));  
  120.             }else{  
  121.                 aTable.addCell(new Phrase("", fontChinese));  
  122.             }  
  123.             aTable.addCell(new Phrase("参加工作时间:", fontChinese));  
  124.             aTable.addCell(new Phrase(resume.getJobtime() ==null ? "":new SimpleDateFormat("yyyy-MM-hh").format(resume.getJobtime()), fontChinese));  
  125.             aTable.addCell(new Phrase("手机号:", fontChinese));  
  126.             aTable.addCell(new Phrase(resume.getMobile(), fontChinese));  
  127.             aTable.addCell(new Phrase("身份证号:", fontChinese));     
  128.             Cell cell6_4 = new Cell(new Phrase(resume.getIdentificationnum(), fontChinese));  
  129.             cell6_4.setColspan(2);  
  130.             aTable.addCell(cell6_4);          
  131.             aTable.addCell(new Phrase("电子邮箱:", fontChinese));  
  132.             aTable.addCell(new Phrase(resume.getEmail(), fontChinese));   
  133.               
  134.             aTable.addCell(new Phrase("通讯地址:", fontChinese));         
  135.             Cell cell7_2 = new Cell(new Phrase(resume.getAddress(), fontChinese));  
  136.             cell7_2.setColspan(3);  
  137.             aTable.addCell(cell7_2);      
  138.             aTable.addCell(new Phrase("邮编:", fontChinese));  
  139.             Cell cell7_6 = new Cell(new Phrase(resume.getCodePostal(), fontChinese));  
  140.             cell7_6.setColspan(2);  
  141.             aTable.addCell(cell7_6);      
  142.               
  143.             Cell cell8_1 = new Cell(new Phrase("本科及以上学习经历(最高学历倒序填写)", fontChinese));  
  144.             if(educationalList !=null && educationalList.size() >0){  
  145.                 cell8_1.setRowspan(educationalList.size()+1);  
  146.             }  
  147.             aTable.addCell(cell8_1);  
  148.             aTable.addCell(new Phrase("起止时间", fontChinese));  
  149.             Cell cell8_3 = new Cell(new Phrase("学校", fontChinese));  
  150.             cell8_3.setColspan(2);  
  151.             aTable.addCell(cell8_3);          
  152.             aTable.addCell(new Phrase("专业", fontChinese));  
  153.             aTable.addCell(new Phrase("所获学位", fontChinese));      
  154.             aTable.addCell(new Phrase("是否全日制", fontChinese));  
  155.               
  156.             if(educationalList !=null && educationalList.size() >0){  
  157.                 for(int i=0;i<educationalList.size();i++){  
  158.                     Educational educational = educationalList.get(i);  
  159.                     aTable.addCell(new Phrase(educational.getSetime(), fontChinese));  
  160.                     Cell cell9_3 = new Cell(new Phrase(educational.getSchoolname(), fontChinese));  
  161.                     cell9_3.setColspan(2);  
  162.                     aTable.addCell(cell9_3);          
  163.                     aTable.addCell(new Phrase(educational.getProfession(), fontChinese));  
  164.                     aTable.addCell(new Phrase(educational.getAcademicdegree(), fontChinese));     
  165.                     aTable.addCell(new Phrase(educational.getFulltime(), fontChinese));  
  166.                 }  
  167.             }  
  168.               
  169.             Cell cell12_1 = new Cell(new Phrase("工作经验(不含实习)(近期经验倒序填写))", fontChinese));  
  170.             if(jobList !=null && jobList.size() >0){  
  171.                 cell12_1.setRowspan(jobList.size()+1);  
  172.             }  
  173.             aTable.addCell(cell12_1);  
  174.             aTable.addCell(new Phrase("起止时间", fontChinese));  
  175.             Cell cell12_3 = new Cell(new Phrase("工作单位及内容", fontChinese));  
  176.             cell12_3.setColspan(2);  
  177.             aTable.addCell(cell12_3);         
  178.             Cell cell12_5 = new Cell(new Phrase("部门", fontChinese));  
  179.             cell12_5.setColspan(2);  
  180.             aTable.addCell(cell12_5);     
  181.             aTable.addCell(new Phrase("职务", fontChinese));    
  182.             if(jobList !=null && jobList.size() >0){  
  183.                 for(int j=0;j<jobList.size();j++){  
  184.                     Job job = jobList.get(j);         
  185.                     aTable.addCell(new Phrase(job.getSntime(), fontChinese));  
  186.                     Cell cell13_3 = new Cell(new Phrase(job.getJobname(), fontChinese));  
  187.                     cell13_3.setColspan(2);  
  188.                     aTable.addCell(cell13_3);         
  189.                     Cell cell13_5 = new Cell(new Phrase(job.getDepartment(), fontChinese));  
  190.                     cell13_5.setColspan(2);  
  191.                     aTable.addCell(cell13_5);     
  192.                     aTable.addCell(new Phrase(job.getPosition(), fontChinese));   
  193.                 }     
  194.             }  
  195.             aTable.addCell(new Phrase("证书及职称", fontChinese));     
  196.             Cell cell18_2 = new Cell(new Phrase(resume.getZhengshu(), fontChinese));  
  197.             cell18_2.setColspan(6);  
  198.             aTable.addCell(cell18_2);     
  199.               
  200.             aTable.addCell(new Phrase("专业经验及特长(限两百字)", fontChinese));     
  201.             Cell cell19_2 = new Cell(new Phrase(resume.getZhuanyetechang(), fontChinese));  
  202.             cell19_2.setColspan(6);  
  203.             aTable.addCell(cell19_2);     
  204.               
  205.             aTable.addCell(new Phrase("主要学术研究成果", fontChinese));      
  206.             Cell cell20_2 = new Cell(new Phrase(resume.getXueshu(), fontChinese));  
  207.             cell20_2.setColspan(6);  
  208.             aTable.addCell(cell20_2);     
  209.               
  210.             aTable.addCell(new Phrase("曾获奖惩情况", fontChinese));    
  211.             Cell cell21_2 = new Cell(new Phrase(resume.getJiangcheng(), fontChinese));  
  212.             cell21_2.setColspan(6);  
  213.             aTable.addCell(cell21_2);  
  214.               
  215.             Cell cell22_1 = new Cell(new Phrase("家庭主要成员", fontChinese));  
  216.             if(familyList !=null && familyList.size()>0){  
  217.                 cell22_1.setRowspan(familyList.size()+1);  
  218.             }  
  219.             aTable.addCell(cell22_1);  
  220.             aTable.addCell(new Phrase("姓名", fontChinese));  
  221.             aTable.addCell(new Phrase("关系", fontChinese));  
  222.             Cell cell22_4 = new Cell(new Phrase("单位", fontChinese));  
  223.             cell22_4.setColspan(2);  
  224.             aTable.addCell(cell22_4);  
  225.             aTable.addCell(new Phrase("职务", fontChinese));    
  226.             aTable.addCell(new Phrase("联系方式", fontChinese));      
  227.               
  228.             if(familyList !=null && familyList.size()>0){  
  229.                 for(int k=0;k<familyList.size();k++){  
  230.                     Family family = familyList.get(k);  
  231.                     aTable.addCell(new Phrase(family.getFamilyname(), fontChinese));  
  232.                     aTable.addCell(new Phrase(family.getRelation(), fontChinese));  
  233.                     Cell cell23_4 = new Cell(new Phrase(family.getWorkunit(), fontChinese));  
  234.                     cell23_4.setColspan(2);  
  235.                     aTable.addCell(cell23_4);  
  236.                     aTable.addCell(new Phrase(family.getPosition(), fontChinese));    
  237.                     aTable.addCell(new Phrase(family.getPhone(), fontChinese));  
  238.                 }     
  239.             }  
  240.             aTable.addCell(new Phrase("其他事项说明", fontChinese));    
  241.             Cell cell27_2 = new Cell(new Phrase(resume.getQita(), fontChinese));  
  242.             cell27_2.setColspan(6);  
  243.             aTable.addCell(cell27_2);  
  244.                
  245.             document.add(aTable);  
  246.             document.add(new Paragraph("\n"));  
  247.             document.close();  
  248.     }  
         需要jar包:itext-2.1.5.jar、itext-rtf-2.1.7.jar、iTextAsian.jar、apache-ant-zip.jar

  1. public String daochuAll(){  
  2.         String sessionId=(String)ServletActionContext.getRequest().getSession().getId();  
  3.         String tmpDir = "D:/"+sessionId+"_zip/";  
  4.         File temp = new File(tmpDir);  
  5.         if(!temp.exists()){  
  6.                 temp.mkdirs();  
  7.         }  
  8.         if(resumeIds !=null){  
  9.             String[] resumeIdList =resumeIds.split(",");      
  10.             if(resumeIdList !=null && resumeIdList.length>0){  
  11.                 //去掉重复简历  
  12.                 List<String> idsList = new ArrayList<String>();    
  13.                 for (int i=0; i<resumeIdList.length; i++) {   
  14.                      if(!idsList.contains(resumeIdList[i])) {    
  15.                          idsList.add(resumeIdList[i]);    
  16.                      }    
  17.                 }   
  18.                 if(idsList !=null && idsList.size()>0){  
  19.                     File zipFile = new File(tmpDir +"简历集合.zip") ;   // 定义压缩文件名称  
  20.                     ZipOutputStream zipOut = null ; // 声明压缩流对象     
  21.                     try {  
  22.                             zipOut = new ZipOutputStream(new FileOutputStream(zipFile)) ;  
  23.                             zipOut.setEncoding("GBK");  
  24.                             for(int i=0;i<idsList.size();i++ ){        
  25.                                 setResumeDoc(idsList.get(i));  
  26.                                 String fileName =null;  
  27.                                 if(resume.getResumename() ==null  ||  "".equals(resume.getResumename())){  
  28.                                     fileName = UUIDGenerator.generate()+".doc";  
  29.                                 }else{                                 
  30.                                      fileName = resume.getResumename()+".doc";  
  31.                                 }  
  32.                                 fileName = tmpDir+fileName;  
  33.                                 WordTools b = new WordTools();  
  34.                                 try {  
  35.                                         b.createDocContext(fileName,jobList,educationalList,familyList,resume);  
  36.                                         removeDoc();  
  37.                                         File file = new File(fileName) ;  // 定义要压缩的文件  
  38.                                         InputStream input = new FileInputStream(file) ; // 定义文件的输入流     
  39.                                         zipOut.putNextEntry(new ZipEntry(file.getName())) ; // 设置ZipEntry对象  
  40.                                         int temp1 = 0 ;    
  41.                                         while((temp1=input.read())!=-1){ // 读取内容     
  42.                                             zipOut.write(temp1) ;    // 压缩输出     
  43.                                         }     
  44.                                         input.close() ; // 关闭输入流      
  45.                                 } catch (Exception e) {  
  46.                                         e.printStackTrace();  
  47.                                 }   
  48.                             }  
  49.                             zipOut.close() ;    // 关闭输出流   
  50.                     } catch (Exception e1) {  
  51.                             e1.printStackTrace();  
  52.                     }  
  53.                     HttpServletResponse response =ServletActionContext.getResponse();  
  54.                     try {  
  55.                         Utils.fileDownLoad(response,tmpDir+zipFile.getName());  
  56.                     } catch (ServletException e) {  
  57.                         e.printStackTrace();  
  58.                     }  
  59.                     Utils.DeleteFolder(tmpDir);  
  60.                 }     
  61.             }     
  62.         }  
  63.         return null;  
  64.     }  


相关文章推荐

JAVA根据ftl模板生成需要的多表格word文档

JAVA根据ftl模板生成需要的多表格word文档
  • JavisZ
  • JavisZ
  • 2014年12月08日 22:18
  • 1896

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Java中根据模板导出数据到word的解决方案

Java中根据模板导出数据到word的解决方案我们需求如下:给你一个模板,里面有一个表格,标题已经给好,程序主要就是完成把数据填写到word中并提供给用户下载。   网上找了很久,发现主要两种开源包...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

目前java导出word的6种解决方案

文章转载自:http://www.cnblogs.com/lcngu/p/5247179.html 最近做的项目,需要将一些信息导出到word中。在网上找了好多解决方案,现在将这几天的总结分享一下。 ...

Java初学者 JDK命令行 路径问题

Java初学者 JDK命令行 路径问题 屏幕出现: Exception in thread "main" java.lang.NoClassDefFoundError: C:\JavaTest\H...

用Notepad++中的NppExec插件编译运行Java小程序

一、NppExec插件的安装 二、javac以及java命令的配置
  • zgz0627
  • zgz0627
  • 2015年11月07日 10:48
  • 1754

java导出word并压缩,含有表格,表格中插入图片

public void createDocContext(String file,List jobList,List educationalList,List familyList,Resume re...

JAVA利用FreeMarker生成(导出)Word文档

一、首先下载相对应的jar包:freemarker.jar; 而我的项目环境是:maven 在pom.xml中: freemarker freemarker 2.3.8 二、自定义...

Java 使用Poi相关jar包导出word文件

poi其实是很强大的jar包,可以导出excel/word,但网上大多数都是导出excel的例子,很少有导出word的例子.由于各个版本的差异,也会导致一些api的不同,导出不了word,所以整理了一...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:java导出word并压缩,含有表
举报原因:
原因补充:

(最多只允许输入30个字)