poi的jar下載地址:http://poi.apache.org/java
下載後會有不少jar,可是若是隻是簡單的excel報表的話,導入一個poi-版本號-日期.jar就能夠了。mysql
導出代碼:ajax
private void outputExcel(String queryDate, String[] headers, List<List<String>> diaochas, HttpServletRequest request, HttpServletResponse response) throws IOException { HSSFWorkbook workbook = new HSSFWorkbook(); //createSheet(excel工做表名) HSSFSheet sheet = workbook.createSheet(queryDate); //下面是設置excel表中標題的樣式 HSSFCellStyle title_style = workbook.createCellStyle(); title_style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); title_style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); title_style.setBorderBottom(HSSFCellStyle.BORDER_THIN); title_style.setBorderLeft(HSSFCellStyle.BORDER_THIN); title_style.setBorderRight(HSSFCellStyle.BORDER_THIN); title_style.setBorderTop(HSSFCellStyle.BORDER_THIN); title_style.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFFont title_font = workbook.createFont(); title_font.setColor(HSSFColor.VIOLET.index); title_font.setFontHeightInPoints((short) 12); title_font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); title_style.setFont(title_font); //內容的樣式 HSSFCellStyle content_style = workbook.createCellStyle(); content_style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); content_style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); content_style.setBorderBottom(HSSFCellStyle.BORDER_THIN); content_style.setBorderLeft(HSSFCellStyle.BORDER_THIN); content_style.setBorderRight(HSSFCellStyle.BORDER_THIN); content_style.setBorderTop(HSSFCellStyle.BORDER_THIN); content_style.setAlignment(HSSFCellStyle.ALIGN_CENTER); content_style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); HSSFFont content_font = workbook.createFont(); content_font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); content_style.setFont(content_font); //填充標題內容 HSSFRow row = sheet.createRow(0); for (int i = 0; i < headers.length; i++) { //設置標題的寬度自適應 sheet.setColumnWidth(i, headers[i].getBytes().length * 2 * 256); HSSFCell cell = row.createCell(i); cell.setCellStyle(title_style); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } //填充內容 囧。。。偷懶沒有創建對象,直接用List存放的數據。 for (int i = 0; i < diaochas.size(); i++) { row = sheet.createRow(i + 1); List<String> diaocha = diaochas.get(i); for (int j = 0; j < diaocha.size(); j++) { HSSFCell cell = row.createCell(j); cell.setCellStyle(content_style); HSSFRichTextString richString = new HSSFRichTextString( diaocha.get(j)); cell.setCellValue(richString); } } //這裏調用reset()由於我在別的代碼中調用了response.getWriter(); response.reset(); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=" + queryDate + ".xls"); OutputStream ouputStream = response.getOutputStream(); workbook.write(ouputStream); ouputStream.flush(); ouputStream.close(); }
嗯 附加一個mysql函數獲取年月的
sql
date_format(formatdate,'%Y-%m')
最後提示一點是,在請求導出的時候,不能用ajax請求,會沒有反映的。
apache