在JasperReport中创建/传递Java bean数据源 [英] Creating/Passing Java bean Datasource in JasperReport
问题描述
我在我的JSF应用程序中使用JasperReport和ireport来动态生成报告。
这就是我想要实现的目标: -
I am using JasperReport and ireport in my JSF application to generate reports on the fly. This is what I am trying to achieve:-
-
我的结构(读作HashMap / ArrayList)包含数据需要传递给报告,以便在报告中显示相同内容。
My structure(read as HashMap/ArrayList) contains data that needs to be passed to the report so that the same is shown in report.
我的报告已经包含一个数据源连接,使用该连接我从中获取一些值数据库并在报告中填充它。
My report already contains a Datasource connection using which I am fetching some value from DB and populating it in report.
我正在创建一个子报表,以便对于需要从代码传递的数据,我可以使用子报表并嵌入此子报表在主要报告中。
I am creating a subreport so that for data which needs to be passed from code I can use the subreport and embed this subreport inside the main report.
我的问题是: -
1.我无法通过收藏(读作HashMap / ArrayList)子报告用我的代码中的数据填充它。
My problem is:- 1. I am unable to pass the collection(read as HashMap/ArrayList) to subreport to populate it with the data from my code.
我绝对肯定必须有一些方法将整个集合传递给子报告为了填充它,我也尝试创建一个JavaBean数据源连接但是在创建连接时它表示缺少类路径条目。
I am absolutely sure there must be some way of passing the entire collection to the subreport in order to populate it and I have also tried creating a JavaBean datasource connection however while creating a connection it says Missing classpath entry.
我无法捆绑jar中的各个类并将jar放在classpath中,因为值在结构中不断变化,....
I cannot bundle the respective classes inside a jar and put the jar in classpath since the values are constantly changing in the structure,....
任何人都可以指导我如何创建/将一个java bean数据源传递给报告,以便填充数据......
Can anyone please guide me on how to create/pass a java bean datasource to the report so that data can be populated...
请指导......
更新部分: -
将参数传递到报告并生成报告的Java代码: -
Java code for passing parameter to report and generating the report:-
public class TestDataSource
{
public static void main(String[] args)
{
try {
JasperDesign jasperDesign = JRXmlLoader.load("D:\\jasperReports\\subReportDataSource.jrxml");
JasperReport jasperReport =(JasperReport)JasperCompileManager.compileReport(jasperDesign);
Map<String,Object> parameters = new HashMap<String,Object>();
parameters.put ("Title",generateCollection());
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JRBeanCollectionDataSource(generateCollection()));
JasperViewer.viewReport(jasperPrint);
}catch(Exception e)
{
e.printStackTrace();
}
}
public static ArrayList<PersonBean> generateCollection()
{
ArrayList<PersonBean> arrlist=new ArrayList<PersonBean>();
arrlist.add(new PersonBean("A", 20));
arrlist.add(new PersonBean("B",30));
arrlist.add(new PersonBean("C",40));
arrlist.add(new PersonBean("D",50));
arrlist.add(new PersonBean("E",40));
arrlist.add(new PersonBean("F",60));
return arrlist;
}
}
现在我创建了一个新的报告(Report)..在里面我放了一个子报告(子报告)..配置子报告数据源为
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource ($ P {Title})
Now I created a new report (Report)..Inside that I placed a sub-report(Sub-Report)..Configured the sub-report datasource to be new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{Title})
连接类型: - 使用数据源连接类型。
Connection Type:-Use a datasource connection type.
现在我的内部子报告我刚刚放置了两个静态字段作为名称和年龄。
如何告诉我的报告/子报告打印正在传递的hashmap中的值。
Now inside my Sub-Report I just placed two static fields as Name and Age. How do i Tell my report/Sub-Report to print value present as value in hashmap which is being passed.
推荐答案
如果您已经有一个DataSource,那么当您填写报告时,您可以通过参数Map传递列表/地图。
If you already have a DataSource then you can pass your List/Map thourgh the param Map when you're filling your report.
Map<String, Object> param = new HashMap<String, Object>();
param.put("SUB_DATA_SOURCE", yourList);
JasperFillManager.fillReport(jasperReport, param,
new JRBeanCollectionDataSource(yourMainListHere));
这样你必须在MAIN报告中创建一个参数,其名称与您在param Map并给它一个Type类(在我的例子中是List)。
Doing that you have to create a parameter inside your MAIN report with the same name you have set in your param Map and also give to this a Type Class (List in my case).
比你必须创建子报表元素并将连接类型设置为使用数据源表达式和数据源表达式中你设置了这个:
Than you have to create your subreport element and set the Connection Type as "Use a datasource expression" and inside the "Data Source Expression" you set this:
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{SUB_DATA_SOURCE})
这篇关于在JasperReport中创建/传递Java bean数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!