天天看点

jasperreport java数据,在JasperReport中创建/传递Java bean数据源

jasperreport java数据,在JasperReport中创建/传递Java bean数据源

I am using JasperReport and ireport in my JSF application to generate reports on the fly.

This is what I am trying to achieve:-

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.

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.

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.

I cannot bundle the respective classes inside a jar and put the jar in classpath since the values are constantly changing in the structure,....

Can anyone please guide me on how to create/pass a java bean datasource to the report so that data can be populated...

Kindly guide...

Updated portion:-

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 parameters = new HashMap();

parameters.put ("Title",generateCollection());

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JRBeanCollectionDataSource(generateCollection()));

JasperViewer.viewReport(jasperPrint);

}catch(Exception e)

{

e.printStackTrace();

}

}

public static ArrayList generateCollection()

{

ArrayList arrlist=new ArrayList();

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;

}

}

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.

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.

解决方案

If you already have a DataSource then you can pass your List/Map thourgh the param Map when you're filling your report.

Map param = new HashMap();

param.put("SUB_DATA_SOURCE", yourList);

JasperFillManager.fillReport(jasperReport, param,

new JRBeanCollectionDataSource(yourMainListHere));

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})