天天看点

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

public void createDocContext(String file,List<Job> jobList,List<Educational> educationalList,List<Family> familyList,Resume resume) throws DocumentException,
			IOException {
			// 设置纸张大小
			Document document = new Document(PageSize.A4);
			// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
			RtfWriter2.getInstance(document, new FileOutputStream(file));
			document.open();
			// 设置中文字体
			BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			// 标题字体风格
			Font titleFont = new Font(bfChinese, 12, Font.BOLD);
			// 正文字体风格
			Font contextFont = new Font(bfChinese, 10, Font.BOLD);
			
			Paragraph title = new Paragraph("个人简历");
			// 设置标题格式对齐方式
			title.setAlignment(Element.ALIGN_CENTER);
			title.setFont(titleFont);	
			document.add(title);
			
			// 设置 Table 表格
			Table aTable = new Table(7);
			int width[] = { 10,15,15,15,15,15,15 };
			aTable.setWidths(width);// 设置每列所占比例
			aTable.setWidth(90); // 占页面宽度 90%
			aTable.setAlignment(Element.ALIGN_CENTER);// 居中显示
			aTable.setAutoFillEmptyCells(true); // 自动填满
			aTable.setBorderWidth(1); // 边框宽度
			aTable.setBorderColor(new Color(0, 125, 255)); // 边框颜色
			aTable.setPadding(0);// 衬距
			aTable.setSpacing(0);// 即单元格之间的间距
			aTable.setBorder(2);// 边框
			
			// 	设置表头
			Cell haderCell = new Cell(new Phrase("基本信息",contextFont));
			haderCell.setHeader(true);
			haderCell.setColspan(7);
			aTable.addCell(haderCell);
			aTable.endHeaders();
			
			Font fontChinese = new Font(bfChinese, 10, Font.NORMAL);
			
			aTable.addCell(new Phrase("姓名:", fontChinese));
			aTable.addCell(new Phrase(resume.getUsername(), fontChinese));
			aTable.addCell(new Phrase("性别:", fontChinese));
			aTable.addCell(new Phrase(resume.getGender(), fontChinese));
			aTable.addCell(new Phrase("出生日期:", fontChinese));
			aTable.addCell(new Phrase(resume.getDateofbirth() == null ? "":new SimpleDateFormat("yyyy-MM-hh").format(resume.getDateofbirth()), fontChinese));	          
			
			//添加图片
			String path = this.getClass().getResource("/").getPath().replace("%20", " ");
			String path1=path.substring(1, path.lastIndexOf("/"));;
			String path2=path1.substring(0,path1.lastIndexOf("/"));
			String path3=path2.substring(0,path2.lastIndexOf("/"));
			String url=path3+"/newImg/"+resume.getImagePath();
			Cell cell1_7  = null;
			if(new File(url).exists()){		
				Image img = Image.getInstance(url);
				img.setAbsolutePosition(0, 0);
				img.setAlignment(Image.MIDDLE);//设置图片显示位置
				img.scaleAbsolute(70,90);//直接设定显示尺寸
				cell1_7 = new Cell(img);
			}else{
				cell1_7 = new Cell(new Phrase("", fontChinese));
			}
			cell1_7.setRowspan(5);
			aTable.addCell(cell1_7);
			
			aTable.addCell(new Phrase("民族:", fontChinese));
			aTable.addCell(new Phrase(resume.getNation(), fontChinese));
			aTable.addCell(new Phrase("户籍地:", fontChinese));
			aTable.addCell(new Phrase(resume.getJgd(), fontChinese));
			aTable.addCell(new Phrase("籍贯:", fontChinese));
			aTable.addCell(new Phrase(resume.getJg(), fontChinese));	
			
			aTable.addCell(new Phrase("身高:", fontChinese));
			aTable.addCell(new Phrase(resume.getStature(), fontChinese));
			aTable.addCell(new Phrase("婚姻状况:", fontChinese));
			if("0".equals(resume.getWedlock())){
				aTable.addCell(new Phrase("未婚", fontChinese));
			}else if("1".equals(resume.getWedlock())){
				aTable.addCell(new Phrase("已婚", fontChinese));
			}else{
				aTable.addCell(new Phrase("", fontChinese));
			}
			aTable.addCell(new Phrase("政治面貌:", fontChinese));
			if("0".equals(resume.getPolitical())){
				aTable.addCell(new Phrase("中共党员", fontChinese));
			}else if("1".equals(resume.getPolitical())){
				aTable.addCell(new Phrase("团员", fontChinese));
			}else if("2".equals(resume.getPolitical())){
				aTable.addCell(new Phrase("群众", fontChinese));
			}else{
				aTable.addCell(new Phrase("", fontChinese));
			}	
			aTable.addCell(new Phrase("最高学历:", fontChinese));
			if("0".equals(resume.getEducational())){
				aTable.addCell(new Phrase("博士", fontChinese));
			}else if("1".equals(resume.getEducational())){
				aTable.addCell(new Phrase("研究生", fontChinese));
			}else if("2".equals(resume.getEducational())){
				aTable.addCell(new Phrase("本科", fontChinese));
			}else if("3".equals(resume.getEducational())){
				aTable.addCell(new Phrase("专科", fontChinese));
			}else{
				aTable.addCell(new Phrase("", fontChinese));
			}	
			aTable.addCell(new Phrase("外语水平:", fontChinese));
			aTable.addCell(new Phrase(resume.getLanguage(), fontChinese));
			aTable.addCell(new Phrase("计算机水平:", fontChinese));
			aTable.addCell(new Phrase(resume.getComputer(), fontChinese));	
			
			aTable.addCell(new Phrase("本人身份:", fontChinese));
			aTable.addCell(new Phrase(resume.getIdentification(), fontChinese));
			aTable.addCell(new Phrase("是否服从调剂:", fontChinese));
			if("0".equals(resume.getDistribution())){
				aTable.addCell(new Phrase("否", fontChinese));
			}else if("1".equals(resume.getDistribution())){
				aTable.addCell(new Phrase("是", fontChinese));
			}else{
				aTable.addCell(new Phrase("", fontChinese));
			}
			aTable.addCell(new Phrase("参加工作时间:", fontChinese));
			aTable.addCell(new Phrase(resume.getJobtime() ==null ? "":new SimpleDateFormat("yyyy-MM-hh").format(resume.getJobtime()), fontChinese));
			aTable.addCell(new Phrase("手机号:", fontChinese));
			aTable.addCell(new Phrase(resume.getMobile(), fontChinese));
			aTable.addCell(new Phrase("身份证号:", fontChinese));	
			Cell cell6_4 = new Cell(new Phrase(resume.getIdentificationnum(), fontChinese));
			cell6_4.setColspan(2);
			aTable.addCell(cell6_4);		
			aTable.addCell(new Phrase("电子邮箱:", fontChinese));
			aTable.addCell(new Phrase(resume.getEmail(), fontChinese));	
			
			aTable.addCell(new Phrase("通讯地址:", fontChinese));		
			Cell cell7_2 = new Cell(new Phrase(resume.getAddress(), fontChinese));
			cell7_2.setColspan(3);
			aTable.addCell(cell7_2);	
			aTable.addCell(new Phrase("邮编:", fontChinese));
			Cell cell7_6 = new Cell(new Phrase(resume.getCodePostal(), fontChinese));
			cell7_6.setColspan(2);
			aTable.addCell(cell7_6);	
			
			Cell cell8_1 = new Cell(new Phrase("本科及以上学习经历(最高学历倒序填写)", fontChinese));
			if(educationalList !=null && educationalList.size() >0){
				cell8_1.setRowspan(educationalList.size()+1);
			}
			aTable.addCell(cell8_1);
			aTable.addCell(new Phrase("起止时间", fontChinese));
			Cell cell8_3 = new Cell(new Phrase("学校", fontChinese));
			cell8_3.setColspan(2);
			aTable.addCell(cell8_3);		
			aTable.addCell(new Phrase("专业", fontChinese));
			aTable.addCell(new Phrase("所获学位", fontChinese));	
			aTable.addCell(new Phrase("是否全日制", fontChinese));
			
			if(educationalList !=null && educationalList.size() >0){
				for(int i=0;i<educationalList.size();i++){
					Educational educational = educationalList.get(i);
					aTable.addCell(new Phrase(educational.getSetime(), fontChinese));
					Cell cell9_3 = new Cell(new Phrase(educational.getSchoolname(), fontChinese));
					cell9_3.setColspan(2);
					aTable.addCell(cell9_3);		
					aTable.addCell(new Phrase(educational.getProfession(), fontChinese));
					aTable.addCell(new Phrase(educational.getAcademicdegree(), fontChinese));	
					aTable.addCell(new Phrase(educational.getFulltime(), fontChinese));
				}
			}
			
			Cell cell12_1 = new Cell(new Phrase("工作经验(不含实习)(近期经验倒序填写))", fontChinese));
			if(jobList !=null && jobList.size() >0){
				cell12_1.setRowspan(jobList.size()+1);
			}
			aTable.addCell(cell12_1);
			aTable.addCell(new Phrase("起止时间", fontChinese));
			Cell cell12_3 = new Cell(new Phrase("工作单位及内容", fontChinese));
			cell12_3.setColspan(2);
			aTable.addCell(cell12_3);		
			Cell cell12_5 = new Cell(new Phrase("部门", fontChinese));
			cell12_5.setColspan(2);
			aTable.addCell(cell12_5);	
			aTable.addCell(new Phrase("职务", fontChinese));	
			if(jobList !=null && jobList.size() >0){
				for(int j=0;j<jobList.size();j++){
					Job job = jobList.get(j);		
					aTable.addCell(new Phrase(job.getSntime(), fontChinese));
					Cell cell13_3 = new Cell(new Phrase(job.getJobname(), fontChinese));
					cell13_3.setColspan(2);
					aTable.addCell(cell13_3);		
					Cell cell13_5 = new Cell(new Phrase(job.getDepartment(), fontChinese));
					cell13_5.setColspan(2);
					aTable.addCell(cell13_5);	
					aTable.addCell(new Phrase(job.getPosition(), fontChinese));	
				}	
			}
			aTable.addCell(new Phrase("证书及职称", fontChinese));	
			Cell cell18_2 = new Cell(new Phrase(resume.getZhengshu(), fontChinese));
			cell18_2.setColspan(6);
			aTable.addCell(cell18_2);	
			
			aTable.addCell(new Phrase("专业经验及特长(限两百字)", fontChinese));	
			Cell cell19_2 = new Cell(new Phrase(resume.getZhuanyetechang(), fontChinese));
			cell19_2.setColspan(6);
			aTable.addCell(cell19_2);	
			
			aTable.addCell(new Phrase("主要学术研究成果", fontChinese));	
			Cell cell20_2 = new Cell(new Phrase(resume.getXueshu(), fontChinese));
			cell20_2.setColspan(6);
			aTable.addCell(cell20_2);	
			
			aTable.addCell(new Phrase("曾获奖惩情况", fontChinese));	
			Cell cell21_2 = new Cell(new Phrase(resume.getJiangcheng(), fontChinese));
			cell21_2.setColspan(6);
			aTable.addCell(cell21_2);
			
			Cell cell22_1 = new Cell(new Phrase("家庭主要成员", fontChinese));
			if(familyList !=null && familyList.size()>0){
				cell22_1.setRowspan(familyList.size()+1);
			}
			aTable.addCell(cell22_1);
			aTable.addCell(new Phrase("姓名", fontChinese));
			aTable.addCell(new Phrase("关系", fontChinese));
			Cell cell22_4 = new Cell(new Phrase("单位", fontChinese));
			cell22_4.setColspan(2);
			aTable.addCell(cell22_4);
			aTable.addCell(new Phrase("职务", fontChinese));	
			aTable.addCell(new Phrase("联系方式", fontChinese));	
			
			if(familyList !=null && familyList.size()>0){
				for(int k=0;k<familyList.size();k++){
					Family family = familyList.get(k);
					aTable.addCell(new Phrase(family.getFamilyname(), fontChinese));
					aTable.addCell(new Phrase(family.getRelation(), fontChinese));
					Cell cell23_4 = new Cell(new Phrase(family.getWorkunit(), fontChinese));
					cell23_4.setColspan(2);
					aTable.addCell(cell23_4);
					aTable.addCell(new Phrase(family.getPosition(), fontChinese));	
					aTable.addCell(new Phrase(family.getPhone(), fontChinese));
				}	
			}
			aTable.addCell(new Phrase("其他事项说明", fontChinese));	
			Cell cell27_2 = new Cell(new Phrase(resume.getQita(), fontChinese));
			cell27_2.setColspan(6);
			aTable.addCell(cell27_2);
			 
			document.add(aTable);
			document.add(new Paragraph("\n"));
			document.close();
	}
           
