如何使用 JasperReports 导出 CSV

How can I export CSV with JasperReports

我想用 JasperReports 导出 CSV 文件。我正在使用 REST,但我不知道如何设置 ExporterOutput。它可以与 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();

exporter.setExporterOutput 我有以下问题:

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

它与 excel 完美配合。

我找到了解决方案,而且非常简单:

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

我已将编写器 OutputStreamExporterOutputSimpleOutputStreamExporterOutput 更改为 SimpleWriterExporterOutput