/**
* 导出宿舍人员信息 Excel文件
* @param type
* @param id
* @param request
* @param response
* @return
* @throws IOException
*/
@GetMapping("/download/export/student")
public R setUpExcel(@Param("type") String type, @Param("id") String id, HttpServletRequest request, HttpServletResponse response) throws IOException{
List roomPersonnelList=roomPersonnelService.getRoomStudent(type, id,LyraUtils.getCompanyId());
/* if (ObjectUtil.isNull(roomPersonnelList)||roomPersonnelList.isEmpty()){
return R.failed("无数据导出");
}*/
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("学生信息");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
// HSSFCellStyle style = wb.createCellStyle();
wb.setSheetName(0, "宿舍学生信息");
String[] headers = { "学生姓名", "学号","性别","年级","班级","宿舍楼","宿舍号","床位号","入住时间"};
for (short i = 0; i < headers.length; i++) {
//创建单元格,每行多少数据就创建多少个单元格
HSSFCell cell = row.createCell(i);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
//给单元格设置内容
cell.setCellValue(text);
}
// 第五步,写入实体数据 这些数据从数据库得到,
HSSFRow rows;
HSSFCell cells;
for (int i = 0; i < roomPersonnelList.size(); i++) {
// 第三步:在这个sheet页里创建一行
rows = sheet.createRow(i+1);
// 第四步:在该行创建一个单元格
cells = rows.createCell(0);
// 第五步:在该单元格里设置值
cells.setCellValue(roomPersonnelList.get(i).getStuName());
cells = rows.createCell(1);
cells.setCellValue(roomPersonnelList.get(i).getStudentId());
cells = rows.createCell(2);
cells.setCellValue(roomPersonnelList.get(i).getGender());
cells = rows.createCell(3);
cells.setCellValue(roomPersonnelList.get(i).getGradeName());
cells = rows.createCell(4);
cells.setCellValue(roomPersonnelList.get(i).getClassName());
cells = rows.createCell(5);
cells.setCellValue(roomPersonnelList.get(i).getDormId());
cells = rows.createCell(6);
cells.setCellValue(roomPersonnelList.get(i).getRoomId());
cells = rows.createCell(7);
cells.setCellValue(roomPersonnelList.get(i).getBunkId());
cells = rows.createCell(8);
cells.setCellValue(roomPersonnelList.get(i).getCreateTime());
}
try {
String fileName = "宿舍人员信息.xls";// 文件名
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename="
+ URLEncoder.encode(fileName, "UTF-8"));
OutputStream out = response.getOutputStream();
wb.write(out);
wb.close();
} catch (Exception e) {
e.printStackTrace();
} /*finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}*/
return null;
}