JasperReport/iReport useful basic technique.

When I do reporting. There are some basic things that one commonly will do.

No matter I am using Crystal Report or Jasper Report of other reporting tools.

1. some statistic result, usually I do this with parameter passing.

2. some mapping to make the data more human readable.

3. sub-report dataset. a little complex, not discuss in this post.

The first one is the easiest in Jasper Report. Just pass a parameter map when you fill the report with data.
HashMap paramMap = new HashMap();
reportInstance = JasperCompileManager.compileReport("c:/jasperXml/admissionReport0613.jrxml");
jasperPrint = JasperFillManager.fillReport(reportInstance,paramMap,new JRBeanCollectionDataSource(attendanceList));

This pass the number of records as a parameter. Many report have this kind of field.

Then you can get is in the report by $P{keyOfMap}.

2. the second one is a little bit tricky. Due to iReport not so clear importing and classpath handling.

I am still not very sure which step(s) make this right. but I will tell you all that I’ve done to make it work.

i. you need to have a class with a static method to do the transformation for you. for example

MyFormatter.toStatusString(int statusInt);

ii. compile this .java into .class. and then add the directory containing .java and .class into iReport classpath.

do this by “Option->Classpath->add folder”.

for example adding “project\build\classes” and “project\src\package\”

iii. also add the package containing that class by “Edit->Import Report Directives->New Import”

example “my.package” where MyFormatter is in my.package.MyFormatter

iv. define an expression “MyFormatter.toStatusString($F{state})”

v. compile and save the report. If you Execute (with active connection), you may get a ClassDefNotFoundException.

vi. add “import my.package.MyFormatter” into the class where you generate the JasperPrint object.

After these steps, I finally can generate a report with ClassDefNotFoundException.

God Bless.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s