Pitch your project

Random text






Blogs

0    
By admin Category Uncategorized, Posted February 12th, 2016
Java reporting

Automation of routine activities and data summaries in a decision-supporting format, commonly known as “reports” has been one of the core purposes in the software development industry.

Different users have different troubles in report development. therefore we can summarize most report issues into four. Core changes, Result exporting, complicated reports and chart reports.

Our point of focus is however on report generation and exporting.

Result exporting

There is a wide range of formats in which we can export data into, i.e. .pdf, .xml, .xlsx, .xls, .csv among others such as .odt, .rtf etc .
There are various means of achieving report generation in system development, but in this case we’ll have a look at JasperReport, which is a report generation engine.

The main purpose or reporting is to provide print ready files and documents in the simplest way possible.

JasperReport Architecture

alt text

The architecture shows how the report engine retrieves data from a data source, and through JRXML generates specifies report files.

Environment

As a normal Java application we would need to import jar files/ libraries like jasperreports-2.0.4.jar, iText-2.0.7.jar (used for PDF exporting)
just to be able to perform report generation. or use dependencies such as

<dependency>
    <groupid>net.sf.jasperreports</groupid>
    <artifactid>jasperreports</artifactid>
    <version>5.0.1</version>
</dependency>

However our main focus remains on the root element.

Report design

Elements contained in a Jasper design file JRXML:

  • <jasperreport>: the root element.
  • <title>: its contents are printed only once at the beginning of the report
  • <pageheader> – its contents are printed at the beginning of every page in the report.
  • <detail> – contains the body of the report, repeated by n number of results
  • <pagefooter> – its contents are printed at the bottom of every page in the report.
  • <band> – defines a report section, all of the above elements contain a band element as its only child element.

The XML below shows an example of how a design would look like


<jasperreport name="sample_report" > <querystring> < ![CDATA[select item_name,item_amount from item where item_amount <=100]]> </querystring> <field name="ITEM_NAME" class="java.lang.String"></field> <field name="ITEM_AMOUNT" class="java.math.BigDecimal"></field> <columnheader> <band height="28" isSplitAllowed="true"> <statictext> <reportelement x="40" y="11" width="193" height="15" key="staticText-1"></reportelement> <text> < ![CDATA[Item Name]]> </text> </statictext> <statictext> <reportelement x="330" y="11" width="193" height="15" key="staticText-2"></reportelement> <text> < ![CDATA[Item Amount]]> </text> </statictext> </band> </columnheader>

The following are the main sections that define report behaviour and layout. the above XML being a simple example just to illustrate:

  • <querystring>: contains the SQL statement which retrieves the report result.
  • <field name>: defines the resulted fields from the query, and give them name to reuse them into the report body (they are case-sensitive).
  • <statictext>: contains the header titles “Item Name” in < ![CDATA[Item Name]]> tag format.
  • <textfieldexpression>: defines the appearance of result field.
  • $F{ITEM_NAME}: is a variable contains the value of Query result predefined field in the tag <field name>.

The above code specifies the styles used in a particular including fonts,styles and their sizes.

Report Implementation Service

The following packages need to be imported in order to implement the reporting service

        import java.sql.Connection;
        import java.sql.DriverManager;
        import java.sql.SQLException;
        import java.util.HashMap;

        import net.sf.jasperreports.engine.JRException;
        import net.sf.jasperreports.engine.JRExporterParameter;
        import net.sf.jasperreports.engine.JasperCompileManager;
        import net.sf.jasperreports.engine.JasperExportManager;
        import net.sf.jasperreports.engine.JasperFillManager;
        import net.sf.jasperreports.engine.JasperPrint;
        import net.sf.jasperreports.engine.JasperReport;
        import net.sf.jasperreports.engine.export.JRXlsExporter;

Then define the data source depending on the different methods available either when using MySQL, Postgresql, or Oracle

        Connection conn = null;
        try
        {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        String oracleURL = "jdbc:oracle:root:@localhost:5432:db";
        conn = DriverManager.getConnection(oracleURL,"username","password");
        conn.setAutoCommit(false);
        }
        catch(SQLException exception)
        {
        exception.printStackTrace();
        }
        return conn;

Jasper objects

Define a Jasper object that will hold report template, compiled files, and result files should atleastlook as follows

 /* JasperReport is the object
        that holds our compiled jrxml file */
        JasperReport jasperReport;


        //JasperPrint is the object contains report after result filling process
        JasperPrint jasperPrint;

        // connection is the data source we used to fetch the data from
        Connection conn = establishConnection();
        // jasperParameter is a Hashmap contains the parameters
        // passed from application to the jrxml layout

Compile process

// jrxml compiling process
jasperReport = JasperCompileManager.compileReport
("C://sample_report.jrxml");

// filling report with data from data source

jasperPrint = JasperFillManager.fillReport(jasperReport,jasperParameter, conn);

Finally, we need to export our desired results into desired files either as PDF, or XLS.

// exporting process
// 1- export to PDF
JasperExportManager.exportReportToPdfFile(jasperPrint, "C://report.pdf");

// 3- export to Excel sheet
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "C://report.xls" );

exporter.exportReport();

At this stage on running the program, one should have successfully generated and exported either report.pdf, report.xls

Posted by Bryant Smith

Leave a Reply

Your email address will not be published. Required fields are marked *