The Jaspersoft community forum is riddled with unanswered questions about how to use a CSV data source with a report.
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.
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.
The major steps include:
Create the CSV file as follows:
city,age,name,address,status "Dallas",47,"Janet Fuller","445 Upland Pl.","Trial" "Lyon",38,"Andrew Heiniger","347 College Av.","Active" "Dallas",43,"Susanne Smith","2 Upland Pl.","Active" "Berne",22,"Bill Ott","250 - 20th Ave.","Active" "Boston",32,"Michael Ott","339 College Av.","Trial" "Dallas",4,"Sylvia Ringer","365 College Av.","Active" "Boston",23,"Julia Heiniger","358 College Av.","Active" "Chicago",39,"Mary Karsen","202 College Av.","Active" "Dallas",40,"Susanne Miller","440 - 20th Ave.","Trial" "Berne",9,"James Schneider","277 Seventh Av.","Active" "Dallas",36,"John Steel","276 Upland Pl.","Suspended" "Chicago",35,"George Karsen","412 College Av.","Suspended" "Dallas",37,"Michael Clancy","19 Seventh Av.","Deleted" "Lyon",2,"Anne Miller","20 Upland Pl.","Active" "Dallas",0,"Laura Steel","429 Seventh Av.","Active" "Lyon",28,"Susanne White","74 - 20th Ave.","Deleted" "Paris",5,"Laura Miller","294 Seventh Av.","Active" "Lyon",17,"Laura Ott","443 Seventh Av.","Active" "New York",46,"Andrew May","172 Seventh Av.","Active" "New York",44,"Sylvia Ott","361 College Av.","Active" "Dallas",19,"Susanne Heiniger","86 - 20th Ave.","Active" "Chicago",11,"Julia White","412 Upland Pl.","Active" "Dallas",10,"Anne Fuller","135 Upland Pl.","Active" "New York",41,"Bill King","546 College Av.","Deleted" "Oslo",45,"Janet May","396 Seventh Av.","Active" "Paris",18,"Sylvia Fuller","158 - 20th Ave.","Trial" "San Francisco",48,"Robert White","549 Seventh Av.","Active" "Paris",25,"Sylvia Steel","269 College Av.","Suspended" "San Francisco",7,"James Peterson","231 Upland Pl.","Active" "Oslo",42,"Robert Ott","503 Sixth Av.","Trial"
data.csv
to a known location.The CSV file is created.
Create the data adapter as follows:
csv.xml
.CSV
data.csv
created in the previous section.The data adapter is created.
This section describes how to create the reports.
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 the subreport as follows:
subreport.jrxml
The subreport is created.
Create a data set from the CSV file as follows:
main.jrxml
) in Jaspersoft Studio.csv
csv.xml
.The CSV data set is created.
csv
The data set is connected.
Create fields in the main report as follows:
csv
.The report fields are created.
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: