您的问题与
How do I print a list of strings contained within another list in iReport?问题重复,与
Passing the List of primitive type objects as datasource for subreport问题相关。
这是工作样本。
此示例的要点:
>使用_THIS表达式;
>使用List(jr:list)组件在Detail band中
生成报告的代码片段:
public static void testBuildPdf() {
try {
Map params = new HashMap();
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, getDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
private static JRDataSource getDataSource() {
Collection coll = new ArrayList();
BeanWithList bean = new BeanWithList(Arrays.asList("London", "Paris"), 1);
coll.add(bean);
bean = new BeanWithList(Arrays.asList("London", "Madrid", "Moscow"), 2);
coll.add(bean);
bean = new BeanWithList(Arrays.asList("Rome"), 3);
coll.add(bean);
return new JRBeanCollectionDataSource(coll);
}
JavaBean代码:
public class BeanWithList {
private List m_cities;
private Integer m_id;
public BeanWithList(List cities, Integer id) {
m_cities = cities;
m_id = id;
}
public List getCities() {
return m_cities;
}
public Integer getId() {
return m_id;
}
}
jrxml文件:
结果将是: