论坛有个需求,导出参加活动的人数情况,用报表来保存,详细代码如下:
@RequestMapping(value = "/activity/export") @ResponseBody public String export(HttpServletRequest request, HttpServletResponse response) { long tid = NumberUtils.toLong(request.getParameter("tid"), 0l); Topic topic = topicService.findTopic(tid); Activity activity = activityService.findActivity(tid); response.reset();//清空缓存 response.setCharacterEncoding("GBK"); response.setHeader("Content-Disposition", "attachment; filename=\"" + "activity_" + topic.getTid() + ".csv\"");//生成的文件初始文件名称 //获取报表内容 StringBuilder sb = activityService.getExportContent(activity, topic); OutputStream os = null; try { os = response.getOutputStream(); byte[] byt = sb.toString().getBytes(); os.write(byt); } catch (Exception e) { log.error("输出活动参与报表失败", e.getStackTrace()); } finally { try { os.close(); } catch (IOException e) { log.error("输出活动参与报表关闭输出流失败", e.getStackTrace()); } } return null; }
其中activityService.getExportContent(activity, topic)这个方法是报表内容,关键代码如下:
//参与活动用户信息 boolean username = false, persons = false, phone = false, city = false, carNum = false, email = false, postNum = false, address = false, desc = false; sb.append("昵称"); if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_USERNAME))) { sb.append(","); sb.append("真实姓名"); sb.append(","); sb.append("用户ID"); sb.append(","); sb.append("用户名"); username = true; } if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_PERSONS))) { sb.append(","); sb.append("参加人数"); persons = true; } if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_PHONE))) { sb.append(","); sb.append("联系电话"); phone = true; } if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_CITY))) { sb.append(","); sb.append("所在城市"); city = true; } if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_CARNUM))) { sb.append(","); sb.append("车牌号码"); carNum = true; } if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_EMAIL))) { sb.append(","); sb.append("电子邮件"); email = true; } if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_POSTNUM))) { sb.append(","); sb.append("邮政编码"); postNum = true; } if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_ADDRESS))) { sb.append(","); sb.append("联系地址"); address = true; } if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_DESC))) { sb.append(","); sb.append("特别说明"); desc = true; } sb.append(",报名时间,状态"); sb.append(separator); User regUser = null; List<ActivityItem> list = activityItemService.queryActivityItem(activity.getTid(), null, true); for (ActivityItem item : list) { sb.append(item.getAuthor().getNickname()).append(","); if (username) { sb.append(item.getName()).append(","); sb.append(item.getUid()).append(","); regUser = userService.findUser(item.getUid()); if (regUser != null) { sb.append(regUser.getUsername()).append(","); } else { sb.append("").append(","); } } if (persons) { sb.append(item.getPersons()).append(","); } if (phone) { sb.append("\t" + item.getPhone() + "\t").append(","); } if (city) { sb.append(item.getCity()).append(","); } if (carNum) { sb.append(item.getCarNum()).append(","); } if (email) { sb.append(item.getEmail()).append(","); } if (postNum) { sb.append(item.getPostNum()).append(","); } if (address) { sb.append(item.getAddress()).append(","); } if (desc) { sb.append(item.getDescription().replace("\r","").replace("\n","").replaceAll("\"","”").replaceAll(",", " ")).append(","); } sb.append(sdf1.format(item.getCreateAt())).append(",") .append(item.getStatus() == SystemConstant.ACTIVITY_ITEM_NOT_CENSOR ? "尚未审核" : item.getStatus() == SystemConstant.ACTIVITY_ITEM_CENSOR_PASS ? "审核通过" : "被拒绝").append(separator);
关于上面的方法有几个说明:
1.后缀为csv的文件,可以用excel打开,来自百科的解析:逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样被解读的数据。CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗号或制表符。通常,所有记录都有完全相同的字段序列。
2.Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显示附加的文件。当 Internet Explorer 接收到头时,它会激活文件下载对话框,它的文件名框自动填充了头中指定的文件名。(请注意,这是设计导致的;无法使用此功能将文档保存到用户的计算机上,而不向用户询问保存位置。)