Dynamic report implementation for all types of formats
Maven depeendecies will be included below
<dependency> <groupId>net.sourceforge.dynamicreports</groupId> <artifactId>dynamicreports-core</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>org.eclipse.birt.runtime.3_7_1</groupId> <artifactId>com.lowagie.text</artifactId> <version>2.1.7</version> </dependency>
XHTML Code:
<h:commandLink id="csv" onclick="PF('data').hide();" action="#{dashboardInfoBean.downloadCsv}"> <h:graphicImage name="images/img_trans.gif" styleClass="ico csvImg" /> </h:commandLink> <h:commandLink id="pdf" onclick="PF('data').hide();" action="#{dashboardInfoBean.downloadPdf}"> <h:graphicImage name="images/img_trans.gif" styleClass="ico pdfImg" /> </h:commandLink> <h:commandLink id="excel" onclick="PF('data').hide();" action="#{dashboardInfoBean.downloadExcel}"> <h:graphicImage name="images/img_trans.gif" styleClass="ico xlsImg" /> </h:commandLink> <h:commandLink id="xml" onclick="PF('data').hide();" action="#{dashboardInfoBean.downloadXml}"> <h:graphicImage name="images/img_trans.gif" styleClass="ico xmlImg" /> </h:commandLink> <h:commandLink id="jasper" onclick="PF('data').hide();" action="#{dashboardInfoBean.downloadJasperReport}"> <h:graphicImage name="images/img_trans.gif" styleClass="ico xmlImg" /> </h:commandLink>
Java Code:
//set datasource for creating the report report.setDataSource(dataSource); JasperPrint jasperPrint = report.toJasperPrint(); HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse(); ServletOutputStream servletOutputStream = response.getOutputStream(); if(downloadFormat.equalsIgnoreCase("PDF")){ response.setContentType("application/pdf"); response.addHeader("Content-disposition", "attachment; filename=report.pdf"); JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream); } else if(downloadFormat.equalsIgnoreCase("XML")){ //response.setContentType("application/pdf"); response.addHeader("Content-disposition", "attachment; filename=report.xml"); JasperExportManager.exportReportToXmlStream(jasperPrint, servletOutputStream); } else if(downloadFormat.equalsIgnoreCase("CSV")){ response.setContentType("text/plain"); response.addHeader("Content-disposition", "attachment; filename=report.csv"); JRCsvExporter exporter = new JRCsvExporter(); exporter.setParameter(JRCsvExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRCsvExporterParameter.OUTPUT_STREAM, servletOutputStream); exporter.setParameter(JRExporterParameter.IGNORE_PAGE_MARGINS, Boolean.TRUE); exporter.exportReport(); } else if(downloadFormat.equalsIgnoreCase("XLS")){ response.setContentType("application/vnd.ms-excel"); response.addHeader("Content-disposition", "attachment; filename=report.xls"); JExcelApiExporter exporterXLS = new JExcelApiExporter(); exporterXLS.setParameter( JRXlsExporterParameter.JASPER_PRINT, jasperPrint); exporterXLS.setParameter( JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); exporterXLS.setParameter( JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); exporterXLS.setParameter( JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); exporterXLS.setParameter( JRXlsExporterParameter .IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, Boolean.TRUE); // exporterXLS.setParameter( // JRXlsExporterParameter.IS_IGNORE_CELL_BORDER, // Boolean.TRUE); exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, servletOutputStream); exporterXLS.exportReport(); } FacesContext.getCurrentInstance().responseComplete();
source share