最近公司的需求,要做一个excel导出功能,在网上看了很多教程都搞得很复杂,我直接上干货! 下面的方法就是直接导出excel的方法,可以直接用,其中evenList是数据源,res是response,type非必须项 public void downLoadTemplate(List eventList, HttpServletResponse res,String type) { try { // 第一步,创建一个webbook,对应一个Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet("sheet1"); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short HSSFRow row = sheet.createRow((int) 0); // 第四步,创建单元格,并设置值表头 设置表头居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCell cell = row.createCell(0); HashMap map= (HashMap) eventList.get(0); List alertNmaelist= (List) map.get("listAlertName"); cell.setCellValue("日期"); cell.setCellStyle(style); for(int i=0;i<alertNmaelist.size();i++){ cell = row.createCell(i+1); cell.setCellValue((String) alertNmaelist.get(i)); cell.setCellStyle(style); } // cell.setCellValue("日期"); // cell.setCellStyle(style); // cell = row.createCell(1); // cell.setCellValue("事件总数"); // cell.setCellStyle(style); // cell = row.createCell(2); // cell.setCellValue("通过数量"); // cell.setCellStyle(style); // cell = row.createCell(3); // cell.setCellValue("规则触发总数"); // cell.setCellStyle(style); // cell = row.createCell(4); // cell.setCellValue("规则触发占比"); // cell.setCellStyle(style); // cell = row.createCell(5); // cell.setCellValue("触发的规则名称"); // cell.setCellStyle(style); // cell = row.createCell(6); // cell.setCellValue("该规则触发次数"); // cell.setCellStyle(style); // cell = row.createCell(7); // cell.setCellValue("该规则触发占比"); // cell.setCellStyle(style); // cell = row.createCell(8); // cell.setCellValue("报警名称"); // cell.setCellStyle(style); // cell = row.createCell(9); // cell.setCellValue("发布模式"); // cell.setCellStyle(style); // 写入文件 List chartData= (List) map.get("chartData"); List chartTime= (List) map.get("timeResult"); if(chartData.size()>0){ int j=0; for(int z=0;z<chartTime.size();z++){ row = sheet.createRow(z+1); for(int i=0;i<chartData.size();i++){ HashMap map1=new HashMap(); map1= (HashMap) chartData.get(i); List list2=new ArrayList(); list2= (List) map1.get("data"); row.createCell(0).setCellValue((String) chartTime.get(z)); row.createCell(i+1).setCellValue((Integer)list2.get(z)); } } } String filePre=null; if("area".equals(type)){ filePre="表名1"; }else{ filePre="表名2"; } String fileName = new StringBuilder().append(new String(filePre.getBytes("gb2312"), "iso8859-1")).append(".xls").toString(); // 文件名和格式 OutputStream os = null; // 输出流定义 ServletOutputStream out2=null; try { out2=res.getOutputStream(); res.reset(); res.setHeader("Content-Disposition", "attachment; filename=" + fileName); res.setContentType("application/octet-stream; charset=utf-8"); wb.write(out2); out2.flush(); } catch (IOException e) { // e.printStackTrace("自定义数据集下载模版失败!"); e.printStackTrace(); } finally { if (out2 != null) { try { out2.close(); } catch (IOException e) { e.printStackTrace(); } } } } catch (Exception e) { e.printStackTrace(); } }
总结
// 第一步,创建一个webbook,对应一个Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet("sheet1"); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short HSSFRow row = sheet.createRow((int) 0); // 第四步,创建单元格,并设置值表头 设置表头居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
然后给row设置值
HSSFCell cell = row.createCell(0);<<<<<<这句话就是创建第*行的第一个格子内容
OK了,这就是主要的过程,然后把这个设置进输出流就OK。
如果喜欢给我点个赞噢