refermycode.com

Develop your world..

How to add alphabetical sequence in Jasper Report.

In this blog we are going to learn one interesting topic of Jasper Report technology. We are going to generate and add alphabetical sequence in Jasper Report. So first of all, let us create simple report which includes the numeric sequence for records. Create a simple PDF report with the help of following steps in Java and Jasper.

We are going to create a report which will print the following information of Employees in our PDF.

Serial NumberEmployee NumberEmployee NameEmployee AddressEmployee Salary
11001AjitLatur40,000
21002AmitAurangabad50,000
31003AniketNashik50,000
41004AnupPune60,000
51005HiteshAkurdi40,000
61006KuldeepHyderabad60,000
71007MayurVishrantwadi50,000
81008OmkarRajgurunagar30,000
91009TusharKopargaon60,000
101010VijayShirdo50,000

So, we will start with the Design part first,

Step 1:
Create a new report in Jasper with blank A4 template. If you are new in Jasper Report then you can refer our tutorial of Jasper from here. Jasper Tutorial includes the information of all the necessary environment setup and provide the guidance for creating a simple report in Jasper and Java.

So now we directly jump to design phase in iReport. Here we are going to design report in Java language hence change the language of report to Java. To change the language click on report in Report Inspector Window and find the language property in Properties window of iReport, by default it is set to Groovy language, click on combo box and change it to Java, as show in following screenshot.

chapter5_0

Step 2:

We are going to display the information of Employees which is filled in above table, we are going to use Field component of Jasper which are the placeholder and it will stored the values which we want to display in our report. These value will be set with the help of Java POJO classes which we will see in the later part in this blog.
For adding the field go into the Report Inspector Window of iReport and click Right Button of mouse on Field and select the “Add Field” option to add the field in report as shown following screenshot.

chapter5_1

Step 3:

After that select added field and rename it by selecting rename option. Jasper provide multiple datatypes for field, parameters and variables. You can change the datatype of Field from property window of iReport. In this chapter we are going to use string datatype to generate the report and String is Default datatype for any field, variable or parameter, hence I am not going to change the datatype. If you want different datatype then you can select the field and change the datatype which you want in your report as shown in following screenshot.

chapter5_2

Step 4:

In this chapter we are just putting the values which is set from Java side so we will remove the other band except Detail and Column Header Band. So drag and drop the required fields in Detail Band. When you add the fields in the Detail Band, we get an option to which value should be display in the field section, mean value which is stored by field or you can show count of the selected field as shown in following screenshot.

chapter5_3

Step 5:

When you used drag and drop method for adding the field in Detail Band of report then Static Text is automatically gets added in the Column Header of report with the label name as Default text in it.

chapter5_4

Step 6:

We can change it by double click in the static text. so we will change the Header labels to standardise our report with proper Header names, After changing the the labels our report design will looks as shown in following screenshot.

chapter5_5

Step 7:

Now lets move to the actual part on our blog. We have to add Sequence Number in our Report. We can also manage the sequence by adding one extra field for sequence number and set value of it from Java. But in case of grouping it will not works properly. Because when we use the grouping concept then Jasper have ability to group the records on the basis of field which is used for grouping and these grouping can be break our sequence if we not handled it from Java. So we will handle these sequence in Jasper instead of Java by using some build in Variable provided by Jasper.

Hence as shown in below screenshot, we are going to use the build in variable REPORT_COUNT to generate the sequence number for each record of our report.

Blog1_1

Now, we can see the generated sample XML code (.jrxml) in XML view of iReport. Our jrxml file is

<?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="report2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<field name="employeeNumber" class="java.lang.String"/>
<field name="employeeName" class="java.lang.String"/>
<field name="employeeAddress" class="java.lang.String"/>
<field name="employeeSalary" class="java.lang.String"/>
<columnHeader>
<band height="30">
    <staticText>
            <reportElement x="106" y="5" width="100" height="20"/>
            <textElement>
                    <font size="8" isBold="true"/>
            </textElement>
            <text><![CDATA[Employee Number]]></text>
    </staticText>
    <staticText>
            <reportElement x="218" y="5" width="100" height="20"/>
            <textElement>
                    <font size="8" isBold="true"/>
            </textElement>
            <text><![CDATA[Employee Name]]></text>
    </staticText>
    <staticText>
            <reportElement x="328" y="5" width="100" height="20"/>
            <textElement>
                    <font size="8" isBold="true"/>
            </textElement>
            <text><![CDATA[Employee Address]]></text>
    </staticText>
    <staticText>
            <reportElement x="439" y="5" width="100" height="20"/>
            <textElement>
                    <font size="8" isBold="true"/>
            </textElement>
            <text><![CDATA[Employee Salary]]></text>
    </staticText>
    <staticText>
            <reportElement x="6" y="5" width="89" height="20"/>
            <textElement>
                    <font size="8" isBold="true"/>
            </textElement>
            <text><![CDATA[Sr. Number]]></text>
    </staticText>
</band>
</columnHeader>
<detail>
<band height="26">
    <textField>
            <reportElement x="106" y="6" width="100" height="20"/>
            <textElement>
                    <font size="8"/>
            </textElement>
            <textFieldExpression><![CDATA[$F{employeeNumber}]]></textFieldExpression>
    </textField>
    <textField>
            <reportElement x="218" y="6" width="100" height="20"/>
            <textElement>
                    <font size="8"/>
            </textElement>
            <textFieldExpression><![CDATA[$F{employeeName}]]></textFieldExpression>
    </textField>
    <textField>
            <reportElement x="328" y="6" width="100" height="20"/>
            <textElement>
                    <font size="8"/>
            </textElement>
            <textFieldExpression><![CDATA[$F{employeeAddress}]]></textFieldExpression>
    </textField>
    <textField>
            <reportElement x="439" y="6" width="100" height="20"/>
            <textElement>
                    <font size="8"/>
            </textElement>
            <textFieldExpression><![CDATA[$F{employeeSalary}]]></textFieldExpression>
    </textField>
    <textField>
            <reportElement x="6" y="6" width="89" height="20"/>
            <textElement>
                    <font size="8"/>
            </textElement>
            <textFieldExpression><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
    </textField>
</band>
</detail>
</jasperReport>

We have finished the design phase of report, now let us move on to the Java side.
Step 1:
Create new project by selecting the simple Java Application Setting and add all the necessary libraries which is described in our Jasper Tutorial.

Step 2:
We have to display the multiple records by using Java POJO classes, so create a new class with name Employee and create the variables with the same name and datatype which was we used in .jrxml file. Create getter and setter method for each variable.

package com.refermycode.mypackage;
/*
* @author Mayur
*/

