Search code examples
javacsvjasper-reportsexport-to-csv

How can I export CSV with JasperReports


I would like to export a CSV file with JasperReports. I am using REST and I do not know how to set ExporterOutput. It works with PDF/XLSX however I can not get it to work to export CSV.

JasperPrint jasperPrint = storageManager.generateYearAggregateXLS(id, year);
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setHeader("Content-Disposition", "attachment;filename=Agregaty.xlsx");
    response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");

    JRCsvExporter exporter = new JRCsvExporter();
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));

    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
    
    SimpleCsvReportConfiguration configuration = new SimpleCsvReportConfiguration();
    exporter.setConfiguration(configuration);
    exporter.exportReport();

In exporter.setExporterOutput I have the following problem:

The method setExporterOutput(WriterExporterOutput) in the type JRAbstractExporter<CsvReportConfiguration,CsvExporterConfiguration,WriterExporterOutput,JRCsvExporterContext> is not applicable for the arguments (SimpleOutputStreamExporterOutput)

It works with excel perfectly.


Solution

  • I found solution and it is really simple:

    exporter.setExporterOutput(new SimpleWriterExporterOutput(response.getOutputStream()));
    

    I've changed writer OutputStreamExporterOutput from SimpleOutputStreamExporterOutput to SimpleWriterExporterOutput