1.pom.xml引入依赖
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.6</version></dependency>
2.页面发送请求
window.location.href = “/exportInfoList?”+param;
3.后台Controller接收请求
//导出列表@RequestMapping(value = "exportInfoList")public void exportInfoList(String param, HttpServletResponse response) {OutputStream outputStream=null;try {HSSFWorkbook workbook= service.exportExcel(param);outputStream = response.getOutputStream();response.reset();response.setContentType("application/msexcel");//response.setCharacterEncoding("utf-8");//设置浏览器响应头对应的Content-disposition 解决中文名字乱码String filename = "XX管理";response.setHeader("Content-disposition", "attachment;filename="+new String(filename.getBytes("gbk"), "iso8859-1")+".xls");workbook.write(outputStream);} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} finally {try {outputStream.flush();outputStream.close();} catch (IOException e) {e.printStackTrace();}}}
4.Service方法
//XX管理导出excelpublic HSSFWorkbook exportExcel(String param){HSSFWorkbook workbook=new HSSFWorkbook();try {OutputStream outputStream = null;List<JpCustomerInfo> list = mapper.selectListByPage(param);HSSFSheet sheet = workbook.createSheet("XX管理");Row row = sheet.createRow(0);// excle标题String[] ar = new String[]{"姓名", "身份证号", "渠道来源", "手机号", "省份", "app是否登录", "注册时间"};for (int i = 0; i < ar.length; i++) {Cell cell = row.createCell(i);cell.setCellValue(ar[i]);sheet.setColumnWidth(i,ar[i].getBytes().length*350);}//excel 内容for (int s = 0; s < list.size(); s++) {JpCustomerInfo info = list.get(s);row = sheet.createRow(s + 1);row.createCell(0).setCellValue(info.getName());row.createCell(1).setCellValue(info.getIdNumber());row.createCell(2).setCellValue(info.getChannelName());row.createCell(3).setCellValue(info.getPhone());row.createCell(4).setCellValue(info.getProvince()); //省份row.createCell(5).setCellValue(info.getAppIsLogin() == 0 ? "是" : "否"); //app是否登录Date date =info.getRegisterTime();if (date==null){row.createCell(6).setCellValue(""); //注册时间}else{String time=DateFormatUtils.format(date,"yyyy-MM-dd HH-mm-ss");row.createCell(6).setCellValue(time); //注册时间}}} catch (Exception e) {e.printStackTrace();}return workbook;}