jasper-reports

Fill report

Parameters#

Parameters Column
jasperPrint The output of the fill process that can be exported to desired format
reportTemplate The compiled design file .jasper
parameters The parameter Map, that if defined can be references inside report by $P{key}
datasource A net.sf.jasperreports.engine.JRDataSource
connection A database connection java.sql.Connection

With IDE (Integrated development environment)

JasperSoft Studio

  1. If datasource or database connection is needed to fill report, create your Data Adapter

in Repository Explorer by right clicking “Data Adapters” selecting “Create Data Adapter”

  1. Enter preview mode by selecting the Preview tab (no errors in deign need to be present)

  2. Select desired dastasource (if no datasource is required select “One Empty Record”

  3. Set parameter as desired

  4. Fill report by clicking the green arrow “Run the report”

Fill report

Fill JasperReport Template using Java

Common Requirements

All reports, regardless of how the data is presented, take a path to the report template and a parameter map. The variables are used in all examples that follow:

// Parameters passed into the report.
Map<String, Object> parameters = new HashMap<>();

// Arbitrary parameter passed into the report.
parameters.put("KEY", "Value");

// The compiled report design.
String path = "path/to/template.jasper";

Using a .jrxml file incurs an extra compilation step that isn’t necessary in most situations. Unless you’ve written custom software to change the .jrxml before the report runs (e.g., adding or removing columns dynamically), use the .jasper file as shown in the subsequent examples.

Using a Database Connection

// Establish a database connection.
Connection connection = DriverManager.getConnection(url, username, password); 

// Fill the report, get the JasperPrint that can be exported to desired format.
JasperPrint jasperPrint = JasperFillManager.fillReport(
  path, parameters, connection); 

Using a Custom Data Source

// Populate this list of beans as per your requirements.
List<Bean> beans = new ArrayList<>();

// Wrap the beans in a beans in a JRBeanCollectionDataSource.
JRBeanCollectionDataSource datasource = new JRBeanCollectionDataSource(beans);

// Fill the report, get the JasperPrint that can be exported to desired format.
JasperPrint jasperPrint = JasperFillManager.fillReport(
  path, parameters, datasource);

Without Data Source, unused Detail Band

// Fill the report, get the JasperPrint that can be exported to desired format.
JasperPrint jasperPrint = JasperFillManager.fillReport(path, parameters);

Without a datas ource, the attribute whenNoDataType="AllSectionsNoDetail" on the JasperReport element must be set, otherwise an empty (blank) report will be generated.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow