web中应用jasperreport

http://923080512.iteye.com/blog/1396484

在web中使用jasperreport 要导入的包:
           jasperreports-4.5.0.jar
           js_activation-1.1.jar
           iText-2.1.7.jar
           iTextAsian.jar
           groovy-all-1.7.5.jar
           commons-beanutils-1.8.2.jar
           commons-digester-1.7.jar
           commons-collections-3.2.1.jar
           poi-3.7-20101029.jar
代码:
   方法1:
        String filename = ServletActionContext.getServletContext(). 
                           getRealPath("/ireport/report5_Javabean.jasper"); 
        Map<String,Object> map = new HashMap<String,Object>(); 
        map.put("SQLSTR", "select * from syz"); 
        Collection<Syz> collection = service.findAll(); 
        JRDataSource datasource = new JRBeanCollectionDataSource(collection); 
        ServletOutputStream out = response.getOutputStream(); 
        //加载jasper文件,生成JasperReport实例 ; 
        JasperReport report = (JasperReport) JRLoader.loadObject(filename); 
        //填充报表,生成JasperPrint实例 
        JasperPrint print = JasperFillManager.fillReport(report,map,datasource); 
        //JRXlsExporter是excel导出器,JRPdfExporter 是pdf导出器,其他类型以此类推 
        JRAbstractExporter exporter = new JRXlsExporter(); 
        //导出器设置参数 
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, print); 
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out); 
        //导出 
        exporter.exportReport(); 


   方法2:
        byte[] b = null ; 
        response.setContentType("application/pdf"); 
        response.setHeader("Content-Disposition","filename="+ 
                 java.net.URLEncoder.encode("Syz_PDF"+new SimpleDateFormat("yyyyMMddHHmmssSS").format(new Date()), "UTF-8")  
    + ".PDF"); 
     //转换成pdf,如果是html,转换后直接response.sendRedirect()	
        b = JasperRunManager.runReportToPdf(filename, map, datasource); 
        out.write(b); 
        out.flush(); 
        out.close(); 

你可能感兴趣的:(jasperReport)