ZetCode

JasperReports CSV

last modified February 12, 2024

JasperReports CSV tutorial shows how to create a report from a CSV data source.

JasperReports is an open-source reporting library. It can create reports in various formats including PDF, HTML, XLS, or CSV. JRCsvDataSource is a datasource implementation that reads a CSV stream.

JasperReports CSV example

The following application creates a report from from a CSV file with JasperReports library. The report is a PDF file.

cars.csv
id,name,price
1,Audi,52642
2,Mercedes,57127
3,Skoda,9000
4,Volvo,29000
5,Bentley,350000
6,Citroen,21000
7,Hummer,41400
8,Volkswagen,21600
9,BMW,36600

The cars.csv contains the data to be used in our report.

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">

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

    <detail>
        <band height="15">

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

                <textElement textAlignment="Right" verticalAlignment="Middle"/>

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

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

                <textElement textAlignment="Left" verticalAlignment="Middle"/>

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

            <textField>
                <reportElement x="200" y="0" width="100" height="15" />
                <textElement textAlignment="Right" verticalAlignment="Middle"/>

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

        </band>
    </detail>

</jasperReport>

This is the report template file. The template contains two bands: columnHeader and detail.

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

There are three fields in the template. Fields are mapped to the elements of the data source. In our case, fields are mapped to the CSV column names.

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

    <textElement textAlignment="Left" verticalAlignment="Middle"/>

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

A text field is an element that has an associated expression, which is evaluated with every iteration in the data source to obtain the text content. This text field displays the name of the car. A text field in a detail band is evaluated for each record in the data source. Since we have nine records in the CSV file, the text field is evaluated nine times.

report.gvy
@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.JRCsvDataSource

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

// def columnNames = ["id", "name", "price"] as String[]

def fileName = "cars.csv"
def ds = new JRCsvDataSource(fileName)
ds.setUseFirstRowAsHeader(true)
// ds.setColumnNames(columnNames)

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

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

The code example generates a PDF report.

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

The XML template file is compiled into a JasperReport. JasperReport is a compiled template ready to be filled with data.

def fileName = "cars.csv"
def ds = new JRCsvDataSource(fileName)
ds.setUseFirstRowAsHeader(true)

JRCsvDataSource is a data source implementation that reads data from a CSV file. With setUseFirstRowAsHeader method, we tell Jasper to use the first row as a header. Alternatively, we can use the setColumnNames method.

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

With the JasperFillManager.fillReport method, we create a JasperPrint object; an object that can be viewed, printed or exported to other formats.

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

The JasperExportManager.exportReportToPdfFile method transforms the JasperPrint object into a PDF file.

In this article we have created a report with JasperReports library from a CSV file.

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.