简体   繁体   English

如何使用JasperReports导出CSV

[英]How can I export CSV with JasperReports

I would like to export CSV file with JasperReports. 我想使用JasperReports导出CSV文件。 I am using REST and I do not know how to set ExporterOutput. 我正在使用REST,但我不知道如何设置ExporterOutput。 It works with PDF/XLSX however I can not get the solution of CSV. 它适用于PDF / XLSX,但是我无法获得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 followin problem: The method setExporterOutput(WriterExporterOutput) in the type JRAbstractExporter<CsvReportConfiguration,CsvExporterConfiguration,WriterExporterOutput,JRCsvExporterContext> is not applicable for the arguments (SimpleOutputStreamExporterOutput) exporter.setExporterOutputThe method setExporterOutput(WriterExporterOutput) in the type JRAbstractExporter<CsvReportConfiguration,CsvExporterConfiguration,WriterExporterOutput,JRCsvExporterContext> is not applicable for the arguments (SimpleOutputStreamExporterOutput)以下问题: The method setExporterOutput(WriterExporterOutput) in the type JRAbstractExporter<CsvReportConfiguration,CsvExporterConfiguration,WriterExporterOutput,JRCsvExporterContext> is not applicable for the arguments (SimpleOutputStreamExporterOutput)

It works with excel perfectly. 它与excel完美兼容。

I found solution and it is really simple: 我找到了解决方案,这真的很简单:

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

I've changed writer OutputStreamExporterOutput from SimpleOutputStreamExporterOutput to SimpleWriterExporterOutput 我已经将作家OutputStreamExporterOutputSimpleOutputStreamExporterOutput更改为SimpleWriterExporterOutput

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM