[英]How to dynamically add multiple pieces of data in Jasper Reports
我有一个用Java处理的Jasper报告。
在Java中,我可以使用以下代码用数据动态填充Bean:
List<ThemeBean> themes = new ArrayList<ThemeBean>();
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
List<String[]> data = csvReader.readAll();
for(String[] d : data) {
ThemeBean tb = new ThemeBean();
tb.setThemes(d[0]);
tb.setComments(d[1]);
tb.setSentiment(d[2]);
themes.add(tb);
}
JasperDesign jasperDesign = JRXmlLoader.load(fileName);
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(themes);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, ds);
当.jrxml文件在标签中指定了正确的字段且textFieldExpression时,此方法效果很好。
使用构建的列表动态填充以下内容:
<textFieldExpression><![CDATA[$F{themes}]]></textFieldExpression>
我的问题是弄清楚如何在同一报表中为两个不同的表动态地执行此操作。 看来我只能使用一次迭代来动态添加数据。 我正在尝试实现一个结果,即在同一报告中生成了两个完全不同的表。 如果这个问题不清楚,请告诉我,我将尝试解决。 谢谢。
贾斯珀(Jasper)意识形态:一份报告-一份数据来源。
在主报表中使用2个子报表。 Main.jrxml包含subreport1(用于显示List <ThemeBean>)和subreport2(用于List <AnotherBean>)。 Subreport1和Subreport2放入标题(或摘要)区域。
在Main.jrxml中定义参数:themesPar,anotherPar作为java.util.Collection
将集合作为参数放入主报告中:
List<ThemeBean> themes = new ArrayList<ThemeBean>();
themes.add(...);
...
List<AnotherBean> blablas = new ArrayList<AnotherBean>();
blablas.add(...);
...
HashMap<String, Object> parameters = new HashMap<String, Object>();
parameters.put("themesPar", themes);
parameters.put("anotherPar", blablas);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
对于主报表中的subreport1数据源表达式,请编写如下内容
new JRBeanCollectionDataSource($P{themesPar})
并对subreport2使用与$ P {anotherPar}相同的方式。 在两种情况下都为子报表设置
Connection Type = "Use a datasource expression"
为什么任何新手都会问这个问题??? :)为什么在jasperforge FAQ中不提供此解决方案?
您可以通过创建另一个包含beanCollection的bean将多个bean集合传递给报表。 这样的bean看起来像这样:
public class DataBean {
private Collection beanCollection = null;
public DataBean() {
}
public Collection getBeanCollection() {
return beanCollection;
}
public void setBeanCollection(Collection beanCollection) {
this.beanCollection = beanCollection;
}
}
我假设其他集合的类型将不同于ThemeBean
。 我以FruitBean
为例。 对于要传递的每个bean集合,创建一个DataBean
并将其添加到集合中,然后将该集合传递到报表中。 因此,您的代码将变为如下所示:
List<DataBean> allData = new ArrayList<DataBean>();
//...Create and populate `List<ThemeBean> themes`
DataBean db = new DataBean();
db.setBeanCollection(themes);
allData.add(db);
//...Create and populate `List<FruitBean> fruits`
db = new DataBean();
db.setBeanCollection(fruits);
allData.add(db);
//... Load the report
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(allData);
//... Fill the report
用此数据源填充的报告将是一个外壳报告,其中包含您创建的每个DataBean
的子报告。 此报告中只有一个字段可用: $F{beanCollection}
将子报表的数据源表达式设置为
new JRBeanCollectionDataSource($F{beanCollection})
。
然后,子报表中将提供ThemeBean
或FruitBean
的字段。
如果您更喜欢使用列表组件而不是子报表,那也可以。
贾斯珀(Jasper)意识形态:一份报告-一份数据来源。
在主报表中使用2个子报表。 Main.jrxml包含subreport1(用于显示列表)和subreport2(用于列表)。 Subreport1和Subreport2放入标题(或摘要)区域。
在Main.jrxml中定义参数:themesPar,anotherPar作为java.util.Collection
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.