当前位置:   article > 正文

Java导出Excel文件(多Sheet表头)_java excel 多页 标题

java excel 多页 标题
  1. /**
  2. * 导出-- 查看筛选条件下:查看当前活动抽奖名单
  3. * @param request
  4. * @param response
  5. * @throws Exception
  6. */
  7. @RequestMapping("/download_lottery_list")
  8. public synchronized void download_lottery_list(@RequestParam(required = false) Integer id,
  9. @RequestParam(required = false) String stationNo,
  10. @RequestParam(required = false) Integer isWin,
  11. HttpServletRequest request,
  12. HttpServletResponse response){
  13. HSSFWorkbook workbook = new HSSFWorkbook();
  14. String token = request.getParameter("REQUEST_USER_TOKEN");
  15. try {
  16. HSSFSheet sheet = workbook.createSheet("抽奖名单1");
  17. Wrapper<ActivityLottery> wrapper = new EntityWrapper<ActivityLottery>();
  18. wrapper.eq("activity_id",id);
  19. if(!StringUtils.isEmpty(stationNo)){
  20. wrapper.like("station_no",stationNo);
  21. }
  22. if(isWin!=null){
  23. wrapper.eq("is_win",isWin);
  24. }
  25. wrapper.orderBy("scan_time",false);
  26. //String :openId ;
  27. Map<String , ActivityUser> mapActivityUser = activityUserService.getActivityUserMap();
  28. //String :stationNo ;
  29. Map<String , ActivityStation> mapActivityStation = activityStationService.getActivityStationMap();
  30. List<ActivityLottery> activityLotteries = activityLotteryService.selectList(wrapper);
  31. if(StringUtils.isEmpty(token)) activityLotteries = new ArrayList<ActivityLottery>();
  32. if(!activityLotteries.isEmpty()){
  33. List<ActivityLotteryVo> activityLotteryVos = BeanUtil.copyListProperties(activityLotteries,ActivityLotteryVo::new);
  34. String fileName = "抽奖名单" + ".xls";//设置要导出的文件的名字
  35. String[] headers = { "是否中奖", "抽奖时间", "站主姓名", "站点编号", "站点地址","站主电话", "抽奖人姓名","手机号码","邮寄地址"};
  36. //headers表示excel表中第一行的表头
  37. HSSFRow row = sheet.createRow(0);
  38. //在excel表中添加表头
  39. for(int i=0;i<headers.length;i++){
  40. HSSFCell cell = row.createCell(i);
  41. HSSFRichTextString text = new HSSFRichTextString(headers[i]);
  42. cell.setCellValue(text);
  43. }
  44. int index = 2;//记录额外创建的sheet数量
  45. //在表中存放查询到的数据放入对应的列
  46. for(int i=0;i<activityLotteryVos.size();i++){
  47. HSSFRow row1 = sheet.createRow((i + 1) - ((index-2) * 50000));
  48. if ((i + 1) % 50000 == 0) {
  49. sheet = workbook.createSheet("抽奖名单" + index);
  50. row = sheet.createRow(0);
  51. //在excel表中添加表头
  52. for(int j=0;j<headers.length;j++){
  53. HSSFCell cell = row.createCell(j);
  54. HSSFRichTextString text = new HSSFRichTextString(headers[j]);
  55. cell.setCellValue(text);
  56. }
  57. index++;
  58. }
  59. ActivityUser activityUser = mapActivityUser.get(activityLotteryVos.get(i).getOpenId());
  60. ActivityStation activityStation = mapActivityStation.get(activityLotteryVos.get(i).getStationNo());
  61. if(activityUser!=null){
  62. activityLotteryVos.get(i).setName(activityUser.getName());
  63. activityLotteryVos.get(i).setUserAddress(activityUser.getAddress());
  64. activityLotteryVos.get(i).setPhone(activityUser.getPhone());
  65. }
  66. if(activityStation!=null){
  67. activityLotteryVos.get(i).setOwner(activityStation.getOwner());
  68. activityLotteryVos.get(i).setStationAddress(activityStation.getAddress());
  69. activityLotteryVos.get(i).setStationPhone(activityStation.getStationPhone());
  70. }
  71. row1.createCell(0).setCellValue(activityLotteryVos.get(i).getIsWin()==0?"否":"是");
  72. row1.createCell(1).setCellValue(DateUtils.formatTimesTampDate(activityLotteryVos.get(i).getScanTime()));
  73. row1.createCell(2).setCellValue(activityLotteryVos.get(i).getOwner());
  74. row1.createCell(3).setCellValue(activityLotteryVos.get(i).getStationNo());
  75. row1.createCell(4).setCellValue(activityLotteryVos.get(i).getStationAddress());
  76. row1.createCell(5).setCellValue(activityLotteryVos.get(i).getStationPhone());
  77. row1.createCell(6).setCellValue(activityLotteryVos.get(i).getName());
  78. row1.createCell(7).setCellValue(activityLotteryVos.get(i).getPhone());
  79. row1.createCell(8).setCellValue(activityLotteryVos.get(i).getUserAddress());
  80. }
  81. response.setContentType("application/octet-stream");
  82. fileName = URLEncoder.encode(fileName, "UTF-8");
  83. response.setHeader("Content-disposition", "attachment;filename=" + fileName);
  84. response.flushBuffer();
  85. workbook.write(response.getOutputStream());
  86. }else{
  87. Result result = new Result();
  88. try {
  89. result.setType(TypeEnum.FAIL.getCode());
  90. result.setTrue(false);
  91. result.setMessage("无权限");
  92. result.setCode(TypeEnum.noauthStatus.getCode());
  93. response.setStatus(403);
  94. response.setCharacterEncoding("UTF-8");
  95. response.setContentType("application/json; charset=utf-8");
  96. response.getWriter().print(JSONObject.toJSONString(result));
  97. }catch (Exception e){
  98. e.printStackTrace();
  99. }
  100. }
  101. }catch (Exception e){
  102. e.printStackTrace();
  103. }
  104. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/590617
推荐阅读
相关标签
  

闽ICP备14008679号