Don´t use Table component in summary band in JasperReport

| January 3, 2012 | 2 Comments

We are designing a report using JasperReport technology. One of the most commonly used component is the table component: a component that allow you to format a collections of data in a table format and can support as data source many differents types such as beans collections, sql connection,etc…

¿What is the problem?

Imagine that you put the table component in the report, you use for example a collections of beans to populate the table. So in the aJava code of your application you can have something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
nombreReport = nombreReport+".jasper";
 
// Name of the report
 
InputStream reportStream = this.getClass().getResourceAsStream("/reportsCompiled/"+nombreReport);
 
// Compile the report
 
JasperReport jr = (JasperReport) JRLoader.loadObject(reportStream);
// Creates the  JasperPrint object : layout, datasource, params
 
JasperPrint jp = JasperFillManager.fillReport(jr, params, dataSource);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
// Create a JRXlsExporter instance
 
JRHtmlExporter exporter = new JRHtmlExporter();
 
// Here we assign the parameters jp and baos to the exporter
 
exporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT, jp);
exporter.setParameter(JRHtmlExporterParameter.OUTPUT_STREAM, baos);
exporter.setParameter(JRHtmlExporterParameter.IGNORE_PAGE_MARGINS, Boolean.TRUE);
// Html specific parameters
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "report/image?image=");
// Retrieve the exported report in XLS format
exporter.exportReport();

where the data source is an object of type  JRBeanCollectionDataSource that have a list of beans as parameter in the constructor.

Inside the report (the jrxml file) you need to declare wich datasource you are going to use:

1
2
3
4
5
6
7
8
9
<subDataset name="FuenteOrigenDatosOracle">
 
<field name="nombre">
 
<fieldDescription><![CDATA[nombre]]></fieldDescription>
 
</field>
 
</subDataset>

and the table component:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<reportElement key="table 2" style="table 2" stretchType="RelativeToBandHeight" x="0" y="0" width="860" height="149"/>
 
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
 
<datasetRun subDataset="FuenteOrigenDatosOracle">
 
<datasetParameter name="REPORT_DATA_SOURCE">
 
<datasetParameterExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]>
 
</datasetParameterExpression>
 
</datasetParameter>
 
</datasetRun>

But, when you tried to test the report there is no data. If you check already that the datasource in your Java code is fine and you read the logs checking that there is no error. ¿Where is the problem?

And the solution is…

A simple but common error is to place the table component inside a band that is forbidden. You need to place the table component in a band different from summary band. Don´t put the table component inside the summary band, beacause in this case when the report is compiled and executed no error is sshow in the logs but the data table is disappear. This can be for you waste a lot of time seeking where is the error and is more simple than you can imagine.

Different table: crosstab.

The crosstab component in jasperreport is a kind of table that have to be place in the summary band. Is not the purpose of this post to explain the use of a crosstab, only we want to note that the crosstab need to be place in the summary band to work.

Conclusion

If you have a problem with table component in jaspereport and don´t see the error in logs yoy can check:

  • The table is place in a band different from band summary.

Tags: , ,

Category: JasperReport, Java, Reporting

Comments (2)

Trackback URL | Comments RSS Feed

  1. Madan Narra says:

    So which is the alternative band to have the table component other then the Summary band ? I just faced this issue and saw ur post not to have table component in Summary Band.

    • Paco says:

      For example you can put the table in the other bands, like header, footer or even detail but never in the summary band. The function of the summary band is make calculations with data.

Leave a Reply