/**返回值类型为Map ---List<Map<String, Object>> logList = logService.queryLogList(null);
返回值类型为对象 ---List<Guidance> guidanceList = guidanceService.queryGuidanceAndResourceList(null); * * @Title: excelLog * @Description: TODO(登陆日志导出excel) * @author: ZHOUPAN * @date: 2018年5月10日 下午3:18:47 * @param @param response 设定文件 * @return void 返回类型 * @throws */ @SuppressWarnings({ "resource", "deprecation" }) @ResponseBody @RequestMapping(value = "/log", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" }) public void excelLog(HttpServletResponse response) { // 创建HSSFWorkbook对象(excel的文档对象) HSSFWorkbook wb = new HSSFWorkbook(); // 建立新的sheet对象(excel的表单) HSSFSheet sheet = wb.createSheet("登陆日志表"); // 设置列宽 sheet.setColumnWidth((short) 0, (short) 35 * 200); sheet.setColumnWidth((short) 1, (short) 35 * 200); sheet.setColumnWidth((short) 2, (short) 35 * 300); sheet.setColumnWidth((short) 3, (short) 35 * 200); sheet.setColumnWidth((short) 4, (short) 35 * 200); // 设置样式 // 头 HSSFCellStyle cellStyle_head = style(0, wb); // 导出时间 HSSFCellStyle cellStyle_export = style(3, wb); // 标题 HSSFCellStyle cellStyle_title = style(1, wb); // 正文 HSSFCellStyle cellStyle = style(2, wb); HSSFCellStyle cellStyle_1 = style(4, wb); // 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列 CellRangeAddress c1 = new CellRangeAddress(0, 0, 0, 4); sheet.addMergedRegion(c1); CellRangeAddress c2 = new CellRangeAddress(1, 1, 0, 4); sheet.addMergedRegion(c2); // 在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个 HSSFRow row0 = sheet.createRow(0); // 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个 HSSFCell cell = row0.createCell(0); // 设置单元格内容 标题 cell.setCellValue("上海明华电力———登陆日志一览表"); cell.setCellStyle(cellStyle_head); // 设置合并单元格边框 setRegionStyle(sheet, c1, cellStyle_head); setRegionStyle(sheet, c2, cellStyle_export); // 在sheet里创建第二行 HSSFRow row1 = sheet.createRow(1); // 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个 HSSFCell cell1 = row1.createCell(0); // 设置单元格内容 标题 cell1.setCellValue("导出时间:" + DateUtils.getCurrentTime("yyyy-MM-dd")); cell1.setCellStyle(cellStyle_export); // 在sheet里创建第三行 HSSFRow row2 = sheet.createRow(2); // 创建单元格并设置单元格内容 row2.createCell(0).setCellValue("用户名"); row2.createCell(1).setCellValue("角色"); row2.createCell(2).setCellValue("IP"); row2.createCell(3).setCellValue("行为"); row2.createCell(4).setCellValue("操作时间"); row2.getCell(0).setCellStyle(cellStyle_title); row2.getCell(1).setCellStyle(cellStyle_title); row2.getCell(2).setCellStyle(cellStyle_title); row2.getCell(3).setCellStyle(cellStyle_title); row2.getCell(4).setCellStyle(cellStyle_title); List<Map<String, Object>> logList = logService.queryLogList(null); if (logList != null) { for (int i = 0; i < logList.size(); i++) { Map<String, Object> log = logList.get(i); // 在sheet里创建第三行 HSSFRow row3 = sheet.createRow(i + 3); //{LOG_TEXT=登录, IP=192.168.1.76, USER_ID=1, ROLE_ID=1, LOG_TYPE=1, ID=4618, USER_NAME=admin, ROLE_NAME=超级管理员, CREATETIME=2018-05-09 11:51:24.0} row3.createCell(0).setCellValue(log.get("USER_NAME").toString()); row3.createCell(1).setCellValue(log.get("ROLE_NAME").toString()); row3.createCell(2).setCellValue(log.get("IP").toString()); row3.createCell(3).setCellValue(log.get("LOG_TEXT").toString()); row3.createCell(4).setCellValue(log.get("CREATETIME").toString()); row3.getCell(0).setCellStyle(cellStyle); row3.getCell(1).setCellStyle(cellStyle_1); row3.getCell(2).setCellStyle(cellStyle); row3.getCell(3).setCellStyle(cellStyle_1); row3.getCell(4).setCellStyle(cellStyle); } } // 输出Excel文件 OutputStream output = null; try { output = response.getOutputStream(); response.reset(); response.setHeader("Content-disposition", "attachment; filename=" + new String("登陆日志".getBytes("gbk"), "iso8859-1") + ".xls"); response.setContentType("application/msexcel"); wb.write(output); } catch (IOException e) { e.printStackTrace(); } finally { try { if (output != null) { output.close(); } } catch (IOException e) { e.printStackTrace(); } } }