Connect master report and subreport - passing list of objects to subreport

纵然是瞬间 提交于 2019-12-06 00:57:19

Java Code:

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>

I think that way you'll accomplish what you're trying to do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!