public class Employee {
String employeeNumber = "";
String employeeName = "";
String employeeAddress = "";
String employeeSalary = "";
public String getEmployeeAddress() {
return employeeAddress;
}
public void setEmployeeAddress(String employeeAddress) {
this.employeeAddress = employeeAddress;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(String employeeNumber) {
this.employeeNumber = employeeNumber;
}
public String getEmployeeSalary() {
return employeeSalary;
}
public void setEmployeeSalary(String employeeSalary) {
this.employeeSalary = employeeSalary;
}
}

Step 3:
In main class add the following code to generate the PFD.

package com.refermycode.mypackage;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

/*
* @author Mayur
*/

public class JasperTutorial2 {

public static List addEmployeeInfo() {
List employeeList = new ArrayList();

Employee employee = new Employee();
employee.setEmployeeNumber("1001");
employee.setEmployeeName("Ajit");
employee.setEmployeeAddress("Latur");
employee.setEmployeeSalary("40,000");
employeeList.add(employee);

employee = new Employee();
employee.setEmployeeNumber("1002");
employee.setEmployeeName("Amit");
employee.setEmployeeAddress("Aurangabad");
employee.setEmployeeSalary("50,000");
employeeList.add(employee);

employee = new Employee();
employee.setEmployeeNumber("1004");
employee.setEmployeeName("Aniket");
employee.setEmployeeAddress("Nashik");
employee.setEmployeeSalary("50,000");
employeeList.add(employee);

employee = new Employee();
employee.setEmployeeNumber("1004");
employee.setEmployeeName("Anup");
employee.setEmployeeAddress("Pune");
employee.setEmployeeSalary("60,000");
employeeList.add(employee);

employee = new Employee();
employee.setEmployeeNumber("1005");
employee.setEmployeeName("Hitesh");
employee.setEmployeeAddress("Akurdi");
employee.setEmployeeSalary("50,000");
employeeList.add(employee);

employee = new Employee();
employee.setEmployeeNumber("1006");
employee.setEmployeeName("Kuldeep");
employee.setEmployeeAddress("Hyderabad");
employee.setEmployeeSalary("60,000");
employeeList.add(employee);

employee = new Employee();
employee.setEmployeeNumber("1007");
employee.setEmployeeName("Mayur");
employee.setEmployeeAddress("Vishrantwadi");
employee.setEmployeeSalary("50,000");
employeeList.add(employee);

employee = new Employee();
employee.setEmployeeNumber("1008");
employee.setEmployeeName("Omkar");
employee.setEmployeeAddress("Rajgurunagar");
employee.setEmployeeSalary("30,000");
employeeList.add(employee);

employee = new Employee();
employee.setEmployeeNumber("1009");
employee.setEmployeeName("Tushar");
employee.setEmployeeAddress("Kopargaon");
employee.setEmployeeSalary("60,000");
employeeList.add(employee);

employee = new Employee();
employee.setEmployeeNumber("1010");
employee.setEmployeeName("Vijay");
employee.setEmployeeAddress("Shirdi");
employee.setEmployeeSalary("60,000");
employeeList.add(employee);

return employeeList;
}

public static void main(String[] args) throws FileNotFoundException {
try {
InputStream inputStream = new FileInputStream("C:/JasperTutorial2/src/com/refermycode/mypackage/Report2.jrxml");
JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

HashMap parameters = new HashMap();
List employeeList = addEmployeeInfo();
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(employeeList);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, "C:/test_jasper.pdf");
} catch (JRException ex) {
Logger.getLogger(JasperTutorial2.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

If you want to know how above code is work then refer the page of our Tutorials from Here.

When we execute above code then Jasper will generate the desire output with help of Java POJO classes and collection framework.

Following is the generated sample pdf of our above code.

Blog1_2

This is the way to generate Numerical Sequence for records in Jasper Report.

Now the actual implementation of this blog is started from here.

To generate alphabetical sequence for records,We need to made some changes in Jasper as well as Java. So let us make changes in Java code first.

Create one ArrayList which include the Alphabets which we need to print instead of Number and put it into the map of parameters with the name “characterList”.

List characters = new ArrayList(26);
for (char c = ‘A’; c &lt;= ‘Z’ ; c++) {
characters.add(String.valueOf(c));
}
parametersMap.put("charactersList", characters);

That is sufficient from Java code. Now we also need to made some changes in design. So add the parameter in the design as shown in following screenshot and give name same as given in Java code, i.e. “characterList”.

Blog1_3

Replace the expression of text field which print the sequence number by following code as shown in following screenshot.

$P{charactersList}.get(($V{REPORT_COUNT}1)%26)

This code will replace the numeric values by alphabets and after 26 character it will start sequence from first alphabet again.

Blog1_4

After this run the program and find the generated PDF which includes the alphabetical sequence which is set in ArrayList ‘charactersList’ as shown in following screenshot.

Blog1_5

Hope this blog is help you to generate custom sequence in your Jasper Report.

Please don’t forget to comment your feedback for this post 🙂