List<CmsAction> list = actionMng.getList(contentId);
String[] tableHeader = {"姓名", "手机号"}; short cellNumber = (short) tableHeader.length;// 表的列数 HSSFWorkbook workbook = new HSSFWorkbook(); // 创建一个excel HSSFCell cell = null; // Excel的列 HSSFRow row = null; // Excel的行 HSSFCellStyle style = workbook.createCellStyle(); // 设置表头的类型 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCellStyle style1 = workbook.createCellStyle(); // 设置数据类型 style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFFont font = workbook.createFont(); // 设置字体 HSSFSheet sheet = workbook.createSheet("sheet1"); // 创建一个sheet HSSFHeader header = sheet.getHeader();// 设置sheet的头 try { if (list.size() < 1) { header.setCenter("查无资料"); } else { header.setCenter("报名表"); row = sheet.createRow(0); row.setHeight((short) 400); for (int k = 0; k < cellNumber; k++) { cell = row.createCell(k);// 创建第0行第k列 cell.setCellValue(tableHeader[k]);// 设置第0行第k列的值 sheet.setColumnWidth(k, 8000);// 设置列的宽度 font.setColor(HSSFFont.COLOR_NORMAL); // 设置单元格字体的颜色. font.setFontHeight((short) 350); // 设置单元字体高度 style1.setFont(font);// 设置字体风格 cell.setCellStyle(style1); } for (int i = 0; i < list.size(); i++) { CmsAction cmsAction = list.get(i); JSONObject jobject =new JSONObject(); jobject.put("name", cmsAction.getName()); jobject.put("phone", cmsAction.getPhone()); row = sheet.createRow((short) (i + 1));// 创建第i+1行 row.setHeight((short) 400);// 设置行高 if (cmsAction.getName() != null) { cell = row.createCell(0);// 创建第i+1行第0列 cell.setCellValue(cmsAction.getName());// 设置第i+1行第0列的值 cell.setCellStyle(style);// 设置风格 } if (cmsAction.getPhone() != null) { cell = row.createCell(1); // 创建第i+1行第1列 cell.setCellValue(cmsAction.getPhone());// 设置第i+1行第1列的值 cell.setCellStyle(style); // 设置风格 } } } } catch (Exception e) { e.printStackTrace(); } OutputStream out = null;// 创建一个输出流对象 try { out = response.getOutputStream();// response.setHeader("Content-disposition", "attachment; filename=" + "cmsAction.xls");// filename是下载的xls的名,建议最好用英文 response.setContentType("application/vnd.ms-excel;charset=UTF-8");// 设置类型 response.setHeader("Pragma", "No-cache");// 设置头 response.setHeader("Cache-Control", "no-cache");// 设置头 response.setDateHeader("Expires", 0);// 设置日期头 workbook.write(out); out.flush(); workbook.write(out); } catch (IOException e) { e.printStackTrace(); } finally { try {if (out != null) {
out.close(); }} catch (IOException e) {
e.printStackTrace(); }}
在HTML中
$('#js-export').click(function(){ window.location.href="/jeecmsv9/api/front/action/daochu?contentId=120"; });