注意:一定不能使用Ajax请求方式,否则 不报错,但是就是下载不下来
1.效果

2.实现过程
2.1 导入jar
<!-- 13.excel导出 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>

2.2 前端请求

2.3 后台
2.3.1 controller
/** * 6.查询所有员工信息 在职在前 离职在后 * @Title: exportEmpoyeeInfo * @Description: * @return void * @throws IOException * @throws @date 2018年6月14日 下午5:15:19 */ @RequestMapping("/exportEmpoyeeInfo.action") public void exportEmpoyeeInfo(HttpServletResponse response ) throws IOException{ response.setCharacterEncoding("UTF-8"); List<User> list = employeeService.queryAllEmployeeInfoToExcel(); System.out.println(list); //创建excel文件 HSSFWorkbook wb = new HSSFWorkbook(); //创建sheet页 HSSFSheet sheet = wb.createSheet("员工信息表"); //创建标题行 HSSFRow titleRow = sheet.createRow(0); titleRow.createCell(0).setCellValue("姓名"); titleRow.createCell(1).setCellValue("爱好"); titleRow.createCell(2).setCellValue("工作年限"); titleRow.createCell(3).setCellValue("电话号码"); titleRow.createCell(4).setCellValue("身份证号码"); titleRow.createCell(5).setCellValue("户籍"); titleRow.createCell(6).setCellValue("入职时间"); titleRow.createCell(7).setCellValue("所在部门"); titleRow.createCell(8).setCellValue("职位"); titleRow.createCell(9).setCellValue("状态 "); titleRow.createCell(10).setCellValue("管理员"); titleRow.createCell(11).setCellValue("其他描述"); //遍历将数据放到excel列中 for (User user : list) { HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1); dataRow.createCell(0).setCellValue(user.getUsername()); dataRow.createCell(1).setCellValue(user.getHobby()); dataRow.createCell(2).setCellValue(user.getWorkage()); dataRow.createCell(3).setCellValue(user.getPhonenumber()); dataRow.createCell(4).setCellValue(user.getPersoncardnumber()); dataRow.createCell(5).setCellValue(user.getAddress()); dataRow.createCell(6).setCellValue(user.getCreatetime()); dataRow.createCell(7).setCellValue(user.getDepartment()); dataRow.createCell(8).setCellValue(user.getJob()); dataRow.createCell(9).setCellValue(user.getStatus()); dataRow.createCell(10).setCellValue(user.getIsadmin()); dataRow.createCell(11).setCellValue(user.getOther()); } /* // 设置下载时客户端Excel的名称 String filename =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ".xls"; response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=" + filename); */ // 设置下载时客户端Excel的名称 (上面注释的改进版本,上面的中文不支持) response.setContentType("application/octet-stream;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + new String("在职员工名单".getBytes(),"iso-8859-1") + ".xls"); OutputStream ouputStream = response.getOutputStream(); wb.write(ouputStream); ouputStream.flush(); ouputStream.close(); } |


2.3.2 service
2.3.2.1 接口

2.3.2.2 实现类

2.3.3 mapper
2.3.3.1 接口

2.3.3.2 映射文件、
