Jasper Reports Exporting Reports - JasperReports

How to transform the reports into other formats in JasperReports?

The reports can be transformed or exported into other formats like PDF, HTML, and XLS using Facade class net.sf.jasperreports.engine.JasperExportManager. The transformation of the JasperPrint object (.jrprint file) into different format is known as exporting.

The following code (JasperReportExport.java) demonstrates the exporting process of the JasperReport document. The JasperExportManager provides methods to export a report into PDF, HTML, and XML only. To export to the XLS format, the class net.sf.jasperreports.engine.export.JRXlsExporter is used. This code generates following three files −

  • sample_report.pdf
  • sample_report.html
  • sample_report.xls

How to export the JasperReports to Other Formats?

A report template is written. The contents of the JRXML file (C:\tools\jasperreports-5.0.1\test\jasper_report_template.jrxml) are as below −

Next, contents of the POJO file C:\tools\jasperreports-5.0.1\test\src\com\wisdomjobs\DataBean.java are as given below −

The contents of the file C:\tools\jasperreports-5.0.1\test\src\com\wisdomjobs\DataBeanList.java are as given below −

A main class file JasperReportFill.java, is written which gets the java bean collection from the class (DataBeanList) and passes it to the JasperReports engine, to fill the report template. Save it to the directory C:\tools\jasperreports-5.0.1\test\src\com\wisdomjobs.

The logic for exporting the jasper print file to pdf, html, xls format is included.

How to generate Reports in JasperReports?

The above files are compiled and executed using regular ANT build process. The build.xml file is as given below −

Go to the command prompt and then go to the directory C:\tools\jasperreports-5.0.1\test, where build.xml is placed. Finally, execute the command ant -Dmain-class=com.wisdomjobs.JasperReportFill. The output is as follows −

It is observed that three files - sample_report.pdf, sample_report.html, sample_report.xls are being generated in the C:\ directory.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JasperReports Topics