Jump to content
Changes to the Jaspersoft community edition download ×

How to create a report from data in a hashmap


nedPhillips

Recommended Posts

I need to create a PDF report from data stored in a HashMap. The keys are the columns, the values are the column values. Pretty straight forward.

 

Please do not tell me to look at the Samples. The samples have been some help but not a complete solution.

 

Do I need to use a JRDataSource?

 

I want to compile a jasper file in iReport. How do I setup my Custom DataSource in iReport?

 

What other JR Classes to I need to use? fillReportToFile? which signature?

 

exportReportToPdf?

 

Anything else?

 

I would love to see a solid example showing how to do this with iReport and the jasperreports api.

 

Thanks

the contents of my HashMap.

Link to comment
Share on other sites

  • Replies 8
  • Created
  • Last Reply

Top Posters In This Topic

Here is some additional information.

 

I'm using iReport and Jasperreports API both versions are 1.2.7.

 

I think I need to create either a JRDataSource or a JRDataSourceProvider class. A Factory class with a static method (e.g. getDataSource) that takes no parameters.

 

As I stated in my first post. I have a Hashmap class with the key values being the column (field) names for the report, and the Hashmap values contain the information for those key/column/field names.

 

Do I need to pass the Hashmap into my JRDataSource/JRDataSourceProvider class ?

 

How do I setup my Custom DataSource in iReport. Do I use an empty DataSource? How is that done?

 

Note: I do not need to hit the database since I've already pulled the data into my Hashmap.

 

This does not seem to be a difficult report to implement, however, I have not had any luck following the jasperreport samples or searching on the internet for answers.

 

Thanks for any help provided.

Link to comment
Share on other sites

This seems to be more of an iReport related question. Have you tried posting on the iReport forum?

 

I'm not an iReport expert, but I guess you do have to use either a Custom JRDataSource or a JRDataSourceProvider. The code would have to fetch your data HashMap and create a JR data source based on it. For instance, a Custom JRDataSource factory class would do

Code:

public class MyDataSourceFactory {
public JRDataSource createDatasource() {
HashMap data = ..fetch the data map..;
return new JRMapArrayDataSource(new Map[]{data});
}
}

 

After you have the class, you'll have to create the iReport data source (via Data -> Connections/Data Sources), and use it as "active connection" when running the report.

 

HTH,

Lucian

Link to comment
Share on other sites

Hi,

 

I suspect you passed the HaspMap containing your records in the place for the report parameters map. If so, no wonder it does not work, as the report will run without a data source and will result in an empty document.

There is a separate argument in the method for passing the data source.

You'll probably have to wrap the values of the map in a JRBeanCollectionDataSource instance.

If you need more details about using data sources, check the /demo/samples/datasource sample provided with the project.

 

I hope this helps.

Teodor

Link to comment
Share on other sites

I have tried it using CustomDataSource and Custombeanfactory

 

snippet:

long start = System.currentTimeMillis();

//Preparing parameters

Map parameters = new HashMap();

parameters.put("ReportTitle", "Address Report");

parameters.put("DataFile", "ReportBeanFactory.java - Bean Collection");

 

JasperFillManager.fillReportToFile(fileName, parameters, new JRBeanCollectionDataSource(ReportBeanFactory.getBeanCollection()));

System.err.println("Filling time : " + (System.currentTimeMillis() - start));

System.exit(0);

 

 

 

but i m getting exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/javaflow/bytecode/Continuable

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(ClassLoader.java:620)

at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)

at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)

at java.net.URLClassLoader.access$100(URLClassLoader.java:56)

at java.net.URLClassLoader$1.run(URLClassLoader.java:195)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)

at java.lang.ClassLoader.loadClass(ClassLoader.java:251)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:601)

at net.sf.jasperreports.engine.JasperFillManager.fillReportToFile(JasperFillManager.java:492)

at net.sf.jasperreports.engine.JasperFillManager.fillReportToFile(JasperFillManager.java:448)

at ReportDataSourceApp.main(ReportDataSourceApp.java:40)

 

Process completed.:(

Post edited by: suryakusumakar, at: 2007/12/14 18:04

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...