java 前端导出exvel_java导出数据到Excel文件 前端进行下载

/**

* 导出宿舍人员信息 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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值