这段时间一直在研究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();
-
- }
|