Monday, September 29, 2014

Row counting with JasperReport

JasperReport has simple way to count row in report. In this report I will tell about counting records from main and sub-report. I take for base report from "Using JavaBean datasource for report and subreport in iReport" article and add necessary changes for obtain next result:
Picture 1: Line numbering result.

In main report (java_beans_datasource_report.jrxml in this example) add internal variable REPORT_COUNT to Detail1 band:

Picture 2: Add REPORT_COUNT variavle to master report.
Repeat this one for subreport (java_beans_datasource_report_subreport1.jrxml).
Picture 3: Numbering row in subreport.
 After compile and preview it you must see numbering record in master and subreport.

For obtain total records count from main report and subreport we must add two variables masterRecordCounter and detailRecordCounter. Set next parameters for its:
  • masterRecordCounterVariable Class: java.lang.Integer; Calculation: Count; Reset type: Report; Increment type: None; Variable Expression: $V{REPORT_COUNT}
  • detailRecordCounterVariable Class: java.lang.Integer; Calculation: Count; Reset type: Report; Increment type: None
Now REPORT_COUNT of master report accumulating in masterRecordCounter but for detailRecordCounter we can bind it with REPORT_COUNT from subreport. For it select Subreport component and change Return values parameter to this maner:
Picture 4: Binding subreport variables.
Enable Summary band and add  to it masterRecordCounter and detailRecordCounter as shown on Picture 2, compile and enjoy of result.

Related posts:


You can download  sources with samples from BitBucket.