I know how to generate jasper report without any subreport. But currently I have a subreport in my report and I would like to know how can I compile that subreport in java?
Asked
Active
Viewed 4.6k times
1 Answers
49
You can compile the subreport like the simple report - with help of JasperCompileManager.compileReport(java.lang.String sourceFileName) method, for example.
After that you can pass the compiled subreport as parameter to the master report.
The sample:
JasperReport jasperMasterReport = JasperCompileManager.compileReport(masterReportSource);
JasperReport jasperSubReport = JasperCompileManager.compileReport(subReportSource);
Map<String, Object> parameters = new HashMap()<String, Object>;
parameters.put("subreportParameter", jasperSubReport);
JasperFillManager.fillReportToFile(jasperMasterReport, parameters, outputFileName, connection);
The snippet from the master report's jrxml file (sample):
<parameter name="subreportParameter" class="net.sf.jasperreports.engine.JasperReport"/>
...
<detail>
<band height="50">
...
<subreport>
<reportElement isPrintRepeatedValues="false" x="5" y="25" width="325" height="20" isRemoveLineWhenBlank="true" backcolor="#ffcc99"/>
<subreportParameter name="City">
<subreportParameterExpression><![CDATA[$F{City}]]></subreportParameterExpression>
</subreportParameter>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<returnValue subreportVariable="PriceSum" toVariable="ProductTotalPrice" calculation="Sum"/>
<subreportExpression class="net.sf.jasperreports.engine.JasperReport"><![CDATA[$P{subreportParameter}]]></subreportExpression>
</subreport>
Notes
I mentioned an old API for generating result: JasperFillManager.fillReportToFile(JasperReport, String, Map, java.sql.Connection)
In case using JasperReports 6.x it is better to use exporters (concrete implementation of net.sf.jasperreports.export.Exporter interface, for example JRPdfExporter) for generating output file
The example how to use Exporter right can be found here

Alex K
- 22,315
- 19
- 108
- 236
-
Can I make value of x,y,width,height dynamic? – user1791574 Jul 16 '13 at 08:23
-
@VivekYadav Yes, with help of Java API – Alex K Jul 16 '13 at 08:26
-
I was trying but did not get success. Can you please post an example that how can I do it? – user1791574 Jul 16 '13 at 08:28
-
@VivekYadav You can find samples [here](http://dynamicjasper.com/documentation-examples/how-to-2/) or [here](http://jasperreports.sourceforge.net/sample.reference/noxmldesign/index.html) – Alex K Jul 16 '13 at 08:34
-
@AlexK thanx for your reply. Its really looking great stuff. I am trying this code on grails but get some problem to implement it properly. Is there any suggestion how to integrate this code with grails. Because its written in purly in java and some code of create problem for implement it properly. – user1791574 Jul 16 '13 at 11:15
-
@VivekYadav If you have the question to ask - you can post it. – Alex K Jul 16 '13 at 11:20
-
1`How can we add multiple sub report in master Dynamically ? ?` – HybrisHelp Sep 02 '13 at 11:51
-
I added the dataSourceExpression tag (and remove connectionExpression) in my xml and now its working – atott Dec 21 '13 at 12:50
-
Whatis connection? – ShadowGames Oct 17 '18 at 13:15
-
1@ShadowGames I added info at answer – Alex K Oct 17 '18 at 19:28