连接主报表和子报表 - 将对象列表传递到子报表

时间:2012-08-21 22:24:59

标签: java jasper-reports ireport

我正在使用 iReport 4.5.1 ,我遇到了连接主报告和子报告的问题。

我有两个对象:

  • 帐户(accountID,date,listOfParagraphs) - 所有字段都在主报告中定义;
  • 段落(account,paragraphNo,someObject) - 子报表( stavke.jrxml )中定义的所有字段。

listOfParagraphs 定义为 java.util.List<Paragraph> (对象列表),我想要将其传递给子报告。

我在主报告中定义了这个子报告的属性:

  • 子报告表达式 stavke.jasper
  • 表达式类 java.lang.String
  • 连接类型使用数据源表达式
  • 数据源表达
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{listOfParagraphs})

我收到此错误:

net.sf.jasperreports.engine.JRException: Error loading object from file : stavke.jasper
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:101)
at net.sf.jasperreports.engine.util.JRLoader.loadObjectFromLocation(JRLoader.java:253)
at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateReport(JRFillSubreport.java:301)
    .....

我已经为此努力了,但我无法解决它。

有人可以帮帮我吗?

@Vyccus(已编辑)

我改变了路径:

<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA["C:\\Documents and Settings\\D\\My Documents\\NetBeansProjects\\Project\\subreport\\"]]></defaultValueExpression>
</parameter>

<subreport>
            <reportElement x="0" y="42" width="555" height="100"/>
            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{ls})]]></dataSourceExpression>
            <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "stavke.jasper"]]></subreportExpression>
        </subreport>

现在我明白了:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.lang.Class.isAssignableFrom(Native Method)
at net.sf.jasperreports.engine.fill.JRFillTextField.getFormat(JRFillTextField.java:706)
at net.sf.jasperreports.engine.fill.JRFillTextField.evaluateText(JRFillTextField.java:394)
at net.sf.jasperreports.engine.fill.JRFillTextField.evaluate(JRFillTextField.java:368)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:258)
at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:499)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2036)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:760)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:270)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:128)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:946)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:864)
at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:613)
at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:59)
at net.sf.jasperreports.engine.fill.JRThreadSubreportRunner.run(JRThreadSubreportRunner.java:205)
at java.lang.Thread.run(Thread.java:662)

1 个答案:

答案 0 :(得分:2)

Java代码:

Map<String, Object> parameters = new HashMap<String, Object>();

//if you want this to be compiled in runtime

JasperReport jasperReport = JasperCompileManager.compileReport(your JRXMLReport path file here);
JasperReport subReport = JasperCompileManager.compileReport(your JRXMLSubReport path file here);

//you can notice that there're a lot of overloaded mathods to compile it, you can choose
//what is better in your case

//if you want to use the complied file *.jasper
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(new File("filenameReport.jasper"));

JasperReport subReport = (JasperReport)JRLoader.loadObject(new File("filenameSubReport.jasper"));


//Then you add your subReport in your parameter Map
parameters.put("SUBREPORT_JASPER_FILE", subReport);

JasperFillManager.fillReport(jasperReport, parameters,
                new JRBeanCollectionDataSource(yourList));

JRXML:

<parameter name="SUBREPORT_JASPER_FILE" class="java.lang.Object"/> // you can also create this using the option in your iReport


<subreport>
    <reportElement uuid="c62c7896-f37a-4e76-a4cb-70e2545947da" style="Style" x="279" y="0" width="296" height="15"/>
       <dataSourceExpression>
            <![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{yourSubList})]]>  
       </dataSourceExpression>
            <subreportExpression><![CDATA[$P{SUBREPORT_JASPER_FILE}]]></subreportExpression>
</subreport>

我认为你将完成你想要做的事