Java jasper 报告列表作为数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3958231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java jasper reports list as data source
提问by d-man
I want to make list object as my data source, can you tell me for .jrxml
file how should i design my report ?
我想将列表对象作为我的数据源,你能告诉我.jrxml
文件我应该如何设计我的报告吗?
public class TestReport
{
public void runReport(String fileName, String outFileName)
{
try
{
List<R> list = new ArrayList<R>(5);
Map parameters = new HashMap();
list.add(new R("a1" ,"a2"));
list.add(new R("b1" ,"b2"));
list.add(new R("c1" ,"c2"));
/*parameters.put("my_name", "faisal khan");
parameters.put("my_addr", "address comes here");*/
JasperPrint print = JasperFillManager.fillReport( fileName, parameters, new JREmptyDataSource());
JRExporter exporter = new JRPdfExporter();
exporter.setParameter(
JRExporterParameter.OUTPUT_FILE_NAME,outFileName);
exporter.setParameter(
JRExporterParameter.JASPER_PRINT, print);
JasperExportManager.exportReportToPdfFile(print, outFileName);
print = null;
exporter = null;
} catch (Exception e) {
e.printStackTrace();
}
}
private class R{
private String name;
private String addr;
public R(String name, String addr) {
super();
this.name = name;
this.addr = addr;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}
public static void main(String args[]){
new TestReport().runReport("/home/faisalloe/ireports/report1.jasper", "/home/faisalloe/ireports/report1.pdf");
}
}
回答by Tomas Narros
Make use of the JasperDatasource for collections: net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
使用 JasperDatasource 进行集合:net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
JasperPrint print = JasperFillManager.fillReport( fileName, parameters, new JRBeanCollectionDataSource(list));
Your report will repeat once for each element. Or you can define a subreport depending the main report, wich is to be repeated once per list element.
您的报告将对每个元素重复一次。或者您可以根据主报告定义子报告,每个列表元素重复一次。