<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>

/**
* 打印考生成绩
* @return
*/
@RequestMapping("/getExamScore")
public void downloadExamScore(HttpServletResponse response, HttpSession httpSession) throws IOException {
String ordId = (String) httpSession.getAttribute("ordId");
//声明一个工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//声明一个表格
HSSFSheet sheet = workbook.createSheet("成绩表");
//设置表格的默认宽度
sheet.setDefaultColumnWidth((short) 30);
//需要打印的信息
List<ExamScoreView> examScoreList = examSeatService.getExamScore(ordId);
//需要导出文件的名字
String fileName = "成绩表" + ".xls";
int rowNum = 1;
//导出Excel表格头部
String[] headers = {"学员姓名", "学员身份证号", "性别", "学历", "工作单位", "手机号", "邮箱", "单位电话", "单位地址", "健康状况", "身份证件类型"
, "初(复)训", "学员标识", "所属班级", "是否补考", "预约考试地点", "单位电话", "考试申请记录标识", "考试场次标识", "阅卷人", "得分", "是否合格", "准考证id"
, "考试类别编号", "准考证号", "考点编号", "考点地址", "考试时间", "考试类别名称", "教室编号"
, "机位号", "机位编号", "考场名称", "负责人名字", "负责人电话", "负责人邮箱", "开始考试时间", "结束考试时间", "场次编号", "操作类型"};
HSSFRow row = sheet.createRow(0);
//在Excel中添加表头
for (int i = 0; i < headers.length; i++) {
HSSFCell cell = row.createCell(i);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
//在表中存放查询到的数据放入对应的列
for (ExamScoreView examScoreView : examScoreList) {
HSSFRow row1 = sheet.createRow(rowNum);
row1.createCell(0).setCellValue(examScoreView.getStuName());
row1.createCell(1).setCellValue(examScoreView.getStuCardId());
row1.createCell(2).setCellValue(examScoreView.getGender());
row1.createCell(3).setCellValue(examScoreView.getEduCode());
row1.createCell(4).setCellValue(examScoreView.getWorkUnit());
row1.createCell(5).setCellValue(examScoreView.getPhon());
row1.createCell(6).setCellValue(examScoreView.getEmail());
row1.createCell(7).setCellValue(examScoreView.getUnitPhon());
row1.createCell(8).setCellValue(examScoreView.getUnitAddr());
row1.createCell(9).setCellValue(examScoreView.getHealth());
row1.createCell(10).setCellValue(examScoreView.getStuCardType());
row1.createCell(11).setCellValue(examScoreView.getFrCode());
row1.createCell(12).setCellValue(examScoreView.getsId());
row1.createCell(13).setCellValue(examScoreView.getClassId());
row1.createCell(14).setCellValue(examScoreView.getIsMakeup());
row1.createCell(15).setCellValue(examScoreView.getAplyAddress());
row1.createCell(16).setCellValue(examScoreView.getUnitPost());
row1.createCell(17).setCellValue(examScoreView.getAplyId());
row1.createCell(18).setCellValue(examScoreView.getOrdId());
row1.createCell(19).setCellValue(examScoreView.getExamInesName());
row1.createCell(20).setCellValue(examScoreView.getScore());
row1.createCell(21).setCellValue(examScoreView.getIsPass());
row1.createCell(22).setCellValue(examScoreView.getAdmId());
row1.createCell(23).setCellValue(examScoreView.getcId());
row1.createCell(24).setCellValue(examScoreView.getAdmNo());
row1.createCell(25).setCellValue(examScoreView.getSiteId());
row1.createCell(26).setCellValue(examScoreView.getTestsitesAddress());
row1.createCell(27).setCellValue(examScoreView.getExamTypeName());
row1.createCell(28).setCellValue(examScoreView.getRoomId());
row1.createCell(29).setCellValue(examScoreView.getSeatNo());
row1.createCell(30).setCellValue(examScoreView.getSeatCode());
row1.createCell(31).setCellValue(examScoreView.getRoomName());
row1.createCell(32).setCellValue(examScoreView.getPersonName());
row1.createCell(33).setCellValue(examScoreView.getPersonMobile());
row1.createCell(34).setCellValue(examScoreView.getPersonEmail());
row1.createCell(35).setCellValue(examScoreView.getOrdTime());
row1.createCell(36).setCellValue(examScoreView.getOrdEndTime());
row1.createCell(37).setCellValue(examScoreView.getOrdNo());
row1.createCell(38).setCellValue(examScoreView.getOpType());
rowNum++;
}
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.flushBuffer();
workbook.write(response.getOutputStream());
}
12-18 02:16