在Jasper Report中使用多个JavaBean DataSource

时间:2012-07-25 12:23:22

标签: jasper-reports osgi

由于JasperReport可以将JavaBean集合作为DataSource。我们可以发送一个SetCollection,其中包含一个引用多个SetCollections的对象。 并且每次传递不同的SetCollection时,使用这些引用传递给已编译的jrxml文件,使用对JasperFillManager.fillReport()的mulitple调用。 只是想知道编译的jrxml文件是否会被最后一次调用填充,或者是否会有每次调用JasperFillManager.fillReport()的数据。

2 个答案:

答案 0 :(得分:5)

这将填充最后一个。您不能以这种方式使用多个数据源。虽然还有其他方法可以做到这一点。通过param Map传递集合就是其中之一。

答案 1 :(得分:2)

最后!! 完成任务。 感谢@Vycuss,因为我只使用子报表实现了它。

mainreport = JasperCompileManager.compileReport(//path of mainreport.jrxml);
subreport = JasperCompileManager.compileReport(//path of subreport.jrxml);
Map<String, Object> params = new HashMap<String, Object>();
params.put("SUB_REPORT", subreport);
params.put("DATA_SOURCE", empService.getEmpProject());

jprint1 = JasperFillManager.fillReport(mainreport,params,new JRBeanCollectionDataSource(empService.getEmpBean()));
JasperExportManager.exportReportToPdfFile(jprint1, "C://Test.pdf");

方法: 在Activator.java中的上述代码之后我创建了一个OSGi包。 在MainReport.jrxml中创建两个参数 第一个是没有类型的“SUB_REPORT”,第二个是带有java.util.collection类型的“DATA_SOURCE”。 现在在MainReport中提供 new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($ P {DATA_SOURCE}) 作为放置在详细信息区域中的子报表元素的数据源表达式。 还要确保子报表只有列标题,详细信息和列页脚带。 此外,还将为javabeans的相应成员创建子报表中的字段。

相关问题