Subreports in Jasper Reports

2020-03-07 06:24发布

问题:

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 googled for implementing this task.

At that time I came across sub-reports concept, but I didn't get much info for how to implement sub-reports.

I have an idea of how to create sub-reports. But I don't know how to fill sub reports with customdatasource. That's my question. Please help me in achieving this.

Note: I tried to implement subreports, but I am getting an 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 an error while filling it through masterreports. Please help me to compile the report.

Stack trace of the 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:

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

Using iReport with eclipse to generate reports approperly



回答2:

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);
       }
   }


回答3:

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.