Jump to content
Changes to the Jaspersoft community edition download ×

Export JasperReports query results into CSV file


marianocamarzana

Recommended Posts

Hi everyone, I have Java Web Project and contains a lot of JasperReports reports with complex SQL queries, containing a lot of parameters. The reports are used to produce pdf documents containing the data returned by the query, grouped and formatted in various ways.

Now I also have the need to export directly the query result into  csv file. Is it possible to ask JasperReports to execute only the query and return results instead of rendering the pdf page?
I try using the JRCsvMetadataExporter class but it produces an empty csv file.

        JRCsvMetadataExporter exporter = new JRCsvMetadataExporter();
        SimpleCsvMetadataExporterConfiguration configuration = new SimpleCsvMetadataExporterConfiguration();
        exporter.setExporterInput(new SimpleExporterInput(getJasperPrint()));
        exporter.setExporterOutput(new SimpleWriterExporterOutput(new File(nombreSalida)));
        exporter.setConfiguration(configuration);
        exporter.exportReport();

Thanks in advance.

Jasper Report 6.3.1

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

I've been using the following code with JasperReports:

jasperPrint = JasperFillManager.fillReport(jasperReport, paramMap, dataSource);
exporter = new JRCsvExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleWriterExporterOutput(new File(outputFilename)));
SimpleCsvExporterConfiguration configuration = new SimpleCsvExporterConfiguration();
csvConfiguration.setWriteBOM(Boolean.TRUE);
csvConfiguration.setRecordDelimiter("rn");
exporter.setConfiguration(configuration);
exporter.exportReport();
Link to comment
Share on other sites

Thank you very much for the quick response. The problem is that I need the result of the query without any format because of that I use according to Jasper's manual JRCsvMetadataExporter class .
In a first approach to the solution I use the JRCsvExporter class in a similar way to which you indicate but the export is not displayed correctly (exports the header, extra information, étc and does not properly separate the names of the columns).
I think that when using the JRCsvMetadataExporter class I need to configure the columns to be exported using the SimpleCsvMetadataExporterConfiguration class, but I do not know how to do it.


Attached example with the file in PDF format and export to CSV in which you can see that the export to CSV was not correct

TestPDF.png.a4c1bcad9bf659e8baa9079a92e2b584.png

TestCSV.png.7891d8390c50ae394b2dd4545e087052.png

Link to comment
Share on other sites

If you don't want to export the header/footer or any other bands, you can just set the properties to suppress it.

http://community.jaspersoft.com/wiki/how-suppress-column-headers-csv-exports-new-5x

 

Concerning extra columns in csv, it's a matter of how you define your report. See the following thread on how to avoid these columns.

http://community.jaspersoft.com/questions/533612/csv-extra-spacing-issue

Link to comment
Share on other sites

I could solve it by adding the following tag In the jxrml file in each column I wanted to export:
<<Property name = "net.sf.jasperreports.export.csv.column.name" value = "Name" />


http://jasperreports.sourceforge.net/sample.reference/jasper/#csvmetadataexport

And in the java code

        JRCsvMetadataExporter exporter = new JRCsvMetadataExporter();
        SimpleCsvMetadataReportConfiguration configuration = new SimpleCsvMetadataReportConfiguration();
        exporter.setExporterInput(new SimpleExporterInput(getJasperPrint()));
        exporter.setExporterOutput(new SimpleWriterExporterOutput(new File(nombreSalida)));
        exporter.setConfiguration(configuration);
        exporter.exportReport();

 

Thanks!.
 

 

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