/** * 导出日志excel表 **/ @SuppressWarnings("deprecation") @RequestMapping(value = "down") public void download(HttpServletRequest request, HttpServletResponse response,String excelTitle,String excelType,String excelTargetPerson) { response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); String fileName="消息日志表-"+DateUtil.formatDate(new Date())+".xls"; fileName=encodeFilename(fileName, request); response.setHeader("Content-disposition", "attachment; filename=" + fileName); // 第一步,创建一个webbook,对应一个Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet("日志报表"); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制int HSSFRow row = sheet.createRow((int) 0); // 第四步,创建单元格,并设置值表头 设置表头居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 HSSFCell cell = row.createCell((int) 0); cell.setCellValue("系统编码"); cell.setCellStyle(style); cell = row.createCell((int) 1); cell.setCellValue("消息标题"); cell.setCellStyle(style); cell = row.createCell((int) 2); cell.setCellValue("消息摘要"); cell.setCellStyle(style); cell = row.createCell((int) 3); cell.setCellValue("消息接收时间"); cell.setCellStyle(style); cell = row.createCell((int) 4); cell.setCellValue("消息发送时间"); cell.setCellStyle(style); cell = row.createCell((int) 5); cell.setCellValue("消息类型"); cell.setCellStyle(style); cell = row.createCell((int) 6); cell.setCellValue("消息类型(第三方系统类型)"); cell.setCellStyle(style); cell = row.createCell((int) 7); cell.setCellValue("目标人员账户"); cell.setCellStyle(style); cell = row.createCell((int) 8); cell.setCellValue("日志类型"); cell.setCellStyle(style); cell = row.createCell((int) 9); cell.setCellValue("日志详细"); cell.setCellStyle(style); // 第五步,写入实体数据 实际应用中这些数据从数据库得到, @SuppressWarnings("unchecked") List<THmaiSendLog> list = wxLogService.getExcelList(excelTitle, excelTargetPerson, excelType); for (int i = 0; i < list.size(); i++) { row = sheet.createRow((int) i + 1); THmaiSendLog log = list.get(i); // 第四步,创建单元格,并设置值 row.createCell((int) 0).setCellValue(log.getSystemCode()); row.createCell((int) 1).setCellValue(log.getMsgTitle()); row.createCell((int) 2).setCellValue(log.getMsgSummary()); row.createCell((int) 3).setCellValue(log.getUpdateTime()); row.createCell((int) 4).setCellValue(log.getCreateTime()); row.createCell((int) 5).setCellValue(log.getMsgType()==null?0:log.getMsgType()); row.createCell((int) 6).setCellValue(log.getMsgCategoryCode()); row.createCell((int) 7).setCellValue(log.getTargetPersonAccount()); row.createCell((int) 8).setCellValue(log.getLogType()); row.createCell((int) 0).setCellValue(log.getLogDetail()); } // 第六步,将文件存到指定位置 try { wb.write(response.getOutputStream()); } catch (Exception e) { LOGGER.error("{}", e); } finally { try { response.getOutputStream().close(); } catch (IOException e) { LOGGER.error("{}", e); } } }
/** * 设置下载文件中文件的名称 * * @param filename * @param request * @return */ public static String encodeFilename(String filename, HttpServletRequest request) { /** * 获取客户端浏览器和操作系统信息 * 在IE浏览器中得到的是:User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; Alexa Toolbar) * 在Firefox中得到的是:User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.7.10) Gecko/20050717 Firefox/1.0.6 */ String agent = request.getHeader("USER-AGENT"); try { if ((agent != null) && (-1 != agent.indexOf("MSIE"))) { String newFileName = URLEncoder.encode(filename, "UTF-8"); newFileName = StringUtils.replace(newFileName, "+", "%20"); if (newFileName.length() > 150) { newFileName = new String(filename.getBytes("GB2312"), "ISO8859-1"); newFileName = StringUtils.replace(newFileName, " ", "%20"); } return newFileName; } if ((agent != null) && (-1 != agent.indexOf("Mozilla"))) { return MimeUtility.encodeText(filename, "UTF-8", "B"); } return filename; } catch (Exception ex) { return filename; } }