Dhrubok Infotech Services Ltd.

Making Dynamic Tables with Jasper Report and Spring Boot

Making Dynamic Tables with Jasper Report and Spring Boot

Jasper report is an open-source java class library that aids developers to add reporting capabilities to their Java applications. The most common way to represent data is in a tabular form, but this is perhaps a bit tricky in jasper report. Worry not because this post tells you all about making dynamic tables with Jasper Report. Without any further ado, let’s get started.

Let’s say you work for a company and they want you to generate a report that displays all the employees’ information in the table and at the bottom of the table, you have to display the number of employees the company has.

To do the above task in Spring Boot, you need to have an employee table. Let’s assume you just need to display the id, name, and department of each employee. Hence your Employee class should look something like this:

Making dynamic tables with jasper report

You have to write your necessary Repository, Service, and Controller code, I am not going into much detail about that, since that is not our main goal here.

Okay, we have the necessary model ready now, it’s time to dig into our report.

Getting Started with Jasper Report Table

You can use the Eclipse plug-in, or you can download Jaspersoft studio from JasperSoft Community -https://community.jaspersoft.com/project/jaspersoft-studio/releases.

After setting up your JasperSoft studio, you can begin by creating a new Blank A4 jasper report. You have to make sure that you choose Data Adapter- One Empty Record – Empty rows in the Data Source section.

You will see your blank report has been created. Now did you notice a section called Palette at the right-hand side of JasperSoft studio. If you scroll down in the basic element section in Palette you will see an option named Table.

Making dynamic tables with jasper report

Click on it and draw a table in the detail 1 section of your main report. As soon as you finish drawing you will see a window pop up. Now, the next steps are really important if you want to pass your dataset from your database to your jasper report in Spring boot.

In the pop-up table window, first, you have to select a dataset. Choose “Create a Table with a new dataset” and click “Next”.

Making dynamic tables with jasper report

Secondly, choose “create an empty data set” and give your data set a name, and click “Next”.

Making dynamic tables with jasper report

In the connection section, choose “Use an empty Data Source” and click Next.

Making dynamic tables with jasper report

After that just click “Next” and choose your layout and you will be ready to go.

In the main report, if you double click on the table it will take you to the Table section and there you can edit your table. Right-click on a table column gives you a variety of options. You can create a column before or after the selected column.
Since we have three fields in our employee class, we will need three columns.

You can resize every column by selecting the column first and dragging the sides with the specified arrows.

An outline in the bottom left-hand side of the table gives all the column information and the empty dataset that you have created.
You can see a table consisting of 5 sections.
Table header – If your Table has a title you can put it here.
Column Header – Contains the title of each column of the table.
Detail – This is the main part of the table. You have to put all your table fields here. This part is dynamic and all your information from the dataset will be shown here.
Column Footer – If you need to put anything at the end of your dataset you can put it here.
Table Footer – Same as column footer.

Okay if you understand till now, you are good to move further.

In the column Header in each cell write your necessary column title with static text. You can format the font size, font style and alignment of each static text. Now go to your main report and the table would look something like this.

Making dynamic tables with jasper report

Configure Your Dataset

The next step is very very crucial, you have to do everything right to make things work accurately. In the main report, at the bottom left-hand side you will see Parameters, right-click on it and choose to create a parameter

Making dynamic tables with jasper report

Give your parameter a name, such as “CollectionBeanParam” and choose your class as “JRBeanCollectionDataSource”. You can find this option in the three dot box beside class name.

Making dynamic tables with jasper report

Now it’s time to create your table fields.
Go to your table’s outline and under your dataset name- “MyDataSet” create fields.
Now be careful while naming your fields. You have to make sure you give the same name as your class’s attributes. In this case, you need three fields: id, name, and department. Make sure you choose the correct classes for each field. Id should be java.lang.Long and others should be java.lang.String. Now drag and drop the fields to their corresponding detail cell.

Making dynamic tables with jasper report

The next step is to set your dataset.
From the outline right click on the table and select show properties. In the properties section go to the dataset tab and select “ Use a JRDataSource expression”. You can see an edit button beside it. Click on it and a window will appear.

In the expression editor, click on parameters and then choose the CollectionBeanParam that you have created a while ago. Lastly click “Finish”.

Making dynamic tables with jasper report

Table Parameters

Now we have another small task left, at the beginning of this post I said you have to show the number of total employees in the table. Since it is not a part of our dataset but the value will come from our backend service we can pass it in the parameter. This is a bit tricky since we want to display this in the table. At first we need to declare a parameter in the table dataset which is the MyDataSet.
So let’s create a parameter called tableNoOfEmployees and a static text that says “ No of employees”
I have deleted all the column footer cells, since we don’t need it. This is what it should look like.

Making dynamic tables with jasper report

Now, go to the main report and create another parameter called “noOfEmployees”. We have to do this because you cannot pass values directly from your spring boot service or controller to your table parameters. You have to pass the values to your global parameters which you have to map with your table parameter. Ok, after you have created your global parameter “noOfEmployees”, your job is now to map this parameter to your table parameter. In order to do so go to your table properties, dataset section. At the bottom you can see parameters, click on it. A window will appear, now your job is to map the parameters. Click on add, select the table parameter and for the expression, you have to select the global parameters. See the photo below if you have any confusion.

Making dynamic tables with jasper report

Your work with JasperSoft studio is done, if you are using a plug-in the jrxml file is already generated, and if you are using the studio you have to copy the source file and paste it in a new created jrxml file in your resource folder of your spring boot application.

You have to determine the classpath of your jrxml file to your InputStream and compile a jasper report with it. After that you have to create a datasource with your dataset, for example, here our dataset is the list of employees.
Now to the important part here is you have to put the parameters name exactly how you have declared in your jasper report. The CollectionBeanParam will map your dataset to the fields in your jasper report.
The noOfEmployee parameter will take the value and pass the value to your mapped table parameter. If you want to download it as a pdf you have to export it.

Making dynamic tables with jasper report

That’s it for today. Thank you for reading. See you around. If you have any questions, you can email at info@dhrubokinfotech.com.

Subscribe to Our Newsletter

Let’s stay in touch.

Have an idea in mind?