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

本文介绍了如何在Spring MVC中结合ireport,利用javabean作为数据源来生成报表。通过将list集合封装到JRBeanCollectionDataSource,然后使用JRHtmlExporter展示给用户,可以实现报表的HTML格式输出。同时,也提到了生成Excel和PDF格式的解决方案。
摘要由CSDN通过智能技术生成

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

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

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

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

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


 

输出为execl表格格式

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


 

设置输出为Pdf格式的文件

@RequestMapping("/customer/reportpdf.do")

         publicvoid exportpdf(HttpServletRequest request,HttpServletResponse response) throwsJRException, IOException{

                   Stringctxpath = request.getSession().getServletContext()

                                     .getRealPath("/report/beandata.jasper");              

                   List<HKCustomer>listInfo = this.hkCustomerService.listAllCustomers();

                   FilereFile = new File(ctxpath);

                   Mapparameters = new HashMap();                

                   JRBeanCollectionDataSourceds = new JRBeanCollectionDataSource(listInfo);

                   JasperPrintjasperPrint = JasperFillManager.fillReport(

                                     reFile.getPath(),parameters, ds);           

                   JRPdfExporterexporter = new JRPdfExporter();     

                   exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT,jasperPrint);

                   exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM,response.getOutputStream());

                   response.setHeader("Content-Disposition","attachment;filename=customer.pdf");

                   response.setContentType("application/pdf");

                   response.setCharacterEncoding("utf-8");

                   exporter.exportReport();

         }


 

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值