views:

1076

answers:

3

Hi all,

I have two tables to be printed in same page. Data for those tables will be supplied from a map from a custom data source. I need to implement it. I Google for implementing this task.

At that time i came across sub-reports concept. but didnt get much tutorial for how to implement sub-reports.

I have a idea of how to create sub-reports. but i dont know how to fill sub reports with customdatasource. Thats my question. Pls help me in achieving.

Thanks in advance

Note: Hi guys, i tried to implement subreports. but i am getting error. here is the code i used for compiling subreports.

    JasperPrint print=new JasperPrint();
    JRPdfExporter exporter=new JRPdfExporter();
    JasperDesign design,design1;
    JasperReport report,report1;
    JasperReport jasperReport;      
    JasperDesign jd1;

            jd1 = JRXmlLoader.load("D:\\sub_report1.jrxml");
    jasperReport = JasperCompileManager.compileReport(jd1);
    Map parameters = new HashMap();
    parameters.put("ReportTitle", "Table Report");
    parameters.put("DataFile", "TableSource.java");
    parameters.put("Subreport_1", jasperReport);

    design= JRXmlLoader.load("D:\\sub_report2.jrxml");
    report=JasperCompileManager.compileReport(design);
    parameters.put("Subreport_2", report);

    design1=JRXmlLoader.load("D:\\master_report.jrxml");
    report1=JasperCompileManager.compileReport(design1);
                  JasperFillManager.fillReportToFile(report1,"D:\\master_report.jrprint",parameters, new TableDataSource());

        exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
        exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "D:K\\Report.pdf");         
        exporter.exportReport();

        JasperViewer.viewReport(print);

Here i have compiled subreport1 & subreport2 individually and added them as a parameter in map. then i m getting error while filling it through masterreports. pls help me to compile the report

Stack trace of compiler error :

Exception in thread "main" net.sf.jasperreports.engine.JRRuntimeException: Unkown print order 0. at net.sf.jasperreports.engine.fill.JRFillSubreport.initSubreportFiller(Unknown Source) at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(Unknown Source) at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(Unknown Source) at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(Unknown Source) at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(Unknown Source) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(Unknown Source) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(Unknown Source) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(Unknown Source) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(Unknown Source) at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(Unknown Source) at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(Unknown Source) at net.sf.jasperreports.engine.fill.JRFiller.fillReport(Unknown Source) at net.sf.jasperreports.engine.JasperFillManager.fillReport(Unknown Source) at net.sf.jasperreports.engine.JasperFillManager.fillReportToFile(Unknown Source) at ReportTableCompiler.main(ReportTableCompiler.java:53)

+1  A: 

I posted the answer for the similar question. You can check the answer here.

http://stackoverflow.com/questions/2812614/using-ireport-with-eclipse-to-generate-reports-approperly/2988567#2988567

Please look the content here also

http://www.itjunky.info/programming/reports-gaphics/creating-subreport-with-jasperreport/

Multiplexer
u have specified code for filling a single report. but my question is that how to handle multiple reports. i.e. subreports ?
Karthikeyan
Subreport is not different from the actual report. You have to compile the sub report before it generates the report. So you can use the same code base to populate other reports(subreports).
Multiplexer
BTW that code does not tell you how to use custom datasource. I gave that code for the ones who have some queries to populate the reports.
Multiplexer
@purusotham: i have modifed my question pls help in solving t..
Karthikeyan
@Karthikeyan:What is the compiler error says ?
Multiplexer
@purusotham: I have included the stack trace
Karthikeyan
@ purusotham : i have solved that error. that prob is becoz of higher version of iReports. now i m getting some other error. can u understand t ?. i have included that stack trace
Karthikeyan
@Karthikeyan: I have some questions here. 1. What does TableDataSource class contains ?2. Do you have these two sub reports placed in the master report ?3. What do you want to see as a final result ? A PDF or JRPrint ?I have next set of questions also. I will ask them based on your answers for these.
Multiplexer
@Karthikeyan: Please check which output you want. You code base contains for both PDF as well as JRPrint. Please make it either PDF or JRPrint.
Multiplexer
@purusotham: My intention is to fill jasper with TableDatasource class and then export the output jrprint into pdf.
Karthikeyan
Karthikeyan
@purusotham: i have kept those subreports inside master report. (if i double click on subreports icon in master report its opening corresponding subreport)
Karthikeyan
I have a suggestion if it is Ok with you. I am not able to recall why that runtime exception occurred offhand. Just go step by step.First try to get the PDF of your first sub report. If there are any issues try to fix them. Try to generate the first sub report successfully. Later think of combining all the reports.
Multiplexer
sure.. i ll try that....
Karthikeyan
@Purusotham: I have tried it individually..Its working gr8.. with no issues.... now i don know how to compile it with master programmatic ally. !!
Karthikeyan
Can u tell me steps to compile master report ? (i.e. whole reports with 2 sub reports)
Karthikeyan
Have a look at this http://www.itjunky.info/programming/reports-gaphics/creating-subreport-with-jasperreport/
Multiplexer
i m not able to open the link.. that link is not loading.(loading..........) can u pls post d content of t as answer here ?
Karthikeyan
Look into the answers section. I posted the link there.
Multiplexer
A: 

Sub reports are just like any other normal reports. They have own JRXML file and use own data sources. You can invoke the subreport alone or inside a master report, using the sub report tool. The data source in this case is specified in the master JRXML, the sub report properties dialog, Subreport tab, and it is different than the master's report data source.

I don't completely understand what are you trying to do, but you can also supply parameters to the sub report, so it filters its data based on the current row from the master table.

m_pGladiator
@m_pGladiato: i have modified my question pls help me solving this issue
Karthikeyan
A: 

If you have only one detail band and your subreportExpression is something like "SUBREPORT_1.jasper" you can use this to compile several subreports

   JRDesignBand jrBand = (JRDesignBand) jasperDesign.getDetailSection().getBands()[0];
   JRElement[] jrElements = jrBand.getElements();
   for (JRElement jrElement : jrElements) {
       if (jrElement instanceof JRDesignSubreport) {
       JRDesignSubreport subReportDesign = (JRDesignSubreport) jrElement;
       JRExpression jrExpression = subReportDesign.getExpression();
       String file = jrExpression.getText();
       file = file.substring(1, file.length()-8)+".jrxml";
       JasperCompileManager.compileReport(file);
       }
   }
Cadu