Background
The Jaspersoft community forum is riddled with unanswered questions about how to use a CSV data source with a report.
Problem
When using Jaspersoft Studio, it is convenient to use a local file as a data source. The instructions for accomplishing this task are piecemeal at best and incomplete at worst.
Question
What are the steps required to use a CSV file (data source) with a main report and have that data be used by a subreport?
Note: This solution drops the first result from the list in the subreport, which requires a workaround to resolve, which is not described in this answer.
Overview
The major steps include:
Create CSV file
Create the CSV file as follows:
data.csv
to a known location.The CSV file is created.
Create Data Adapter
Create the data adapter as follows:
csv.xml
.CSV
data.csv
created in the previous section.The data adapter is created.
Create main report and subreport
This section describes how to create the reports.
Create main report
Create the main report as follows:
main.jrxml
csv.xml
.The main report is created.
Note that the data source is not yet connected to the main report. This is where people run into trouble. Assigning the CSV data source does not mean that the report can "query" the data. An additional step is required, described later in this answer.
Create subreport
Create the subreport as follows:
subreport.jrxml
The subreport is created.
Create data set
Create a data set from the CSV file as follows:
main.jrxml
) in Jaspersoft Studio.csv
csv.xml
.The CSV data set is created.
Connect data set
csv
The data set is connected.
Create report fields
Create fields in the main report as follows:
csv
.The report fields are created.
Connect subreport
Connect the subreport to the main report as follows:
main.jrxml
report.subreport.jrxml
report.main.jrxml
report.$P{REPORT_DATA_SOURCE}
.The output resembles: