Monday, February 19, 2007

JasperReports

JasperReport API:

It is a popular open source Java engine which is used for reporting output from many data sources in web and desktop applications .

JasperReports' reports are defined in XML files with an “jrxml” extension which contains of jasperReport , title , pageHeader , detail , pageFooter, band tag element , all of the elements are optional, except for the root jasperReport element.

A jrxml file needs to be compiled , this can be achieved by calling the compileReport() method on the net.sf.jasperreports.engine.JasperCompileManager class .

it needs to be filled with data, this is achieved by calling the fillReport() method on the net.sf.jasperreports.engine.JasperFillManager class which has three parameters an instance of net.sf.jasperreports.engine.JasperReport, a java.util.HashMap containing any parameters passed to the report, and an instance of a java.sql.Connection class .

finally it can be exported to a pdf/Html/xml file , this is achieved by Using :

net.sf.jasperreports.engine.JasperExportManager class and calling exportReportToPdfStream method which create and view the report in output stream 'view in browser' “used with web application” or calling exportReportToPdf method which create the file on the machine without viewing it .
OR
net.sf.jasperreports.view.JasperViewer class and calling viewReport method which create and open the Pdf file .

IReport :


It is used for building Jrxml file better than building it manually .This tool allows users to visually edit complex reports with charts, images, and subreports. iReport is integrated with leading open source chart libraries for java.

Sample code


After creating the jrxml file by Ireport or manually .

// 1- put parameter “examId”
Map parameters = new HashMap();
long examId ;
parameters.put("examId", String.valueOf(examId));

// 2- load and compile report
String jasperJRXMLFileName = “D:/jasperReport.jrxml” ;
JasperDesign jasperDesign = JRXmlLoader.load(jasperJRXMLFileName);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

// 3- DB connection
Connection conn = ResourceManager.getConnection();

// 4- fill report
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,parameters,conn);

// 5- create/viewing PDF file .
JasperExportManager.exportReportToPdfStream(jasperPrint,servletResponse.getOutputStream());
servletResponse.getOutputStream().flush();
servletResponse.flushBuffer();
OR
JasperExportManager.exportReportToPdfFile(jasperPrint, "reports/simple_report.pdf");
OR
JasperViewer.viewReport(jasperPrint);

JasperReport Requirements :
http://www.jasperforge.org/sf/wiki/do/viewPage/projects.jasperreports/wiki/Requirements

you can visit
http://jasperforge.org/ , http://jasperreports.sourceforge.net/api/overview-summary.html for more details or download





6 comments:

My Name : Sarah Hamid said...

If you have any questions or need more details; you can reach me on [e-mail]

balu said...

This is balu , i have thousand record in db . I need to display in html upto 100 . i need to have pagination i.e. first,next,previous,last . pls me out with source

SD said...

Hi Guys, i'm using JasperReports with seam framework, the code lines posted by you above was very useful for me.

Thank you very much!!

God bless you,


SD.Plox

Anonymous said...

thank you for the code it helps more than you can think thanks

Anonymous said...
This comment has been removed by a blog administrator.
RANGOLI said...

Have a look at the following

http://helptodeveloper.blogspot.com/2010/02/jasper-reports-with-pagination.html