JasperReports uses XML file for creating report. The user
designs a report using respective XML tags and attributes, defined in a file
called jasperreports.dtd. Save the file with a
JRXML/XML extension. This file contains all the information about the report
such as title, page headers and footers, column headers and footers, summary,
query, fonts, images, report details, etc.
For example, the following XML template is used to design a
report that will show the salesman details with a report title that has been
given as a parameter.
Listing 1
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="demo">
<parameter name="Report_Title" class="java.lang.String"/>
<queryString><![CDATA[select * from sales_master]]></queryString>
<field name="SALESMANNO" class="java.lang.String"/>
<field name="SALESMAN_NAME" class="java.lang.String"/>
<field name="CITY" class="java.lang.String"/>
<field name="STATE" class="java.lang.String"/>
<field name="QTY_SALES" class="java.math.BigDecimal"/>
<title>
<band height="50" >
<textField >
<reportElement x="230" y="4" width="100" height="18"/>
<textFieldExpression class="java.lang.String">
<![CDATA[$P{Report_Title}]]>
</textFieldExpression>
</textField>
</band>
</title>
<columnHeader>
<band height="30">
<staticText>
<reportElement x="29" y="0" width="102" height="25"/>
<text><![CDATA[Salesman Name]]></text>
</staticText>
<staticText>
<reportElement x="146" y="2" width="104" height="23"/>
<text><![CDATA[City]]></text>
</staticText>
<staticText>
<reportElement x="260" y="1" width="100" height="24"/>
<text><![CDATA[State]]></text>
</staticText>
<staticText>
<reportElement x="383" y="1" width="94" height="23"/>
<text><![CDATA[Quantity Sold]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="100">
<textField>
<reportElement x="28" y="8" width="100" height="29"/>
<textFieldExpression class="java.lang.String">
<![CDATA[$F{SALESMAN_NAME}]]>
</textFieldExpression>
</textField>
<textField >
<reportElement x="146" y="11" width="100" height="25" />
<textFieldExpression class="java.lang.String">
<![CDATA[$F{CITY}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="263" y="11" width="100" height="24" />
<textFieldExpression class="java.lang.String">
<![CDATA[$F{STATE}]]>
</textFieldExpression>
</textField>
<textField >
<reportElement x="384" y="12" width="100" height="24" />
<textFieldExpression class="java.math.BigDecimal">
<![CDATA[$F{QTY_SALES}]]>
</textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Brief descriptions of the most important sections for
designing a report are given below.
title
As the name implies, this section contains the report title
and appears at the top of the report.
queryString
This section defines the SQL query to obtain data for report
filling process. This section of the XML template does not contain a height or
a width.
pageHeader
This section appears at the top of each page in the report
and can be used to display information such as dates, page numbers, etc.
columnHeader
This section is used to display the information as heading
for each column.
detail
This is the body of the report. It contains data from the
data repository.
columnFooter
This section goes at the bottom of each column and can be
used to display information such as the total number of records.
pageFooter
This section appears at the bottom of each page in the
report.
Summary
This section contains the information that will be displayed
at the end of the report.
Now, let us see how to use JasperReports in Java
applications. To create JasperReports report, this JRXML/XML file needs to be
loaded. A JasperDesign Object represents the report
design. The following codes illustrate this.
Listing 2
JasperDesign jasperDesign = JRXmlLoader.load (
C:/jasperReports/demo.jrxml);