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?
In the Navigation Pane, right-click the report to which you want to add a subreport, and then click Design View. In the menu that appears, ensure that Use Control Wizards is selected. Open the Controls Gallery again, and then click Subform/Subreport. On the report, click where you want to place the subreport.
JasperReports is an open source Java reporting tool that can write to a variety of targets, such as: screen, a printer, into PDF, HTML, Microsoft Excel, RTF, ODT, comma-separated values (CSV) or XML files. JasperReports.
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With