Jump to content
We've recently updated our Privacy Statement, available here ×

JasperReports and C#: I did them work together


jeenajeena

Recommended Posts

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Of course.

 

I started with this article:

 

http://www.codeproject.com/KB/dotnet/Espresso.aspx

 

which is very very interesting. If you are registered, you can download a full working demo and all the sources.

 

I used the JNI.NET.Bridge.dll and the minijavart.dll from that article.

 

Basically, you need to write a Java class to produce your report. This is the very basic example I used:

 

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;

import net.sf.jasperreports.engine.JasperCompileManager;

import net.sf.jasperreports.engine.JasperExportManager;

import net.sf.jasperreports.engine.JasperFillManager;

import net.sf.jasperreports.engine.JasperPrint;

import net.sf.jasperreports.engine.JasperReport;

import java.util.Map;

import javax.swing.plaf.synth.Region;

 

 

 

public class BeJasper {

public BeJasper () {

 

}

 

public static void print(String report, String pdf)

{

 

Connection connection;

JasperReport jasperReport;

JasperPrint jasperPrint;

 

try

{

Class.forName("org.postgresql.Driver");

 

System.out.println("loading drivers...");

connection = DriverManager.getConnection("jdbc:postgresql://192.168.100.5:5432/d2000","postgres","mypass");

 

jasperReport = JasperCompileManager.compileReport(report);

 

// set here your parameters

Map parameters = new HashMap();

 

/*

java.util.Date dataDa = new java.util.Date();

java.util.Date dataA = new java.util.Date();

 

parameters.put("dataDa", dataDa);

parameters.put("dataA", dataA);

parameters.put("impianto", 8);

*/

 

jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);

 

JasperExportManager.exportReportToPdfFile(jasperPrint, pdf);

 

}

catch(JRException jrException)

{

jrException.printStackTrace();

}

catch (ClassNotFoundException classNotFoundException)

{

classNotFoundException.printStackTrace();

}

catch (SQLException sqlException)

{

 

sqlException.printStackTrace();

}

}

 

 

}

 

 

What we're trying to do, now, is to be able to call

 

BeJasper.print("myReport.jrxml", "result.pdf") from our c# application.

 

Download (and study) the article I suggested you then modify the code to map the Java code listed above. The C# code is quite long, so I'm posting here only the essential

 

public class Prog : JObject

{

 

readonly static JMethod _print;

 

static Prog ()

{

clazz = JClass.ForName ("BeJasper");

_ctr = clazz.GetConstructor ();

 

_print = clazz.GetStaticMethod ("print", "(SS)V");

 

}

 

public virtual void print(string report, string pdf)

{

_print.CallVoid (report, pdf);

}

 

 

In your app.conf you will also need to specify the right classpath, which in my case is

 

<java.class.path value=".;C:dataprgeclipseBeJasperbin;C:ProgrammiJavajre1.5.0_15librt.jar;C:ProgrammiJavajre1.5.0_15libjsse.jar;C:ProgrammiJavajre1.5.0_15libjce.jar;C:ProgrammiJavajre1.5.0_15libcharsets.jar;C:ProgrammiJavajre1.5.0_15libextdnsns.jar;C:ProgrammiJavajre1.5.0_15libextlocaledata.jar;C:ProgrammiJavajre1.5.0_15libextsunjce_provider.jar;C:ProgrammiJavajre1.5.0_15libextsunpkcs11.jar;C:ProgrammiiReportlibmysql-connector-java-3.1.11-bin.jar;C:Programmijasperreportsdistjasperreports-2.0.5.jar;C:Programmijasperreportslibitext-1.3.1.jar;C:Programmijasperreportslibjdt-compiler-3.1.1.jar;C:Programmijasperreportslibcommons-beanutils-1.7.jar;C:Programmijasperreportslibcommons-collections-2.1.jar;C:Programmijasperreportslibcommons-digester-1.7.jar;C:Programmijasperreportslibcommons-javaflow-20060411.jar;C:Programmijasperreportslibcommons-logging-1.0.2.jar;C:Programmijasperreportslibcommons-logging-api-1.0.2.jar;C:ProgrammiEclipsepluginsorg.eclipse.core.commands_3.3.0.I20070605-0010.jar;C:ProgrammiEclipsepluginsorg.eclipse.equinox.common_3.3.0.v20070426.jar;C:ProgrammiPostgreSQLjdbcpostgresql-8.2-506.jdbc3.jar;C:dataprgjasperd2000extensionsMyUtil.jar"/>

 

(MyUtil.jar is a helper class I wrote which is included in my reports)

 

I got crazy with classpath, so I adopted this solution: I created a libraries.jar containing all the jars needed for JR (find here a list http://www.jasperforge.org/jaspersoft/opensource/business_intelligence/jasperreports/requirements.html or use the classpath I wrote above); doing that, deployment of your application is much easier and you'd need just to write

 

<java.class.path value=".;./libraries.jar"/>

 

 

After all that, all you need to generate a report is something like

 

Prog jasperBride = new Prog();

jasperBride.print("sold.jrxml", "result.pdf");

 

// You can also use parameters, if you write a suitable method

jasperBride.print("sold2.jrxml", "result.pdf", parameters);

 

 

 

I'm very sorry this is still very confusing: actually, I got this result yesterday night and I'm still working on a cleanup. I'm going to publish a dll with all that's needed (and may be clear and detailed article too) so if your interested, please keep in touch (my email is jeenajeena (at) hotmail.com)

 

Post edited by: jeenajeena, at: 2008/03/28 11:06

Post edited by: jeenajeena, at: 2008/03/28 11:13

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...