Spring MVC结合ireport采用javabean作为数据源的实现
这段时间一直在研究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(); }
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30