ZetCode

JasperReports filter data

last modified February 12, 2024

JasperReports filtering data shows how to filter data in a report created with JasperReports library. One way to filter data is to use the filterExpression tag.

JasperReports is an open-source reporting library. It can create reports in various formats including PDF, HTML, XLS, or CSV. JasperReports creates page-oriented, ready-to-print documents.

JasperReports filter data example

The following application loads data from a bean collection data source and creates a report from it with JasperReports library. The report is a PDF file.

report.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
        "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports
   http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
    name="report" topMargin="20" bottomMargin="20">

    <style name="textRight" hAlign="Right" vAlign="Middle" />

    <field name="id" class="java.lang.Long"/>
    <field name="name"/>
    <field name="price" class="java.lang.Integer"/>

    <filterExpression>
        <![CDATA[$F{price} < 40000]]>
    </filterExpression>

    <detail>
        <band height="15">

            <textField>
                <reportElement x="0" y="0" width="50" height="15" style="textRight"/>

                <textElement />

                <textFieldExpression class="java.lang.Long">
                    <![CDATA[$F{id}]]>
                </textFieldExpression>
            </textField>

            <textField>
                <reportElement x="150" y="0" width="100" height="15"/>

                <textElement />

                <textFieldExpression>
                    <![CDATA[$F{name}]]>
                </textFieldExpression>
            </textField>

            <textField>
                <reportElement x="200" y="0" width="100" height="15" style="textRight"/>
                <textElement />

                <textFieldExpression class="java.lang.Integer">
                    <![CDATA[$F{price}]]>
                </textFieldExpression>
            </textField>

        </band>
    </detail>

</jasperReport>

This is the report template file. The template contains the detail band, in which each element is repeated for every record provided by the data source.

<filterExpression>
    <![CDATA[$F{price} < 40000]]>
</filterExpression>

The filterExpression tag is used for filtering data. The filtering expression shows cars whose price is lower than 40000.

<textField>
    <reportElement x="200" y="0" width="100" height="15" style="textRight"/>
    <textElement />

    <textFieldExpression class="java.lang.Integer">
        <![CDATA[$F{price}]]>
    </textFieldExpression>
</textField>

The price field is shown in the textField tag with the $F{} syntax.

report.gvy
package com.zetcode 

@Grab(group='net.sf.jasperreports', module='jasperreports', version='6.21.0')
@Grab(group='com.github.librepdf', module='openpdf', version='1.3.39')

import net.sf.jasperreports.engine.JasperCompileManager
import net.sf.jasperreports.engine.JasperFillManager
import net.sf.jasperreports.engine.JasperExportManager
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
import groovy.transform.Immutable

@Immutable
class Car {
    Long id;
    String name;
    int price;
}

def cars = [
    new Car(1L, 'Audi', 52642),
    new Car(2L, 'Mercedes', 57127),
    new Car(3L, 'Skoda', 9000),
    new Car(4L, 'Volvo', 29000),
    new Car(5L, 'Bentley', 350000),
    new Car(6L, 'Citroen', 21000),
    new Car(7L, 'Hummer', 41400),
    new Car(8L, 'Volkswagen', 21600),
]

def xmlFile = "report.xml"
def jrReport = JasperCompileManager.compileReport(xmlFile)

def ds = new JRBeanCollectionDataSource(cars)

def params = [:]
def jrPrint = JasperFillManager.fillReport(jrReport, params, ds)

JasperExportManager.exportReportToPdfFile(jrPrint, "report.pdf")

In the example, we take data from a JRBeanCollectionDataSource.

def xmlFile = "report.xml"
def jrReport = JasperCompileManager.compileReport(xmlFile)

We compile the XML template file into a JasperReport. JasperReport is a compiled template ready to be filled with data.

def cars = [
    new Car(1L, 'Audi', 52642),
    new Car(2L, 'Mercedes', 57127),
    new Car(3L, 'Skoda', 9000),
    new Car(4L, 'Volvo', 29000),
    new Car(5L, 'Bentley', 350000),
    new Car(6L, 'Citroen', 21000),
    new Car(7L, 'Hummer', 41400),
    new Car(8L, 'Volkswagen', 21600),
]

A list of cars is our data.

def ds = new JRBeanCollectionDataSource(cars)

JRBeanCollectionDataSource is a data source implementation that wraps a collection of Java bean objects. We put four Car beans into the data source.

def jrPrint = JasperFillManager.fillReport(jrReport, params, ds)

A JasperPrint object is created; an object that can be viewed, printed, or exported to other formats.

JasperExportManager.exportReportToPdfFile(jrPrint, "report.pdf")

The JasperExportManager.exportReportToPdfFile method exports the JasperPrint into a PDF file.

In this article we have created a PDF file report with filtered data.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all JasperReports tutorials.