java word 导出(FreeMarker,HTML模板,包含图片)

来源:互联网 发布:真实的蒋介石知乎 编辑:程序博客网 时间:2024/05/16 02:22
public void exportTEmployeeInfo() {this.temployee = this.personnelService.getTEmployee(eid);try {HashMap<String, Object> dataMap = new HashMap<String, Object>();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");if (temployee.getEname() != null) {dataMap.put("ename", temployee.getEname());} else {dataMap.put("ename", "");}if (temployee.getEenName() != null) {dataMap.put("eenname", temployee.getEenName());} else {dataMap.put("eenname", "");}if (temployee.getEuseName() != null) {dataMap.put("eusename", temployee.getEuseName());} else {dataMap.put("eusename", "");}if (temployee.getEcode() != null) {dataMap.put("ecode", temployee.getEcode());} else {dataMap.put("ecode", "");}if(temployee.getEbirthday()!=null && !"".equals(temployee.getEbirthday())){dataMap.put("ebirthday", sdf.format(temployee.getEbirthday()));} else {dataMap.put("ebirthday", "");}if (temployee.getEnation()!=null) {nationList = this.personnelService.querySysCodeList("民族");for (int i = 0; i < nationList.size(); i++) {if (nationList.get(i).get("id").toString().equals(temployee.getEnation().toString())) {dataMap.put("enation", nationList.get(i).get("name"));} else {dataMap.put("enation", "");}}} else {dataMap.put("enation", "");}if (temployee.getEnative() != null) {dataMap.put("enative", temployee.getEnative());} else {dataMap.put("enative", "");}if (temployee.getEcertificateType()!=null) {certificateTypeList = this.personnelService.querySysCodeList("证件类型");for (int i = 0; i < certificateTypeList.size(); i++) {if (certificateTypeList.get(i).get("id").toString().equals(temployee.getEcertificateType().toString())) {dataMap.put("ecertificatetype", certificateTypeList.get(i).get("name"));} else {dataMap.put("ecertificatetype", "");}}} else {dataMap.put("ecertificatetype", "");}if (temployee.getEcertificateId() != null) {dataMap.put("ecertificateid", temployee.getEcertificateId());} else {dataMap.put("ecertificateid", "");}if (temployee.getEsex()!=null) {sexList = this.personnelService.querySysCodeList("性别");for (int i = 0; i < sexList.size(); i++) {if (sexList.get(i).get("id").toString().equals(temployee.getEsex().toString())) {dataMap.put("esex", sexList.get(i).get("name"));} else {dataMap.put("esex", "");} }} else {dataMap.put("esex", "");}if (temployee.getEtel() != null) {dataMap.put("etel", temployee.getEtel());} else {dataMap.put("etel", "");}if (temployee.getEemail() != null) {dataMap.put("eemail", temployee.getEemail());} else {dataMap.put("eemail", "");}if (temployee.getEbankName() != null) {dataMap.put("ebankname", temployee.getEbankName());} else {dataMap.put("ebankname", "");}if (temployee.getEbankNo() != null) {dataMap.put("ebankno", temployee.getEbankNo());} else {dataMap.put("ebankno", "");}if (temployee.getEpolitical()!=null) {politicalList = this.personnelService.querySysCodeList("政治面貌");for (int i = 0; i < politicalList.size(); i++) {if (politicalList.get(i).get("id").toString().equals(temployee.getEpolitical().toString())) {dataMap.put("epolitical", politicalList.get(i).get("name"));} else {dataMap.put("epolitical", "");} }} else {dataMap.put("epolitical", "");}if (temployee.getEpostCode() != null) {dataMap.put("epostcode", temployee.getEpostCode());} else {dataMap.put("epostcode", "");}if (temployee.getEhomeTel() != null) {dataMap.put("ehometel", temployee.getEhomeTel());} else {dataMap.put("ehometel", "");}if (temployee.getEmarriage() != null) {marriageList = this.personnelService.querySysCodeList("婚姻状况");for (int i = 0; i < marriageList.size(); i++) {if (marriageList.get(i).get("id").toString().equals(temployee.getEmarriage().toString())) {dataMap.put("emarriage", marriageList.get(i).get("name"));} else {dataMap.put("emarriage", "");} }} else {dataMap.put("emarriage", "");}if (temployee.getEdegree()!=null) {degreeList = this.personnelService.querySysCodeList("学历");for (int i = 0; i < degreeList.size(); i++) {if (degreeList.get(i).get("id").toString().equals(temployee.getEdegree().toString())) {dataMap.put("edegree", degreeList.get(i).get("name"));} else {dataMap.put("edegree", "");} }} else {dataMap.put("edegree", "");}if (temployee.getEworkYear() != null) {dataMap.put("eworkyear", temployee.getEworkYear());} else {dataMap.put("eworkyear", "");}if (temployee.getEgraduateSchool() != null) {dataMap.put("egraduateschool", temployee.getEgraduateSchool());} else {dataMap.put("egraduateschool", "");}if (temployee.getEspecial() != null) {dataMap.put("especial", temployee.getEspecial());} else {dataMap.put("especial", "");}if(temployee.getEgraduateDate()!=null && !"".equals(temployee.getEgraduateDate())){dataMap.put("egraduatedate", sdf.format(temployee.getEgraduateDate()));} else {dataMap.put("egraduatedate", "");}if (temployee.getEcompetence()!=null) {competenceList = this.personnelService.querySysCodeList("职业资格");for (int i = 0; i < competenceList.size(); i++) {if (competenceList.get(i).get("id").toString().equals(temployee.getEcompetence().toString())) {dataMap.put("ecompetence", competenceList.get(i).get("name"));} else {dataMap.put("ecompetence", "");} }} else {dataMap.put("ecompetence", "");}if (temployee.getEqualifications()!=null) {qualificationsList = this.personnelService.querySysCodeList("教师职称");for (int i = 0; i < qualificationsList.size(); i++) {if (qualificationsList.get(i).get("id").toString().equals(temployee.getEqualifications().toString())) {dataMap.put("equalifications", qualificationsList.get(i).get("name"));} else {dataMap.put("equalifications", "");} }} else {dataMap.put("equalifications", "");}if(temployee.getEinSchoolDate()!=null && !"".equals(temployee.getEinSchoolDate())){dataMap.put("einschooldate", sdf.format(temployee.getEinSchoolDate()));} else {dataMap.put("einschooldate", "");}if (temployee.getEcredentials() != null) {dataMap.put("ecredentials", temployee.getEcredentials());} else {dataMap.put("ecredentials", "");}if (temployee.getEaddr() != null) {dataMap.put("eaddr", temployee.getEaddr());} else {dataMap.put("eaddr", "");}if (temployee.getEhomeAddr() != null) {dataMap.put("ehomeaddr", temployee.getEhomeAddr());} else {dataMap.put("ehomeaddr", "");}if (temployee.getEremark() != null) {dataMap.put("eremark", temployee.getEremark());} else {dataMap.put("eremark", "");}
//上面是获取数据,下面是导出的方法
try {String address=ServletActionContext.getServletContext().getRealPath("/");String serverName = ServletActionContext.getRequest().getScheme()+"://"+ServletActionContext.getRequest().getServerName()+":"+ServletActionContext.getRequest().getServerPort()+ServletActionContext.getRequest().getContextPath()+"/";byte[] pic=temployee.getEphoto();String dir=address+"images/student/"+temployee.getEcode()+".jpg";FileOutputStream fos = null;fos = new FileOutputStream(dir);if(pic==null){dataMap.put("stuphoto",serverName+"images/student/default.jpg");}else {fos.write(pic);dataMap.put("stuphoto",serverName+"images/student/"+temployee.getEcode()+".jpg");}fos.close();} catch (Exception e) {dataMap.put("stuphoto", "无相片");}String ftlFolder = "/com/mds/util/ftl";String ftlFileName = "employee.ftl";String outFilePath = request.getRealPath("/UploadFile");sdf = new SimpleDateFormat("yyyyMMddHHmmss");String filePath = outFilePath+"\\"+sdf.format(new Date())+".doc";;outFilePath = outFilePath+"/"+sdf.format(new Date())+".doc";//DocumentHandler.createDoc(dataMap, ftlFolder, ftlFileName, outFilePath);// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,      // 这里我们的模板是放在com.havenliu.document.template包下面   Configuration  configuration = new Configuration();   configuration.setDefaultEncoding("utf-8");   configuration.setClassForTemplateLoading(PersonnelAction.class, ftlFolder);      Template t = null;      try {         // ftlFileName为要装载的模板     t = configuration.getTemplate(ftlFileName);         t.setEncoding("utf-8");         System.out.println(t.toString());      } catch (IOException e) {         e.printStackTrace();      }      // 输出文档路径及名称      File outFile = new File(outFilePath);      Writer out = null;         //out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(outFile), "utf-8"));response.setHeader("Content-Disposition","attachment;filename="+new String("教师档案表".getBytes("GB2312"),"ISO8859_1")+".doc");response.setCharacterEncoding("utf-8");out = response.getWriter();         t.process(dataMap, out);         out.close();//response.setContentType("application/msexcel");//OutputStream os = response.getOutputStream();} catch (Exception e) {e.printStackTrace();}}
// 下面是前台html模板
<html><head>  <title>Welcome!</title>  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8"></head><body>  <h1>员工档案表</h1><table width="724" border="1" cellpadding='0' cellspacing='0' bordercolor='#000000' style='border-collapse:collapse;'>  <tr>    <td width="92">中文名:</td>    <td width="166">${ename}</td>    <td width="95">英文名:</td>    <td width="175">${eenname}</td>    <td width="184" rowspan="10"><input name="image" type="image" src="${stuphoto}" align="top" width="180" height="180" /></td>  </tr>  <tr>    <td>曾用名:</td>    <td>${eusename}</td>    <td>员工号:</td>    <td>${ecode}</td>  </tr>  <tr>    <td>出生日期:</td>    <td>${ebirthday}</td>    <td>民族:</td>    <td>${enation}</td>  </tr>  <tr>    <td>籍贯:</td>    <td>${enative}</td>    <td>证件类型:</td>    <td>${ecertificatetype}</td>  </tr>  <tr>    <td>证件编号:</td>    <td>${ecertificateid}</td>    <td>性别:</td>    <td>${esex}</td>  </tr>  <tr>    <td>联系电话:</td>    <td>${etel}</td>    <td>Email:</td>    <td>${eemail}</td>  </tr>  <tr>    <td>开户银行:</td>    <td>${ebankname}</td>    <td>银行账户:</td>    <td>${ebankno}</td>  </tr>  <tr>    <td>政治面貌:</td>    <td>${epolitical}</td>    <td>婚姻状况:</td>    <td>${emarriage}</td>  </tr>  <tr>    <td>邮政编码:</td>    <td>${epostcode}</td>    <td>固定电话:</td>    <td>${ehometel}</td>  </tr>  <tr>    <td>最高学历:</td>    <td>${edegree}</td>    <td>工作年限:</td>    <td>${eworkyear}</td>  </tr>  <tr>    <td>毕业院校:</td>    <td>${egraduateschool}</td>    <td>职业资格:</td>    <td>${ecompetence}</td>    <td width="184" rowspan="7"> </td>  </tr>  <tr>    <td>所学专业:</td>    <td>${especial}</td>    <td>毕业时间:</td>    <td>${egraduatedate}</td>  </tr>  <tr>    <td>职称:</td>    <td>${equalifications}</td>    <td>进学院时间:</td>    <td>${einschooldate}</td>  </tr>  <tr>    <td>教师资格:</td>    <td colspan="3">${ecredentials}</td>  </tr>  <tr>    <td>联系地址:</td>    <td colspan="3">${eaddr}</td>  </tr>  <tr>    <td>家庭地址:</td>    <td colspan="3">${ehomeaddr}</td>  </tr>  <tr>    <td height="100" align="left" valign="top">个人简介:</td>    <td colspan="3" valign="top">${eremark}</td>  </tr></table></body></html> 

	
				
		
原创粉丝点击