分享

Spring MVC结合ireport采用javabean作为数据源的实现

 风_宇星 2015-04-07

这段时间一直在研究ireport如何和springmvc结合来实现报表的显示

网上查了很多资料,从各位前辈们的经验下结合自己的心得总结了SpringMVC结合ireport采用javabean作为数据源的实现方式,并总结代码如下:

Ireport采用javabean作为数据源实现数据的载入,对于java中,使用得到的list集合封装到JRBeanCollectionDataSource中来得到数据。最后通过JRHtmlExporter将结果展现给用户。

输出为html格式的设计方案:

  1. @RequestMapping("/customer/reporthtml.do")  
  2.     public void report(HttpServletRequest request, HttpServletResponse response)  
  3.             throws IOException, JRException {  
  4.         String ctxpath = request.getSession().getServletContext()  
  5.                 .getRealPath("/report/beandata.jasper");          
  6.         List<HKCustomer> listInfo = this.hkCustomerService.listAllCustomers();  
  7.         File reFile = new File(ctxpath);  
  8.         Map parameters = new HashMap();//根据变量来查询      
  9.         JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(listInfo);  
  10.         JasperPrint jasperPrint = JasperFillManager.fillReport(  
  11.                 reFile.getPath(), parameters, ds);  
  12.           
  13.         JRHtmlExporter exporter = new JRHtmlExporter();       
  14.         //输出为html格式的报表文件  
  15.         exporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT, jasperPrint);  
  16.         exporter.setParameter(JRHtmlExporterParameter.OUTPUT_STREAM,response.getOutputStream());       exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE);  
  17.       //移除设计报表中的空白行 exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);  
  18.        exporter.setParameter(JRHtmlExporterParameter.CHARACTER_ENCODING, "utf-8");  
  19.           exporter.exportReport();  
  20.     }  


 

输出为execl表格格式

  1. @RequestMapping("/customer/reportxls.do")  
  2.     public void exportxls(HttpServletRequest request,  
  3.             HttpServletResponse response) throws IOException, JRException {  
  4.         String ctxpath = request.getSession().getServletContext()  
  5.                 .getRealPath("/report/beandata.jasper");          
  6.         List<HKCustomer> listInfo = this.hkCustomerService.listAllCustomers();  
  7.         File reFile = new File(ctxpath);  
  8.         Map parameters = new HashMap();       
  9.         JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(listInfo);  
  10.         JasperPrint jasperPrint = JasperFillManager.fillReport(  
  11.                 reFile.getPath(), parameters, ds);    
  12.         JRXlsExporter exporter=new JRXlsExporter();       
  13.         exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);  
  14.         exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, response.getOutputStream());  
  15.         exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);   
  1. exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);  
  2. /设置输出的格式  
  3. response.setHeader("Content-Disposition", "attachment;filename=customer.xls");  
  4. response.setContentType("application/vnd_ms-excel");  
  5. exporter.exportReport();  


 

设置输出为Pdf格式的文件

  1. @RequestMapping("/customer/reportpdf.do")  
  2.   
  3.          publicvoid exportpdf(HttpServletRequest request,HttpServletResponse response) throwsJRException, IOException{  
  4.   
  5.                    Stringctxpath = request.getSession().getServletContext()  
  6.   
  7.                                      .getRealPath("/report/beandata.jasper");                
  8.   
  9.                    List<HKCustomer>listInfo = this.hkCustomerService.listAllCustomers();  
  10.   
  11.                    FilereFile = new File(ctxpath);  
  12.   
  13.                    Mapparameters = new HashMap();                  
  14.   
  15.                    JRBeanCollectionDataSourceds = new JRBeanCollectionDataSource(listInfo);  
  16.   
  17.                    JasperPrintjasperPrint = JasperFillManager.fillReport(  
  18.   
  19.                                      reFile.getPath(),parameters, ds);             
  20.   
  21.                    JRPdfExporterexporter = new JRPdfExporter();       
  22.   
  23.                    exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT,jasperPrint);  
  24.   
  25.                    exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM,response.getOutputStream());  
  26.   
  27.                    response.setHeader("Content-Disposition","attachment;filename=customer.pdf");  
  28.   
  29.                    response.setContentType("application/pdf");  
  30.   
  31.                    response.setCharacterEncoding("utf-8");  
  32.   
  33.                    exporter.exportReport();  
  34.   
  35.          }  


 

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多