檔案匯出下載
////檔案匯出
//@ResponseBody
@GetMapping("/execl")
@RequiresPermissions("studentmanage:studentinfo:execl")
public void execl(@RequestParam Map<String, Object> params ,
HttpServletRequest request, HttpServletResponse response) throws Exception{
//查詢列表資料
UserDO user=getUser();
String hallId=user.getHallId();
if(1!=getUserId().intValue()) {
if(hallId != null&&hallId!="") {
params.put("hallId", hallId);
}else {
//return null;
}
}
params.put("flag", "1");
params.put("limit", "40");
params.put("offset", "0");
Query query = new Query(params);
List<StudentVO> financeList = studentService.list(query);
//
String fileName="學生資訊";
// response.setContentType("application/excel");
// response.setHeader("Content-disposition","attachment;filename=" + fileName +";filename*=utf-8''"+ URLEncoder.encode(fileName,"UTF-8"));
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();
//style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 建立一個居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("序號");
cell = row.createCell((short) 1);
cell.setCellValue("學員姓名");
cell = row.createCell((short) 2);
cell.setCellValue("道館名稱");
cell = row.createCell((short) 3);
cell.setCellValue("班級名稱");
cell = row.createCell((short) 4);
cell.setCellValue("級別名稱");
cell = row.createCell((short) 5);
cell.setCellValue("性別");
cell = row.createCell((short) 6);
cell.setCellValue("聯絡方式");
cell = row.createCell((short) 7);
cell.setCellValue("協議人");
cell = row.createCell((short) 8);
cell.setCellValue("報名日期");
cell = row.createCell((short) 9);
cell.setCellValue("加入方式");
cell = row.createCell((short) 10);
cell.setCellValue("所屬學校");
// 第五步,寫入實體資料 實際應用中這些資料從資料庫得到,
for(int i=0;i<financeList.size();i++){
StudentVO map =(StudentVO)financeList.get(i);
// 第四步,建立單元格,並設定值
row = sheet.createRow((int) i+1);
HSSFCell celli = row.createCell((short) 0);
row.createCell((short) 0).setCellValue(i);
if(map.getStudentName()!=null)//學員姓名
row.createCell((short) 1).setCellValue((String) map.getStudentName());
if(map.getHallName()!=null)//道館名稱
row.createCell((short) 2).setCellValue((String) map.getHallName());
if(map.getClassName()!=null)//班級名稱
row.createCell((short) 3).setCellValue((String) map.getClassName());
if(map.getLevelName()!=null)//級別名稱
row.createCell((short) 4).setCellValue((String) map.getLevelName());
if(map.getStudentSex()!=null)//性別
row.createCell((short) 5).setCellValue((String) map.getStudentSex()=="0"?"男":"女");
if(map.getStudentContact()!=null)//聯絡方式
row.createCell((short) 6).setCellValue((String) map.getStudentContact());
if(map.getStudentFiduciary()!=null)//協議人
row.createCell((short) 7).setCellValue((String) map.getStudentFiduciary());
if(map.getStudentRegistDate()!=null)//報名時間
row.createCell((short) 8).setCellValue( map.getStudentRegistDate().toLocaleString());
if(map.getStudentJoinMethod()!=null)//加入方式
row.createCell((short) 9).setCellValue((String) map.getStudentJoinMethod());
if(map.getStudentSchool()!=null)//所屬學校
row.createCell((short) 10).setCellValue((String) map.getStudentSchool());
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
wb.write(os);
} catch (IOException e) {
e.printStackTrace();
}
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 設定response引數,可以開啟下載頁面
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
ServletOutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (final IOException e) {
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}