Rodiq » Page 'Subreports in jasper (without sql)'

Subreports in jasper (without sql)

I had to create a report having two detail sections, having both the same datasource. The solution was to create a subreport included in the initial report.

For it to work:

The subreport

– I created a subreport having as initial dimensions exactly the size of the rectangle where the subreport is added in the report, and with 0 margins.

– in the subreport I used the same field names as in the report

Including the subreport in the main report

In the initial report, the subreport is received from the application as a parameter. Also the datasource for the subreport is received from the application as a parameter. Like this, I avoided writing sql in the subreport.

<parameter name=”ProductsSubreport” isForPrompting=”false” class=”net.sf.jasperreports.engine.JasperReport”/>

<parameter name=”datasource” isForPrompting=”false” class=””/>

And it is added like this in the main report:

<band height=”50″ isSplitAllowed=”true” >
<subreport isUsingCache=”true”>
<subreportExpression class=”net.sf.jasperreports.engine.JasperReport”><![CDATA[$P{ProductsSubreport}]]></subreportExpression>
Using iReports:

subreport common

subreport tab

subreport other tab

Running report and subreport in the application:

//filling the bean collection and the parameters for the main report

Collection beanCollection = oferteManager.getOfertaPrintabilaBeanCollection(oferta);
Map parameters = oferteManager.getOfertaPrintabilaParameters(oferta, null, user);

//the subreport datasource parameter in this case is the same bean collection as for the main report

parameters.put(“datasource”, new JRBeanCollectionDataSource(beanCollection));

//the subreport is loaded from classpath and sent as a parameter to the main report
InputStream productsStream = getClass().getResourceAsStream(“/ro/nit/vanzari/oferte/Products.jasper”);
JasperReport subreport = (JasperReport)JRLoader.loadObject(productsStream);
parameters.put(“ProductsSubreport”, subreport);
//exporting the main report (this code is the same, even without a subreport):

JasperPrint jasperPrint = JasperFillManager.fillReport(ofertaStream, parameters, new JRBeanCollectionDataSource(beanCollection));
JRPdfExporter exporter = new JRPdfExporter();

//the fontmap is usefull for styled text in textfields

Map fontMap = new HashMap();
fontMap.put(new FontKey(“Arial”, true, false), new PdfFont(“Helvetica-Bold”, “Cp1252”, false));
fontMap.put(new FontKey(“Arial”, false, true), new PdfFont(“Helvetica-Oblique”, “Cp1252”, false));
fontMap.put(new FontKey(“Arial”, true, true), new PdfFont(“Helvetica-BoldOblique”, “Cp1252”, false));

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, output);
exporter.setParameter(JRExporterParameter.FONT_MAP, fontMap);


Share and Enjoy:
  • Digg
  • StumbleUpon
  • Facebook
  • Google Bookmarks
  • Reddit
  • RSS

14 comments to “Subreports in jasper (without sql)”

  1. Hello,
    Can you please post the source code of subreports in jasper. I have followed all the steps but don’t work.
    I have not found any other similar example.

  2. hi, your article is very useful thanks for your posting, good luck. Btw sorry for my poor english

  3. Hi all

    I am facing a problem in my subreport

    I have a main report which i populate by JRBeanCollectionDataSource by bean A
    and the subreport report i populate by JRBeanCollectionDataSource by bean B

    but I am not able to see the subreport , how can we do this

    please help .

    Kind Regards

    Nasir Qureshi

  4. thank u so much

  5. It is possible to “connect” the main report and subreport ? I mean, the way you say here ( with the datasource from the code), with two different datasources ? I mean, for an id field of the main report there are some subreport records to display.


  6. Thanks you so much for this post. That is exactly what i am looking for. i have done the above similar, but i get the wrong result:
    In the master report, the subreport’s detail display the last Map element in “datasource” Collection. It seems, the ds has been iterated be for the subreport wants it to iterate…Dear Rodiq, Have you got what i mean, can you help me with this? Many Thanks.
    My Email is:

  7. Dear John, I did not encounter that case before, but please try the jasper forums, they are a good source of inspiration.

    And to Liu: I did not have this problem, maybe the jasper forums will give you a solution too,

    And thank you for the comments!

  8. hi can u have example where in the data source of my master and subreport are different?tnx

  9. Rodiq, your post is excellent! I have searched an example like this a long time. Thank’s.

  10. Buna!

    Din articol mi-am dat seama ca esti roman asa ca iti scriu pe limba noastra.

    Multumesc foarte mult pentru solutia ta m-a ajutat foarte mult. Este exact ce cautam.

  11. cu placere!

  12. This is excellent example but i still can’t it to work pls can you post me the source code?


  13. Come here so I can kiss you! Thank you so much for posting this, it helped me solve my problem.

  14. you’re welcome 🙂

Leave a comment

× 6 = thirty