Ticker

6/recent/ticker-posts

JasperReports Tutorial

JasperReports Tutorial

1. What is JasperReports?
JasperReports is an open-source Java reporting library that allows developers to create dynamic, pixel-perfect reports for various data sources. It is widely used for generating PDF, Excel, HTML, and other formats, making it an essential tool for data visualization and business intelligence applications.

2. How to Install JasperReports?
To use JasperReports, you need to follow these steps:

Step 1: Download JasperReports:
Visit the official JasperReports website (https://community.jaspersoft.com/project/jasperreports-library) and download the latest version of the library (e.g., jasperreports-x.x.x.jar).

Step 2: Add JasperReports to Your Project:
Include the downloaded JAR file in your Java project's build path or dependency management tool (e.g., Maven, Gradle).

Step 3: Install JasperReports Designer (Optional):
JasperReports Designer is a graphical tool that simplifies report design. You can download it from the same website and install it on your machine.

3. Report Example using JasperReports:
Let's create a simple Java program to generate a basic JasperReport using data from a collection. For this example, we will create a list of users and generate a report displaying their names and emails.

Step 1: Create a Java Bean (User.java):

java
public class User {
private String name;
private String email;

// Constructor, getters, and setters
}

Step 2: Prepare Data Source (UserDataSource.java):

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

import java.util.List;

public class UserDataSource {
public static JRDataSource getUsersDataSource(List<User> userList) {
return new JRBeanCollectionDataSource(userList);
}
}

Step 3: Design the JasperReport Template (report_template.jrxml):
Create a report template using the JasperReports Designer or any text editor. Here's a basic template to get you started:

xml
<?xml version="1.0" encoding="UTF-8"?>
<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="UserReport"
pageWidth="595" pageHeight="842" orientation="Landscape" whenNoDataType="AllSectionsNoDetail"
columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"
isIgnorePagination="true" uuid="1bbdd000-b846-48e8-9781-079b759ae57b">

<queryString language="SQL">
<![CDATA[]]>
</queryString>
<field name="name" class="java.lang.String"/>
<field name="email" class="java.lang.String"/>
<title>
<band height="79" splitType="Stretch">
<textField>
<reportElement x="0" y="30" width="555" height="30" uuid="33b78c4e-5f68-4290-9be7-72e27c5c0f92"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="20" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["User Report"]]></textFieldExpression>
</textField>
</band>
</title>
<detail>
<band height="30" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="277" height="30" uuid="f5980b8d-3ab2-4e9b-af7b-ec197003f760"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="277" y="0" width="278" height="30" uuid="c8b2e99b-14f2-4f15-947b-4a4d8c2b2973"/>
<textFieldExpression><![CDATA[$F{email}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>

Step 4: Generate the Report (ReportGenerator.java):

java
import net.sf.jasperreports.engine.*;

import java.util.ArrayList;
import java.util.List;

public class ReportGenerator {
public static void main(String[] args) {
try {
// Prepare data
List<User> userList = new ArrayList<>();
userList.add(new User("John Doe", "john.doe@example.com"));
userList.add(new User("Jane Smith", "jane.smith@example.com"));

// Load report template
String reportTemplate = "path_to_your_report_template/report_template.jrxml";
JasperReport jasperReport = JasperCompileManager.compileReport(reportTemplate);

// Create data source
JRDataSource dataSource = UserDataSource.getUsersDataSource(userList);

// Generate report
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource);

// Export the report to PDF
String outputFilePath = "path_to_output_folder/user_report.pdf";
JasperExportManager.exportReportToPdfFile(jasperPrint, outputFilePath);

System.out.println("Report generation successful!");
} catch (JRException e) {
e.printStackTrace();
}
}
}

Explanation:

  1. We created a simple Java bean called User to represent user data with name and email properties.
  2. The UserDataSource class provides a static method to convert a list of users into a JRDataSource, which is required by JasperReports for data input.
  3. We designed the report template in JRXML format, defining the layout and how to display the user data in the report.
  4. The ReportGenerator class demonstrates how to generate the report using the compiled template and user data list, and then export it to a PDF file.

That's it! You now have a basic understanding of JasperReports and how to create a simple report using it. You can explore more features and customization options as you delve deeper into JasperReports documentation and examples.

Post a Comment

0 Comments