This guide helps you to create your first report. It uses FoodMart data source, a MySQL database example provided when installing JasperReports Server.
Name your report and choose a path to store it.
Define Data Source
Define FoodMart as your data source. See iReport - Adding a Data Source for a guide.
Click on Edit Report Query icon.
Copy and paste the below SQL query in the Edit Report Query window.
SELECT emp.employee_id, emp.first_name, emp.last_name, emp.position_title, emp.birth_date, emp.hire_date, emp.end_date, emp.salary, emp.gender, dep.department_id, dep.department_description FROM employee emp JOIN department dep ON (emp.department_id=dep.department_id) WHERE dep.department_id IN (1,2,3,4,5);
Note: if your query is valid, iReport will automatically retrieve the fields. If not, check your query mistakes.
Then do OK to save your SQL query. You will see on your left the Report Inspector panel filled with query's fields.
- For this example, keep the following bands: Page Header, Detail and Page Footer.
To delete bands, right click on it and select Delete Band.
Let's fill it out
Page Header Band
The Palette window contains different types of elements for a report.
To activate Palette, click on Window > Palette.
Drag and drop 6 Static Text fields in Page Header band. Double click on each one and name them as below:
Properties allow you to customize elements of a report.
- To activate Properties, click on Window > Properties.
Select any static field and modify its properties: backcolor, text size, bold, italic, field size...
Note: some properties are directly available on that mini toolbar
Select all static fields, right-click and select Padding and Borders. Modify the Line witdh value to 1 and Line color to black. Then do OK.
Formatting Tools Window
This is really important to use these tools. You can save a lot of time when creating reports !
- To activate Formatting Tools, click on Window > Formatting Tools.
Select the following static fields: First Name, Last Name, Position Title, Salary, Department. In the Formatting Toolswindow, click on Align Top and Remove Horiz. Space.
See the difference, your fields are well organize with only 2 clicks.
Double click on the bottom border of Page Header band to reduce its height.
Now you know basic tools to create contents for your report.
Drag from Report Inspector and drop in Detail band the following fields:
first_name, last_name, position_title, salary, department_description.
Double click on the bottom border of Detail band to reduce its height.
Page Footer Band
Drag from Palette and drop in Page Footer band the 2 elements Current Date and Page X of Y.
For Current Date element, choose an appropriate pattern.
Place the 2 elements. Don't forget to stretch enough Current Date element to display the entire date. Double click on the bottom border of Page Footer band to reduce its height.
To preview your report, click on Preview button.
See the preview. Not bad but they are some issues:
- Text touches cells' borders.
- Salary display pattern is not clear.
Some department's descriptions are truncated
'''Text touches cells' borders:''' add some padding for all Detail band's elements: 5px for Left and Right; 2px for Top and Bottom.
Salary display pattern: see Salary field's Properties. Add the following Pattern:
Truncated column: select all Detail band's elements and go to Properties. Set Stretch Type to Relative to Band Height. Check Stretch With Overflow box.
Click on Detail band and go to Properties. Uncheck Split allowed box. This will prevent from row splitting between 2 pages.
See the preview. Your report looks better now.