poi的jar下载地址:http://poi.apache.org/
下载后会有很多jar,但是如果只是简单的excel报表的话,导入一个poi-版本号-日期.jar就可以了。
导出代码:
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函数获取年月的
date_format(formatdate,‘%Y-%m‘)
最后提示一点是,在请求导出的时候,不能用ajax请求,会没有反映的。
时间: 2024-10-22 07:49:26