HttpServletResponse response = getResponse();
Integer test_id = getParaToInt("test_id");
// sql 查询出来的列表
List<UserTest> list = UserTest.dao.find(" ");
//创建Excel 对象1
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
//设置sheet
HSSFSheet sheet = hssfWorkbook.createSheet("分数排行榜");
//设置表头
HSSFRow headRow = sheet.createRow(0);
headRow.createCell(0).setCellValue("ID");
headRow.createCell(1).setCellValue("试卷名称");
headRow.createCell(2).setCellValue("分数");
headRow.createCell(3).setCellValue("姓名");
headRow.createCell(4).setCellValue("手机");
headRow.createCell(5).setCellValue("地址");
//填充表数据 循环列表并赋值
for(int i=0;i<list.size();i++){
HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
dataRow.createCell(0).setCellValue(list.get(i).getStr("w_id"));
dataRow.createCell(1).setCellValue(list.get(i).getStr("title"));
dataRow.createCell(2).setCellValue(list.get(i).getScore());
dataRow.createCell(3).setCellValue(list.get(i).getStr("name"));
dataRow.createCell(4).setCellValue(list.get(i).getStr("phone"));
dataRow.createCell(5).setCellValue(list.get(i).getStr("site"));
}
String filename = "分数排行表.xls";
// 设置响应头,对文件进行url编码
// 第一步:设置响应类型
// 下载导出
// 设置头信息
response.setContentType("application/force-download");
filename = URLEncoder.encode(filename, "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
ServletOutputStream outputStream = response.getOutputStream();
hssfWorkbook.write(outputStream);
outputStream.flush();
// 关闭
outputStream.close();