实体类
package cn.edu.hbcf.plugin.enroll.pojo;
import java.util.Date;
import cn.edu.hbcf.privilege.pojo.BaseUsers;
/**
* 录取结果POJO
* @author wufengbin
* @date 2017年2月16日
* @time 上午10:56:36
*
*/
public class EnrollResult {
/**
* 主键
*/
private String id;
/**
* 序列号
*/
private int xh;
/**
* 考生号
*/
private String ksh;
/**
* 身份证号
*/
private String sfzh;
/**
* 姓名
*/
private String xm;
/**
* 成绩
*/
private Double score;
/**
* 年份
*/
private String year;
/**
* 录取状态 0未录取 1已录取
*/
private int enrollStatus;
/**
* 录取系别
*/
private String enrollDepartment;
/**
* 录取专业
*/
private String enrollMajor;
/**
* 操作人
*/
private BaseUsers operUser;
/**
* 操作时间
*/
private Date operDate;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getXh() {
return xh;
}
public void setXh(int xh) {
this.xh = xh;
}
public String getKsh() {
return ksh;
}
public void setKsh(String ksh) {
this.ksh = ksh;
}
public String getSfzh() {
return sfzh;
}
public void setSfzh(String sfzh) {
this.sfzh = sfzh;
}
public String getXm() {
return xm;
}
public void setXm(String xm) {
this.xm = xm;
}
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public int getEnrollStatus() {
return enrollStatus;
}
public void setEnrollStatus(int enrollStatus) {
this.enrollStatus = enrollStatus;
}
public String getEnrollDepartment() {
return enrollDepartment;
}
public void setEnrollDepartment(String enrollDepartment) {
this.enrollDepartment = enrollDepartment;
}
public String getEnrollMajor() {
return enrollMajor;
}
public void setEnrollMajor(String enrollMajor) {
this.enrollMajor = enrollMajor;
}
public BaseUsers getOperUser() {
return operUser;
}
public void setOperUser(BaseUsers operUser) {
this.operUser = operUser;
}
public Date getOperDate() {
return operDate;
}
public void setOperDate(Date operDate) {
this.operDate = operDate;
}
}
生成表头 以及原始模拟数据
List<String> assignmentColumn(String str) {
List<String> list = new ArrayList<String>();
if ("xh".equals(str)) {
list.add("序列号");
list.add("1");
list.add("2");
list.add("3");
} else if ("ksh".equals(str)) {
list.add("考生号");
list.add("130624001");
list.add("130624002");
list.add("130624003");
} else if ("sfzh".equals(str)) {
list.add("身份证号");
list.add("130930199312120301");
list.add("130930199312120302");
list.add("130930199312120303");
} else if ("xm".equals(str)) {
list.add("姓名");
list.add("张三");
list.add("李四");
list.add("王五");
} else if ("score".equals(str)) {
list.add("成绩");
list.add("100");
list.add("99.6");
list.add("89");
} else if ("enrollStatus".equals(str)) {
list.add("录取状态(0未录取 1已录取)");
list.add("0");
list.add("1");
list.add("1");
} else if ("enrollDepartment".equals(str)) {
list.add("录取系别");
list.add("数学与计算机系");
list.add("外语系");
list.add("物理系");
} else if ("enrollMajor".equals(str)) {
list.add("录取专业");
list.add("软件工程");
list.add("商务英语");
list.add("物理学");
}else if ("year".equals(str)) {
list.add("2017");
list.add("2017");
list.add("2017");
list.add("2017");
}
return list;
}
生成Excel 模板
@Override
public void putOutExcel(HttpServletRequest request,
HttpServletResponse response) {
// 生成第一列和第二列的集合,写死的数据,就是下载的模板而已
OutputStream ouputStream = null;
List<String> xhColumn = assignmentColumn("xh");// 序号列
List<String> kshColumn = assignmentColumn("ksh");// 考生号列
List<String> sfzhColumn = assignmentColumn("sfzh");// 身份证号列
List<String> xmColumn = assignmentColumn("xm");// 姓名列
List<String> scoreColumn = assignmentColumn("score");// 分数列
List<String> enrollStatusColumn = assignmentColumn("enrollStatus");// 录取状态列
List<String> enrollDepartmentColumn = assignmentColumn("enrollDepartment");// 录取系别列
List<String> enrollMajorColumn = assignmentColumn("enrollMajor");// 录取专业列
List<String> yearColumn = assignmentColumn("year");// 录取年份列
try {
// 创建新的Excel 工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
// 如要新建一名为"Sheet1"的工作表
XSSFSheet sheet = workbook.createSheet("Sheet1");
for (int i = 0; i < xhColumn.size(); i++) {
// 在索引0的位置创建行(最顶端的行)
XSSFRow row = sheet.createRow(i);
for (int j = 0; j < 9; j++) {
// 在索引0的位置创建单元格(左上端)
XSSFCell cell = row.createCell( j);
// 定义单元格为字符串类型
//cell.setCellType(XSSFCell.CELL_TYPE_STRING);
// 在单元格中输入一些内容
if (j == 0) {
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(xhColumn.get(i));
} else if (j == 1) {
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(kshColumn.get(i));
} else if (j == 2) {
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(sfzhColumn.get(i));
} else if (j == 3) {
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(xmColumn.get(i));
} else if (j == 4) {
cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(scoreColumn.get(i));
} else if (j == 5) {
cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(enrollStatusColumn.get(i));
} else if (j == 6) {
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(enrollDepartmentColumn.get(i));
} else if (j == 7){
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(enrollMajorColumn.get(i));
} else {
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(yearColumn.get(i));
}
}
}
String fileName = "example.xlsx";// 设置下载时客户端Excel的名称
response.setContentType("application/vnd.x-xls");
// 解决中文乱码
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("gbk"), "iso-8859-1"));
ouputStream = response.getOutputStream();
workbook.write(ouputStream);
ouputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 操作结束,关闭文件
IOUtils.closeQuietly(ouputStream);
}
}
创建Excel模板 (2007)
最新推荐文章于 2020-12-24 10:49:11 发布