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

Exporting Report to CSV or XLS


ozwolverine

Recommended Posts

Hello everybody,

 

I need to export my generated report to PDF and csv (or XLS), I know how to export to PDF with

Code:
JasperRunManager.runReportToPdfFile

 

Is there any way to do it, but to export to CSV (XLS)?

Can anyone give me some code excerpt?

 

thanks a lot.

Post edited by: ozwolverine, at: 2006/08/29 01:15

Link to comment
Share on other sites

  • Replies 12
  • Created
  • Last Reply

Top Posters In This Topic

1. Spend some time with reading the API documentation of the classes JRAbstractExporter and JRExporterParameter and derivated classes.

 

2. Then you will have no problem to understand the examples in the sample directory of the JasperReports distribution. The most of them supports exports in different formats including CSV and Excel.

Link to comment
Share on other sites

Alexander Merz wrote:

1. Spend some time with reading the API documentation of the classes JRAbstractExporter and JRExporterParameter and derivated classes.

2. Then you will have no problem to understand the examples in the sample directory of the JasperReports distribution. The most of them supports exports in different formats including CSV and Excel.

Thanks a lot, I will read them.

Link to comment
Share on other sites

Hello Teodore and Alexander, and everyone :)

 

I have read the API doc and watch some examples and I found code like this:

 

 

Code:


File sourceFile = new File(fileName);

JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject( sourceFile );

File destFile = new File( sourceFile.getParent(), jasperPrint.getName() + ".xls" );

JRXlsExporter exporter = new JRXlsExporter();

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);

exporter.exportReport();

 

 

But I still have a question. If my reports have parameters, how could I pass them to the report?

I can see in the examples that there are some parameters, but those are specific for the classes used (JRXlsExporter).

 

Thanks a lot,

Link to comment
Share on other sites

Hello Teodore and Alexander, and everyone :)

 

I have read the API doc and watch some examples and I found code like this:

 

 

Code:


File sourceFile = new File(fileName);

JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject( sourceFile );

File destFile = new File( sourceFile.getParent(), jasperPrint.getName() + ".xls" );

JRXlsExporter exporter = new JRXlsExporter();

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);

exporter.exportReport();

 

 

But I still have a question. If my reports have parameters, how could I pass them to the report?

I can see in the examples that there are some parameters, but those are specific for the classes used (JRXlsExporter).

 

Thanks a lot,

Link to comment
Share on other sites

Creating a report requires three steps:

- compile the report xml template

- filling with data

- exporting to something viewable

 

The methods in JasperRunManager hide these steps.

 

To make each step manually:

Responsible for compiling -> JasperCompileManager

filling -> JasperFillManager

exporting -> the already mentioned Exporter classes

Link to comment
Share on other sites

Hello, After watching the examples, I have done this code to run and export my report, but it is throwing this exception:

 

net.sf.jasperreports.engine.JRException: Error loading object from file : c:tmpreportesSECReporteUsuariosRegistrados.jrxml

 

my procedure is:

 

 

 

Code:
		       try {
NOM_ARCHIVO=PropiedadesSistema.HOME_RUTA_REPORTES +"ReporteUsuariosRegistrados.jrxml";
// NOM_ARCHIVO = rutaReportesBIRT + "WebappReport.jrxml";
System.out.println("Inicia Proceso CSV"«»);
System.out.println("Archivo fuente: " +NOM_ARCHIVO);

//COmpilar Reporte
JasperCompileManager.compileReportToFile( NOM_ARCHIVO );

//Llenar Reporte

JasperFillManager.fillReportToFile(NOM_ARCHIVO, parametros, getConnection());


//Exportar Reporte a CSV

File sourceFile = new File( NOM_ARCHIVO );

JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);

File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".xls"«»);

System.out.println("Archivo XLS de salida: " + destFile.toString());

JRXlsExporter exporter = new JRXlsExporter();
// exporter.setParameters ( ) ;
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString());
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);

exporter.exportReport();

FileInputStream fileInputStream = new FileInputStream( destFile );
int tam = fileInputStream.available();
byte[] bytesXLS = new byte[tam];
fileInputStream.read( bytesXLS );
fileInputStream.close();
long time = System.currentTimeMillis();
UtilidadesSEC.descargarArchivo( bytesXLS, "reporteUsuariosRegistrados" + time+ ".pdf" );

// JasperRunManager.runReportToPdfFile(NOM_ARCHIVO, archivoPDF, parametros, new JREmptyDataSource());
} catch (JRException e) {
logger.error( e );
logger.info( e.getMessage() );
request.setAttribute( "errores", e.getMessage() );
return "";
}

 

What should be the problem? I'm sure the file is in the suggested location.

 

What could the problem be?

 

I'm attaching the report file.

[file name=ReporteUsuariosRegistrados.jrxml size=12491]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/ReporteUsuariosRegistrados.jrxml[/file]

Post edited by: ozwolverine, at: 2006/08/31 05:43

Link to comment
Share on other sites

Ok, look on this lines in your code:

 

Code:

File sourceFile = new File( NOM_ARCHIVO );
JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);

 

Remember the content of NOM_ARCHIVO?

 

Code:
[code]
NOM_ARCHIVO = rutaReportesBIRT +
"WebappReport.jrxml";

 

You trying to load the JRXML file, what is nonsense, because the method creates a JasperPrint object from a serialized JasperPrint object stored in a file. But the JRXML does not contain such an serialized object.

 

For your first trials, try this way: Instead of saving the results to file pass it directly to a variable.

 

Code:
[code]JasperReport jasperReport =
JasperCompileManager.compileReport( NOM_ARCHIVO );

JasperPrint jasperPrint =
JasperFillManager.fillReport(jasperReport,
parametros,
getConnection());

The rest seems to be ok.

Post edited by: Alexander Merz, at: 2006/08/31 08:11

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