这几个月经常做excel的导出,今天就整理一下吧!!!
动态导出excel,通用与各种title的excel ,不需要模板和excel注解类
1 /** 2 * 导出餐费统计excel 3 */ 4 public void exportAttendList(){ 5 6 ByteArrayOutputStream baos = null; 7 ByteArrayInputStream bais = null; 8 try { 9 if (StringUtils.isBlank(param)) { 10 this.writeJson("0"); 11 return; 12 } 13 //获取前台的参数 14 JSONObject jo = JSONObject.parseObject(this.param); 15 List<String> dateList = com.alibaba.fastjson.JSONArray.toJavaObject(jo.getJSONArray("dateList"), List.class); 16 17 //对前台的数据进行判断 18 Integer classId = jo.getInteger("classId"); 19 if (dateList == null || dateList.size() == 0|| classId == null) { 20 this.writeJson(null, EcloudConstants.Flag.FAILURE.getIndex() 21 + "", ""); 22 return; 23 } 24 List<Map<String, Object>> list = this.getiEcloudAttendService().findEcloudAttendCountList(dateList, classId, getUserDto().getSchoolId()); 25 // 初始化参数 26 String title = "序号,学号,姓名,连续请假次数,连续请假详情"; 27 String titleName = "index,ecloudStudentNo,userName,count,list"; 28 //设置文件导出的名称 字符串加时间拼接 29 String fileName = "餐费统计表"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); 30 31 // 声明一个工作薄 32 HSSFWorkbook wb = new HSSFWorkbook(); 33 // 声明一个单子并命名 34 HSSFSheet sheet = wb.createSheet(fileName); 35 // 给单子名称一个长度 36 sheet.setDefaultColumnWidth((short) 15); 37 38 //设置列宽 39 sheet.setColumnWidth(0, "学号".getBytes().length * 2 * 256); 40 sheet.setColumnWidth(4, "2017-03-13 -- 2017-03-15 ( 3天 )".getBytes().length * 1 * 256); 41 // 生成一个样式 42 HSSFCellStyle style = wb.createCellStyle(); 43 // 创建第一行(也可以称为表头) 44 HSSFRow row = sheet.createRow(0); 45 // 样式字体居中 46 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中 47 style.setWrapText(true); 48 49 // 给表头第一行一次创建单元格 50 int i = 0; 51 for (String entry : title.split(",")) { 52 HSSFCell cell = row.createCell((short) i++); 53 cell.setCellValue(entry); 54 cell.setCellStyle(style); 55 } 56 57 for (int b=0; b<list.size(); b++) { 58 Map map = list.get(b); 59 row = sheet.createRow(b + 1); 60 for (int j = 0; j < titleName.split(",").length; j++) { 61 HSSFCell cell = row.createCell((short) j); 62 String name = titleName.split(",")[j]; 63 if (name.equals("index")) { 64 //记录序号 由于这个参数是递增的,所以需要自定义判断 65 cell.setCellValue(b + 1 + ""); 66 } else { 67 //遍历请假记录 68 if (name.equals("list")) { 69 List<String> list2 = (List<String>) map.get(name); 70 //设置行高 使用list的大小对行高进行设置 71 row.setHeight((short)(list2.size()*250)); 72 String t = ""; 73 //把list转为我们想要的参数 并加入换行符 最后不加 74 for (int k = 0; k < list2.size(); k++) { 75 t=k!=list2.size()-1?(t+list2.get(k)+"\r\n"):t+list2.get(k); 76 } 77 cell.setCellValue(t); 78 }else if(name.equals("count")){ 79 //请假次数 根据需求增加一些字符串拼接 80 cell.setCellValue(map.get(name) != null ? map.get(name).toString()+"次": ""); 81 }else{ 82 //普通内容渲染 83 cell.setCellValue(map.get(name) != null ? map.get(name).toString(): ""); 84 } 85 } 86 cell.setCellStyle(style); 87 } 88 } 89 90 //下面这段是一个固定的文件输出格式 91 baos = new ByteArrayOutputStream(); 92 wb.write(baos); 93 HttpServletResponse response = this.getResponse(); 94 response.setContentType("application/vnd.ms-excel;charset=utf-8"); 95 String filename = this.encodeDownloadFilename(fileName, 96 getRequest().getHeader("User-Agent")) + ".xls"; 97 response.setHeader("Content-Disposition", "attachment;filename=" 98 + filename); 99 bais = new ByteArrayInputStream(baos.toByteArray()); 100 int b; 101 while ((b = bais.read()) != -1) { 102 response.getOutputStream().write(b); 103 } 104 response.getOutputStream().flush(); 105 bais.close(); 106 baos.close(); 107 } catch (Exception e) { 108 e.printStackTrace(); 109 this.writeJson("0"); 110 } 111 } 112
时间: 2024-10-23 03:08:11