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 Zip压缩输入输出流问题

java Zip压缩输入输出流问题  分享| 2012-11-27 22:03a672489861 | 浏览 2752 次  java package com.zjw; impo...
  • evilcry2012
  • evilcry2012
  • 2016年04月18日 10:51
  • 692

java代码操作zip文件--写入zip文件

public class TestZipWriter { @SuppressWarnings("resource") public static void writeZipFileTe...
  • u014373031
  • u014373031
  • 2016年08月24日 21:06
  • 1738

POI, 模板读写和修改, 单元格内换行, 打包成zip

 同上一篇TableTree4J的, 这是我实习一开始就遇到的一个问题. 据说还是已经离职的那位仁兄研究了好久都没有解决的疑难问题呢... 之前没有接触过POI, 研究了一个周末POI的AP...
  • cinneptio
  • cinneptio
  • 2014年09月29日 22:24
  • 2186

poi导出word和excel,附带jar包

  • 2014年05月16日 11:40
  • 7.28MB
  • 下载

java导出多个excel表格,并压缩成zip输出

/** * 导出支付宝批量支付文件excel * * @param name * @param begintime * @param endtime * @param p ...
  • qq_14861089
  • qq_14861089
  • 2016年11月15日 11:14
  • 4786

java导出多个excel压缩zip下载

package com.dianping.emidas.activity.biz.utils; import java.io.BufferedOutputStream; import java.io...
  • hongwangzhang
  • hongwangzhang
  • 2016年03月01日 10:29
  • 3579

导出下载多个Excel文件并打包成zip文件

导出下载单个Excel也许简单,但是有的时候也会用到下载多个excel并且打包成zip文件的情况,最近遇到了,现在简单整理一下! 1、ftl文件 导出 ...
  • javaGirlOne
  • javaGirlOne
  • 2016年06月15日 11:32
  • 2435

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

public void createDocContext(String file,List jobList,List educationalList,List familyList,Resume re...
  • fm19901229
  • fm19901229
  • 2013年05月03日 18:07
  • 1172

java 通过FreeMarker导出数据库结构word文档

一般所有的项目都需要数据库设计文档,对于表不多的可以编辑word文档,自己写,但对于表很多,字段很多的数据库,这时候写设计文档就很麻烦,楼主就是遇到这个问题,项目70张表,一个个写到文档里是在麻烦,所...
  • Coder_Qiang
  • Coder_Qiang
  • 2017年06月28日 17:42
  • 195

使用Aspose.Words for Java完成复杂Word与PDF的导出

使用Aspose.Words for Java 可以导出复杂WORD PDF HTML 多种数据格式  官方下载地址:http://www.aspose.com/java/word-componen...
  • dhdhxgx
  • dhdhxgx
  • 2015年05月03日 23:11
  • 2529
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:java导出word并压缩,含有表
举报原因:
原因补充:

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