文件导出
//@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();
}
}
文件导出下载
转载上一篇:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【导出Excel】Vue实现导出下载Excel文件(blob文件流)--亲测可用
// 下载blob文件流 downloadFile(res, name) { const blob = new Blob([r
vue.js javascript 前端 ajax ios