java导出excel
需要jar包:poi-3.7.jar,poi-ooxml-3.7.jar,poi-ooxm-schemas-3.7.jar
java类代码
public void loadExpressExcel(){ List<Map<String, Object>> expressList = new ArrayList<Map<String,Object>>(); expressList = 从数据库获取数据; HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(System.currentTimeMillis()+""); sheet.setDefaultColumnWidth(16); sheet.setColumnWidth(0, 3000);//设置单元格宽带 sheet.setColumnWidth(9, 16000); //生成一种单元格样式 HSSFCellStyle style = workbook.createCellStyle(); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成另一个字体 HSSFFont font = workbook.createFont(); font.setBoldweight((short) 800); // 把字体应用到当前的样式 style.setFont(font); HSSFCellStyle style2 = workbook.createCellStyle(); style2.setWrapText(true); style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); style2.setBorderRight(HSSFCellStyle.BORDER_THIN); style2.setBorderTop(HSSFCellStyle.BORDER_THIN); style2.setAlignment(HSSFCellStyle.ALIGN_LEFT); style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成另一个字体 HSSFFont font2 = workbook.createFont(); font2.setBoldweight((short) 400); // 把字体应用到当前的样式 style2.setFont(font2); int rowNum = 0; HSSFRow row = sheet.createRow(rowNum); row.setHeight((short) 400); // 产生表格标题行 String[] headers = {"序号","名字",“年级","班级","学号"}; for (int i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellStyle(style); cell.setCellValue(text); } int num = 0; int successNum = 0; for (int i = 0; i < expressList.size(); i++) { String name = (String) expressList.get(i).get("name"); String grade = (String) expressList.get(i).get("grade"); String class = (String) expressList.get(i).get("class"); String num = (String) expressList.get(i).get("num"); rowNum++; row = sheet.createRow(rowNum); row.setHeight((short) 400); HSSFCell cell = row.createCell(0); cell.setCellStyle(style2); cell.setCellValue(i+1);//序号 HSSFCell cell1 = row.createCell(1); cell1.setCellStyle(style2); cell1.setCellValue(name); HSSFCell cell2 = row.createCell(2); cell2.setCellStyle(style2); cell2.setCellValue(grade); HSSFCell cell3 = row.createCell(3); cell3.setCellStyle(style2); cell3.setCellValue(class); HSSFCell cell4 = row.createCell(4); cell4.setCellStyle(style2); cell4.setCellValue(num); num++; int a = 0; SimpleDateFormat expFormat = new SimpleDateFormat("yyyy-MM-dd HH:00:00"); String expDate = expFormat.format(new Date()); try { a = //修改单条数据的状态,和导出时间,限制在小时,导出时点击取消,数据内状态依然会更改,根据时间恢复,重新导出 } catch (Exception e) { } if(a == 1){ successNum++; } } //提供下载 HttpServletResponse response = Result.getServletResponse(); response.reset(); response.setContentType("application/x-download;charset=GBK"); SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); response.setHeader("Content-Disposition", "attachment;filename="+format.format(new Date())+".xls"); try { workbook.write(response.getOutputStream()); } catch (Exception e) { } }
js代码
function loadExpExcel(){ var url = Global.contextPath + "/xxx/xxxx.do";控制层方法 openNewWin({ url : url, target : "_self" }); }
jsp代码
<a class="btn" href="javascript:void(0);" onclick="loadExpExcel()"><i class="icon-play"></i>下载xxx</a>