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

本文介绍了如何利用jasperreports库来生成包括PDF、Excel和HTML在内的各种格式报表,提供了关键代码示例,特别是针对HTML格式的生成。

jasperreport生成报表的各种格式

关键代码:

 Connection conn = null;
      String type = request.getParameter("type");
      try
      {
         
         Class.forName("oracle.jdbc.driver.OracleDriver");
         conn = DriverManager.getConnection(

         "jdbc:oracle:thin:@127.0.0.1:1521:orcl","mutouyihao","xx");

         ServletContext servletContext = this.getServletContext();
         File reportFile = new File(servletContext.getRealPath("/")+"/WEB-INF/report/Untitled_report_1.jasper");
         Map parameters = new HashMap();
         if("pdf".equals(type)){
            byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, conn);
            response.setContentType("application/pdf");
            response.addHeader("Content-Disposition", "attachment; filename=report.pdf");
            response.setContentLength(bytes.length);
            ServletOutputStream ouputStream = response.getOutputStream();
            ouputStream.write(bytes, 0, bytes.length);
            ouputStream.flush();
            ouputStream.close();
         } else if ("excel".equals(type)){
            JRXlsExporter exporter = new JRXlsExporter();   
            ByteArrayOutputStream oStream = new ByteArrayOutputStream();   
            JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, conn);   
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);   
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);   
            exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);   
            exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);   
            exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);   
            exporter.exportReport();   
               
            byte[] bytes = oStream.toByteArray();   
            response.setContentType("application/vnd.ms-excel"); 
            response.addHeader("Content-Disposition", "attachment; filename=report.xls");
            response.setContentLength(bytes.length);   
            ServletOutputStream ouputStream = response.getOutputStream();   
            ouputStream.write(bytes, 0, bytes.length);   
            ouputStream.flush();   
            ouputStream.close();   
           
         }
      }
      catch (JRException jre)
      {
         System.out.println("JRException:" + jre.getMessage());
      }
      catch (Exception e)
      {
         System.out.println("Exception:" + e.getMessage());
      }
      finally{
         try
         {
            conn.close();
         }
         catch (SQLException ex)
         {
            // TODO Auto-generated catch block
            ex.printStackTrace();
         }
      }


生成html的参考

 //生成html   
            JRHtmlExporter exporter = new JRHtmlExporter();   
            ByteArrayOutputStream oStream = new ByteArrayOutputStream();   
            JasperPrint jasperPrint = JasperFillManager.fillReport(rpt.getPath(), map, con);   
            exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);   
            exporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT, jasperPrint);   
            exporter.setParameter(JRHtmlExporterParameter.CHARACTER_ENCODING, "utf-8");   
            exporter.setParameter(JRHtmlExporterParameter.OUTPUT_STREAM, oStream);   
            exporter.exportReport();   
            byte[] bytes = oStream.toByteArray();   
            response.setContentType("text/html");   
            response.setContentLength(bytes.length);   
            response.setCharacterEncoding("utf-8");   
            ServletOutputStream ouputStream = response.getOutputStream();   
            ouputStream.write(bytes, 0, bytes.length);   
            ouputStream.flush();   
            ouputStream.close();   
            con.close();   
            out.clear();   
            out = pageContext.pushBody();   


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值