public String daochuAll(){
		String sessionId=(String)ServletActionContext.getRequest().getSession().getId();
		String tmpDir = "D:/"+sessionId+"_zip/";
		File temp = new File(tmpDir);
		if(!temp.exists()){
				temp.mkdirs();
		}
		if(resumeIds !=null){
			String[] resumeIdList =resumeIds.split(",");	
			if(resumeIdList !=null && resumeIdList.length>0){
				//去掉重复简历
				List<String> idsList = new ArrayList<String>();  
		        for (int i=0; i<resumeIdList.length; i++) { 
		        	 if(!idsList.contains(resumeIdList[i])) {  
		        		 idsList.add(resumeIdList[i]);  
		        	 }	
		        } 
		        if(idsList !=null && idsList.size()>0){
		        	File zipFile = new File(tmpDir +"简历集合.zip") ;   // 定义压缩文件名称
		        	ZipOutputStream zipOut = null ; // 声明压缩流对象   
		        	try {
							zipOut = new ZipOutputStream(new FileOutputStream(zipFile)) ;
							zipOut.setEncoding("GBK");
							for(int i=0;i<idsList.size();i++ ){		
								setResumeDoc(idsList.get(i));
								String fileName =null;
								if(resume.getResumename() ==null  ||  "".equals(resume.getResumename())){
									fileName = UUIDGenerator.generate()+".doc";
								}else{								 
									 fileName = resume.getResumename()+".doc";
								}
								fileName = tmpDir+fileName;
								WordTools b = new WordTools();
								try {
										b.createDocContext(fileName,jobList,educationalList,familyList,resume);
										removeDoc();
										File file = new File(fileName) ;  // 定义要压缩的文件
										InputStream input = new FileInputStream(file) ; // 定义文件的输入流   
								        zipOut.putNextEntry(new ZipEntry(file.getName())) ; // 设置ZipEntry对象
								        int temp1 = 0 ;  
								        while((temp1=input.read())!=-1){ // 读取内容   
								            zipOut.write(temp1) ;    // 压缩输出   
								        } 	
								        input.close() ; // 关闭输入流    
								} catch (Exception e) {
										e.printStackTrace();
								} 
							}
				        	zipOut.close() ;    // 关闭输出流 
					} catch (Exception e1) {
							e1.printStackTrace();
					}
		        	HttpServletResponse response =ServletActionContext.getResponse();
					try {
						Utils.fileDownLoad(response,tmpDir+zipFile.getName());
					} catch (ServletException e) {
						e.printStackTrace();
					}
		        	Utils.DeleteFolder(tmpDir);
		        }	
			}	
		}
		return null;
	}
           

需要jar包:itext-2.1.5.jar、itext-rtf-2.1.7.jar、iTextAsian.jar、apache-ant-zip.jar