java ssh导出excel
这个有做过
今天用了springmvc的框架导出excel
其实大同小益
改下几个参数就行了。。
导出代码
/**springmvc_java导出excel的学习方法 * 设置表头 * * @param wb * @return * @throws Exception */ public static HSSFCellStyle stylecreateTitle(HSSFWorkbook wb, int layer) throws Exception { HSSFCellStyle style = wb.createCellStyle(); // style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // style.setBorderLeft(HSSFCellStyle.BORDER_THIN); // style.setBorderRight(HSSFCellStyle.BORDER_THIN); // style.setBorderTop(HSSFCellStyle.BORDER_THIN); if (layer == 1) { /** * 设置背景颜色 */ style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); /** * 设置字体 */ HSSFFont f = wb.createFont(); f.setFontName("宋体"); // 字体 f.setColor(HSSFColor.WHITE.index);// 字体颜色 f.setFontHeight((short) 300); // 字高 f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗 style.setFont(f); } else if (layer == 2) { style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);// 背景 style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFFont font = wb.createFont(); font.setFontName("宋体"); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗 style.setFont(font); } else if (layer == 3) { style.setFillForegroundColor(HSSFColor.WHITE.index);// 背景 style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); } else { } return style; }
/** springmvc_java导出excel的学习方法 * 导出excel 表格 * @author: lyq * @date: Jun 5, 2014 10:35:20 AM * @return */ @RequestMapping(value = "downExcel.php") public String downExcel(ModelMap model,HttpServletRequest request,HttpServletResponse response){ List<CrmManager> list= crmService.findCrmManagerList(null); try { //第一步,创建一个webbook,对应一个Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); //第二步,在webbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet("crm客户管理"); sheet.setDefaultColumnWidth((short) 15); // 设置每列默认宽度 sheet.setDefaultRowHeightInPoints(15); //样式 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.BORDER_HAIR); //创建一个居中格式 //第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short //标题 HSSFRow row = sheet.createRow((int)0);// 行和列索引都是从0开始 row.setHeightInPoints(35); HSSFCell cell = row.createCell((short) 0); cell.setCellStyle(stylecreateTitle(wb,1)); cell.setCellValue(new HSSFRichTextString("crm客户管理")); HSSFRow row2 = sheet.createRow((int)1);// 行和列索引都是从0开始 row2.setHeightInPoints(25); HSSFCell cell2 = row2.createCell((short) 0); cell2.setCellValue("客户ID"); cell2.setCellStyle(style); cell2.setCellStyle(stylecreateTitle(wb, 2)); cell2 = row2.createCell((short) 1); cell2.setCellValue("客户名"); cell2.setCellStyle(style); cell2.setCellStyle(stylecreateTitle(wb, 2)); cell2 = row2.createCell((short) 2); cell2.setCellValue("客户类型"); cell2.setCellStyle(style); cell2.setCellStyle(stylecreateTitle(wb, 2)); cell2 = row2.createCell((short) 3); cell2.setCellValue("客户电话"); cell2.setCellStyle(style); cell2.setCellStyle(stylecreateTitle(wb, 2)); cell2 = row2.createCell((short) 4); cell2.setCellValue("客户状态"); cell2.setCellStyle(style); cell2.setCellStyle(stylecreateTitle(wb, 2)); cell2 = row2.createCell((short) 5); cell2.setCellValue("创建时间"); cell2.setCellStyle(style); cell2.setCellStyle(stylecreateTitle(wb, 2)); cell2 = row2.createCell((short) 6); cell2.setCellValue("放出时间"); cell2.setCellStyle(style); cell2.setCellStyle(stylecreateTitle(wb, 2)); cell2 = row2.createCell((short) 7); cell2.setCellValue("跟入时间"); cell2.setCellStyle(style); cell2.setCellStyle(stylecreateTitle(wb, 2)); cell2 = row2.createCell((short) 8); cell2.setCellValue("跟入人"); cell2.setCellStyle(style); cell2.setCellStyle(stylecreateTitle(wb, 2)); cell2 = row2.createCell((short) 9); cell2.setCellValue("放出人"); cell2.setCellStyle(style); cell2.setCellStyle(stylecreateTitle(wb, 2)); int i =0; for(CrmManager p:list){ row = sheet.createRow((int)i+2); row.setHeightInPoints(20); // GamePayDetailEntity gs =gamePayDetailEntitys.get(i); //第四步,创建单元格,并设置值 row.createCell((short)0).setCellValue(p.getId()); row.createCell((short)1).setCellValue(p.getCrmName()); row.createCell((short)2).setCellValue(p.getCrmType());//p.getRefererSiteId().toString() row.createCell((short)3).setCellValue(p.getCrmPhone()); row.createCell((short)4).setCellValue(p.getCrmStatus()); row.createCell((short)5).setCellValue(p.getCrmCreatetime()); System.out.println(p.getCrmEndtime()); if(p.getCrmEndtime()!=null) row.createCell((short)6).setCellValue(p.getCrmEndtime().toString()); if(p.getCrmStarttime()!=null) row.createCell((short)7).setCellValue(p.getCrmStarttime().toString()); if(p.getCrmStartname()!=null) row.createCell((short)8).setCellValue(p.getCrmStartname().toString()); if(p.getCrmEndname()!=null) row.createCell((short)9).setCellValue(p.getCrmEndname().toString()); // cell = row.createCell((short)3); // cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu.getBrithday())); i++; } Region region =new Region(); region.setColumnFrom((short) 0); region.setColumnTo((short) 11); region.setRowFrom(0); region.setRowTo(0); sheet.addMergedRegion(region); response.reset(); response.setContentType("application/x-download;charset=GBK"); response.setHeader("Content-Disposition", "attachment;filename=USER_SITE_"+System.currentTimeMillis()+".xls"); wb.write(response.getOutputStream()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
java导出excel的学习方法 大概就是这样