为什么要写这个小Demo:每次收集学生信息汇总是最让人头疼的事性,因为每个人提交完以后还要往Excel表格中录入。
实现技术:easyui+spring+struts+poi
实现功能步骤:
第一步:
写一个静态页面,form表单提交到后台action中from表单数据
第二步:
配置web.xml配置文件
HM index.html index.htm index.jsp default.html default.htm default.jsp org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:applicationContext.xml StrutsPrepareAndExecuteFilter org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter StrutsPrepareAndExecuteFilter /*
第三步:
配置applicationContext.xml
第四步:
创建实体类,实体类的字段要和form表中的字段一致
package com.kaifang.domain; public class Student { private Integer id; private String username;// 姓名 private String sex;// 姓别 private String age;// 年龄 private String qq;// qq private String phone;// 本人手机 private String identification;// 身份证号 private String email;// 邮件 private String meAddress;// 现住地址 private String school;// 学校 private String graduateTime;// 毕业时间 private String specialty;// 专业 private String educationalBackground;// 学历 private String parents;// 家庭联系人 private String parentsPhone;// 家庭联系电话 private String maritalStatus;// 婚否 private String children;// 是否有子女 private String homeAddress;// 家庭住址 private String work;// 是否有工作经验 private String workLast;// 最后工作 private String english;// 英语水平 private String upgrade;// 是否升级 private String cause; public String getCause() { return cause; } public void setCause(String cause) { this.cause = cause; } private String remark;// 备注 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getQq() { return qq; } public void setQq(String qq) { this.qq = qq; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getIdentification() { return identification; } public void setIdentification(String identification) { this.identification = identification; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getMeAddress() { return meAddress; } public void setMeAddress(String meAddress) { this.meAddress = meAddress; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public String getGraduateTime() { return graduateTime; } public void setGraduateTime(String graduateTime) { this.graduateTime = graduateTime; } public String getSpecialty() { return specialty; } public void setSpecialty(String specialty) { this.specialty = specialty; } public String getEducationalBackground() { return educationalBackground; } public void setEducationalBackground(String educationalBackground) { this.educationalBackground = educationalBackground; } public String getParents() { return parents; } public void setParents(String parents) { this.parents = parents; } public String getParentsPhone() { return parentsPhone; } public void setParentsPhone(String parentsPhone) { this.parentsPhone = parentsPhone; } public String getMaritalStatus() { return maritalStatus; } public void setMaritalStatus(String maritalStatus) { this.maritalStatus = maritalStatus; } public String getChildren() { return children; } public void setChildren(String children) { this.children = children; } public String getHomeAddress() { return homeAddress; } public void setHomeAddress(String homeAddress) { this.homeAddress = homeAddress; } public String getWork() { return work; } public void setWork(String work) { this.work = work; } public String getWorkLast() { return workLast; } public void setWorkLast(String workLast) { this.workLast = workLast; } public String getEnglish() { return english; } public void setEnglish(String english) { this.english = english; } public String getUpgrade() { return upgrade; } public void setUpgrade(String upgrade) { this.upgrade = upgrade; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } @Override public String toString() { return "Student [id=" + id + ", username=" + username + ", sex=" + sex + ", age=" + age + ", qq=" + qq + ", phone=" + phone + ", identification=" + identification + ", email=" + email + ", meAddress=" + meAddress + ", school=" + school + ", graduateTime=" + graduateTime + ", specialty=" + specialty + ", educationalBackground=" + educationalBackground + ", parents=" + parents + ", parentsPhone=" + parentsPhone + ", maritalStatus=" + maritalStatus + ", children=" + children + ", homeAddress=" + homeAddress + ", work=" + work + ", workLast=" + workLast + ", english=" + english + ", upgrade=" + upgrade + ", cause=" + cause + ", remark=" + remark + "]"; } }
第五步:
编写Studentaction 使用ModelDriven模型驱动获取form表单交的数据并写到D盘根目录下保存,获取提交人员的IP实现只提交一次的操作!
package com.kaifang.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.HashSet; import java.util.Set; import javax.servlet.http.HttpServletRequest; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.struts2.ServletActionContext; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.kaifang.domain.Student; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; @ParentPackage("struts-default") @Namespace("/") @Controller @Scope("prototype") public class StudentAction extends ActionSupport implements ModelDriven { private static final long serialVersionUID = -2974993378615025102L; private Student student = new Student(); private static Set set = new HashSet(); static int i = 0; @Override public Student getModel() { return student; } @Action(value = "student_save", results = { @Result(name = "success", type = "redirect", location = "success.html"), @Result(name = "input", type = "redirect", location = "error.html") }) public synchronized String save() throws IOException { String ip = getClientIP(); if (!set.add(ip)) { return INPUT; } String path = "D:/"; String fileName = "学生信息收集"; String fileType = "xlsx"; Workbook hssfWorkbook = null; Sheet sheet = null; String excelPath = path + File.separator + fileName + "." + fileType; File file = new File(excelPath); // 创建工作文档对象 // 判断是否有文件 if (!file.exists()) { // 没有文件 if (fileType.equals("xls")) { hssfWorkbook = new HSSFWorkbook(); } else if (fileType.equals("xlsx")) { hssfWorkbook = new XSSFWorkbook(); } else { System.out.println("文件格式不正确"); } // 创建sheet对象 sheet = (Sheet) hssfWorkbook.createSheet("学员信息收集"); OutputStream outputStream = new FileOutputStream(excelPath); // 表头 Row headRow = sheet.createRow(0); headRow.createCell(0).setCellValue("序号"); headRow.createCell(1).setCellValue("姓名"); headRow.createCell(2).setCellValue("性别"); headRow.createCell(3).setCellValue("年龄"); headRow.createCell(4).setCellValue("QQ"); headRow.createCell(5).setCellValue("本人手机"); headRow.createCell(6).setCellValue("身份证号"); headRow.createCell(7).setCellValue("邮件"); headRow.createCell(8).setCellValue("现住地址"); headRow.createCell(9).setCellValue("学校"); headRow.createCell(10).setCellValue("毕业时间"); headRow.createCell(11).setCellValue("专业"); headRow.createCell(12).setCellValue("学历"); headRow.createCell(13).setCellValue("家庭联系人"); headRow.createCell(14).setCellValue("家庭联系电话"); headRow.createCell(15).setCellValue("婚否"); headRow.createCell(16).setCellValue("是否有子女"); headRow.createCell(17).setCellValue("家庭住址"); headRow.createCell(18).setCellValue("是否有工作经验"); headRow.createCell(19).setCellValue("最后工作"); headRow.createCell(20).setCellValue("英语水平"); headRow.createCell(21).setCellValue("是否升级"); headRow.createCell(22).setCellValue("不升级原因"); headRow.createCell(23).setCellValue("备注"); hssfWorkbook.write(outputStream); outputStream.flush(); outputStream.close(); } FileInputStream fs = new FileInputStream(file); // 有文件 if (fileType.equals("xls")) { hssfWorkbook = new HSSFWorkbook(fs); } else if (fileType.equals("xlsx")) { hssfWorkbook = new XSSFWorkbook(fs); } else { System.out.println("文件格式不正确"); } sheet = hssfWorkbook.getSheetAt(0); Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1); dataRow.createCell(0).setCellValue(set.size()); if (!"".equals(student.getUsername()) || null != student.getUsername()) { dataRow.createCell(1).setCellValue(student.getUsername()); } if (!"".equals(student.getSex()) || null != student.getSex()) { dataRow.createCell(2).setCellValue(student.getSex()); } if (!"".equals(student.getAge()) || null != student.getAge()) { dataRow.createCell(3).setCellValue(student.getAge()); } if (!"".equals(student.getQq()) || null != student.getQq()) { dataRow.createCell(4).setCellValue(student.getQq()); } if (!"".equals(student.getPhone()) || null != student.getPhone()) { dataRow.createCell(5).setCellValue(student.getPhone()); } if (!"".equals(student.getIdentification()) || null != student.getIdentification()) { dataRow.createCell(6).setCellValue(student.getIdentification()); } if (!"".equals(student.getEmail()) || null != student.getEmail()) { dataRow.createCell(7).setCellValue(student.getEmail()); } if (!"".equals(student.getMeAddress()) || null != student.getMeAddress()) { dataRow.createCell(8).setCellValue(student.getMeAddress()); } if (!"".equals(student.getSchool()) || null != student.getSchool()) { dataRow.createCell(9).setCellValue(student.getSchool()); } if (!"".equals(student.getGraduateTime()) || null != student.getGraduateTime()) { dataRow.createCell(10).setCellValue(student.getGraduateTime()); } if (!"".equals(student.getSpecialty()) || null != student.getSpecialty()) { dataRow.createCell(11).setCellValue(student.getSpecialty()); } if (!"".equals(student.getEducationalBackground()) || null != student.getEducationalBackground()) { dataRow.createCell(12).setCellValue(student.getEducationalBackground()); } if (!"".equals(student.getParents()) || null != student.getParents()) { dataRow.createCell(13).setCellValue(student.getParents()); } if (!"".equals(student.getParentsPhone()) || null != student.getParentsPhone()) { dataRow.createCell(14).setCellValue(student.getParentsPhone()); } if (!"".equals(student.getMaritalStatus()) || null != student.getMaritalStatus()) { dataRow.createCell(15).setCellValue(student.getMaritalStatus()); } if (!"".equals(student.getChildren()) || null != student.getChildren()) { dataRow.createCell(16).setCellValue(student.getChildren()); } if (!"".equals(student.getHomeAddress()) || null != student.getHomeAddress()) { dataRow.createCell(17).setCellValue(student.getHomeAddress()); } if (!"".equals(student.getWork()) || null != student.getWork()) { dataRow.createCell(18).setCellValue(student.getWork()); } if (!"".equals(student.getWorkLast()) || null != student.getWorkLast()) { dataRow.createCell(19).setCellValue(student.getWorkLast()); } if (!"".equals(student.getEnglish()) || null != student.getEnglish()) { dataRow.createCell(20).setCellValue(student.getEnglish()); } if (!"".equals(student.getUpgrade()) || null != student.getUpgrade()) { dataRow.createCell(21).setCellValue(student.getUpgrade()); } if (!"".equals(student.getCause()) || null != student.getCause()) { dataRow.createCell(22).setCellValue(student.getCause()); } if (!"".equals(student.getRemark()) || null != student.getRemark()) { dataRow.createCell(23).setCellValue(student.getRemark()); } // String title[] = { "序号", "姓名", "性别", "年龄", "QQ", "本人手机", "身份证号", // "邮件","现住地址","学校","毕业时间","专业","学历","家庭联系人","家庭联系电话","婚否","是否有子女" // ,"家庭住址","是否有工作经验","最后工作","英语水平","是否升级","不升级原因","意见或建议"}; // 创建文件流 OutputStream stream = new FileOutputStream(excelPath); // 写入数据 hssfWorkbook.write(stream); // 关闭 hssfWorkbook.close(); fs.close(); System.out.println("IP是:" + ip + " 学生叫:" + student.getUsername() + " 已经提交:" + set.size()); return SUCCESS; } // 获取提交的IP public String getClientIP() { HttpServletRequest request = ServletActionContext.getRequest(); String ip = request.getHeader("x-forwarded-for"); if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("X-Real-IP"); } if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; } }