Header Ads Widget

Responsive Advertisement

How to Use Jasper Report


JasperReports is a popular open-source reporting tool that allows developers to create sophisticated and dynamic reports. These reports can be rendered in various formats, including PDF, HTML, Excel, and more. Here's a guide to get you started with JasperReports, including setting up a simple report in a Java application.

Step 1: Set Up JasperReports

  1. Download JasperReports Library:
    • You can download the latest JasperReports library from the Jaspersoft Community.
  2. Add JasperReports to Your Project:
    • If you're using Maven, add the following dependencies to your pom.xml:

xml

<dependency>

    <groupId>net.sf.jasperreports</groupId>

    <artifactId>jasperreports</artifactId>

    <version>6.20.0</version>

</dependency>

 

Step 2: Create a Simple Report Template

  1. Design the Report:
    • You can design your report using Jaspersoft Studio, a free Eclipse-based report designer for JasperReports.
  2. Create a Report Template (JRXML):
    • Create a simple report template (JRXML file) using Jaspersoft Studio. For example, create a file named sample_report.jrxml with the following content:

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="sample_report" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="7d7b88b3-4b30-43e2-9e4e-98c45e1fbb7b">

    <queryString>

        <![CDATA[SELECT * FROM your_table]]>

    </queryString>

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

    <field name="name" class="java.lang.String"/>

    <title>

        <band height="50">

            <textField>

                <reportElement x="0" y="0" width="555" height="50" uuid="b12345d8-48f6-41a4-a85e-4d12c3a85f3c"/>

                <textElement>

                    <font size="24" isBold="true"/>

                </textElement>

                <textFieldExpression><![CDATA["Sample Report"]]></textFieldExpression>

            </textField>

        </band>

    </title>

    <detail>

        <band height="20">

            <textField>

                <reportElement x="0" y="0" width="100" height="20" uuid="e7589a1d-38d4-4b2a-b5d4-5dfae8b1b5f5"/>

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

            </textField>

            <textField>

                <reportElement x="100" y="0" width="455" height="20" uuid="f7589a1d-38d4-4b2a-b5d4-5dfae8b1b5f6"/>

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

            </textField>

        </band>

    </detail>

</jasperReport>

 

Step 3: Generate the Report in Java

  1. Load and Compile the Report Template:
    • Use JasperReports API to load and compile the JRXML file.
  2. Fill the Report with Data:
    • Use a data source to fill the report with data. This can be a database connection or a collection of Java objects.
  3. Export the Report:
    • Export the filled report to the desired format (PDF, HTML, etc.).

Here is an example Java code to generate a PDF report:

java

import net.sf.jasperreports.engine.*;

import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

 

import java.util.*;

 

public class JasperReportExample {

    public static void main(String[] args) {

        try {

            // Compile the JRXML file

            JasperReport jasperReport = JasperCompileManager.compileReport("path/to/sample_report.jrxml");

 

            // Create a data source (for example, a list of Java objects)

            List<Map<String, Object>> dataSource = new ArrayList<>();

            Map<String, Object> data1 = new HashMap<>();

            data1.put("id", 1);

            data1.put("name", "John Doe");

            dataSource.add(data1);

 

            Map<String, Object> data2 = new HashMap<>();

            data2.put("id", 2);

            data2.put("name", "Jane Doe");

            dataSource.add(data2);

 

            JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(dataSource);

 

            // Fill the report with data

            Map<String, Object> parameters = new HashMap<>();

            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanCollectionDataSource);

 

            // Export the report to a PDF file

            JasperExportManager.exportReportToPdfFile(jasperPrint, "path/to/sample_report.pdf");

 

            System.out.println("Report generated successfully!");

        } catch (JRException e) {

            e.printStackTrace();

        }

    }

}

 

Explanation

  1. Compile the JRXML File:
    • The JasperCompileManager.compileReport method compiles the JRXML file into a JasperReport object.
  2. Create a Data Source:
    • The JRBeanCollectionDataSource is used to create a data source from a list of Java objects. You can use other data sources like JDBC or custom data sources as well.
  3. Fill the Report with Data:
    • The JasperFillManager.fillReport method fills the compiled report with data from the data source.
  4. Export the Report:
    • The JasperExportManager.exportReportToPdfFile method exports the filled report to a PDF file.

Conclusion

JasperReports is a flexible and powerful reporting tool that integrates well with Java applications. By following the steps above, you can create, design, and generate reports in various formats. For more advanced reporting features, refer to the official JasperReports documentation and explore the capabilities of Jaspersoft Studio.




Jasper Report
Jasper Report




Post a Comment

0 Comments