jasperreports制作报表(导出pdf excel html)

来源:互联网 发布:淘宝卖家注册流程 编辑:程序博客网 时间:2024/04/30 04:53

jasperreport生成报表的各种格式

关键代码:

[java] view plaincopy
  1. Connection conn = null;  
  2.      String type = request.getParameter("type");  
  3.      try  
  4.      {  
  5.           
  6.         Class.forName("oracle.jdbc.driver.OracleDriver");  
  7.         conn = DriverManager.getConnection(  
  8.   
  9.         "jdbc:oracle:thin:@127.0.0.1:1521:orcl","mutouyihao","xx");  
  10.   
  11.         ServletContext servletContext = this.getServletContext();  
  12.         File reportFile = new File(servletContext.getRealPath("/")+"/WEB-INF/report/Untitled_report_1.jasper");  
  13.         Map parameters = new HashMap();  
  14.         if("pdf".equals(type)){  
  15.            byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, conn);  
  16.            response.setContentType("application/pdf");  
  17.            response.addHeader("Content-Disposition""attachment; filename=report.pdf");  
  18.            response.setContentLength(bytes.length);  
  19.            ServletOutputStream ouputStream = response.getOutputStream();  
  20.            ouputStream.write(bytes, 0, bytes.length);  
  21.            ouputStream.flush();  
  22.            ouputStream.close();  
  23.         } else if ("excel".equals(type)){  
  24.            JRXlsExporter exporter = new JRXlsExporter();     
  25.            ByteArrayOutputStream oStream = new ByteArrayOutputStream();     
  26.            JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, conn);     
  27.            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);     
  28.            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);     
  29.            exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);     
  30.            exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);     
  31.            exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);     
  32.            exporter.exportReport();     
  33.                 
  34.            byte[] bytes = oStream.toByteArray();     
  35.            response.setContentType("application/vnd.ms-excel");   
  36.            response.addHeader("Content-Disposition""attachment; filename=report.xls");  
  37.            response.setContentLength(bytes.length);     
  38.            ServletOutputStream ouputStream = response.getOutputStream();     
  39.            ouputStream.write(bytes, 0, bytes.length);     
  40.            ouputStream.flush();     
  41.            ouputStream.close();     
  42.             
  43.         }  
  44.      }  
  45.      catch (JRException jre)  
  46.      {  
  47.         System.out.println("JRException:" + jre.getMessage());  
  48.      }  
  49.      catch (Exception e)  
  50.      {  
  51.         System.out.println("Exception:" + e.getMessage());  
  52.      }  
  53.      finally{  
  54.         try  
  55.         {  
  56.            conn.close();  
  57.         }  
  58.         catch (SQLException ex)  
  59.         {  
  60.            // TODO Auto-generated catch block  
  61.            ex.printStackTrace();  
  62.         }  
  63.      }  


生成html的参考

[java] view plaincopy
  1. //生成html     
  2.            JRHtmlExporter exporter = new JRHtmlExporter();     
  3.            ByteArrayOutputStream oStream = new ByteArrayOutputStream();     
  4.            JasperPrint jasperPrint = JasperFillManager.fillReport(rpt.getPath(), map, con);     
  5.            exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);     
  6.            exporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT, jasperPrint);     
  7.            exporter.setParameter(JRHtmlExporterParameter.CHARACTER_ENCODING, "utf-8");     
  8.            exporter.setParameter(JRHtmlExporterParameter.OUTPUT_STREAM, oStream);     
  9.            exporter.exportReport();     
  10.            byte[] bytes = oStream.toByteArray();     
  11.            response.setContentType("text/html");     
  12.            response.setContentLength(bytes.length);     
  13.            response.setCharacterEncoding("utf-8");     
  14.            ServletOutputStream ouputStream = response.getOutputStream();     
  15.            ouputStream.write(bytes, 0, bytes.length);     
  16.            ouputStream.flush();     
  17.            ouputStream.close();     
  18.            con.close();     
  19.            out.clear();     
  20.            out = pageContext.pushBody();     

0 0