Java exports word and compresses it, including tables

  1. public void createDocContext(String file,List<Job> jobList,List<Educational> educationalList,List<Family> familyList,Resume resume) throws DocumentException,  
  2.             IOException {  
  3.             //Set paper size  
  4.             Document document = new Document(PageSize.A4);  
  5.             // Establish a writer (Writer) associated with the document object. The document can be written to the disk through the writer (Writer)  
  6.             RtfWriter2.getInstance(document, new FileOutputStream(file));  
  7.             document.open();  
  8.             //Set Chinese font  
  9.             BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);  
  10.             //Title font style  
  11.             Font titleFont = new Font(bfChinese, 12, Font.BOLD);  
  12.             // Text font style  
  13.             Font contextFont = new Font(bfChinese, 10, Font.BOLD);  
  14.               
  15.             Paragraph title =  new  Paragraph( "Personal Resume" );  
  16.             //Set title format alignment  
  17.             title.setAlignment(Element.ALIGN_CENTER);  
  18.             title.setFont(titleFont);     
  19.             document.add(title);  
  20.               
  21.             //Set the Table table  
  22.             Table aTable = new Table(7);  
  23.             int width[] = { 10,15,15,15,15,15,15 };  
  24.             aTable.setWidths(width); // Set the proportion of each column  
  25.             aTable.setWidth( 90 );  // 90% of the page width  
  26.             aTable.setAlignment(Element.ALIGN_CENTER); // Centered display  
  27.             aTable.setAutoFillEmptyCells( true );  // Automatically fill  
  28.             aTable.setBorderWidth( 1 );  // border width  
  29.             aTable.setBorderColor( new  Color( 0 125 255 ));  // Border color  
  30.             aTable.setPadding( 0 ); // padding  
  31.             aTable.setSpacing( 0 ); // That is the spacing between cells  
  32.             aTable.setBorder( 2 ); // border  
  33.               
  34.             //Set header  
  35.             Cell haderCell =  new  Cell( new  Phrase( "Basic information" ,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( "First and last name:" , 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.     }  


Guess you like

Origin blog.csdn.net/linwei_hello/article/details/17137673