【问题标题】:JasperReports export to xlsx, not xlsJasperReports 导出到 xlsx,而不是 xls
【发布时间】:2011-08-30 14:40:07
【问题描述】:

我在 JasperReports 4.1.1 中找不到如何以 .xlsx 格式导出文件。 班级:

JRXlsExporter

没有 Xlsx 等价物。而且我找不到将输出格式从 xls 设置为 xlsx 的参数。

【问题讨论】:

标签: java jasper-reports xls xlsx


【解决方案1】:

JRXlsxExporter 类应该用于以 XLSX 格式导出。

在 JasperReports 5.5.2 之前的版本中使用导出器的示例

直到 JasperReports 5.5.1 这段代码可用于生成 xlsx 格式的报告:

JRMapArrayDataSource dataSource = new JRMapArrayDataSource(data);

JasperReport jasperReport = JasperCompileManager.compileReport(reportJRXMLSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);

JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRXlsExporterParameter.OUTPUT_FILE_NAME, outputFileName);

exporter.exportReport();

由于库的 5.5.2 版本,JRAbstractExporter.setParameter(JRExporterParameter, Object) 方法已被弃用。

在现代 JasperReports 版本中使用导出器的示例

在这个例子中,我使用了 JRS 6.4.1 版本:

JasperReport jasperReport;
try (InputStream inputStream = JRLoader.getResourceInputStream(jrxmlFilePath)) {
    jasperReport = JasperCompileManager.compileReport(JRXmlLoader.load(inputStream));
}
Map<String, Object> params = new HashMap<>();

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());

SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();
configuration.setOnePagePerSheet(true);
configuration.setIgnoreGraphics(false);

File outputFile = new File("output.xlsx");
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
     OutputStream fileOutputStream = new FileOutputStream(outputFile)) {
    Exporter exporter = new JRXlsxExporter();
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(byteArrayOutputStream));
    exporter.setConfiguration(configuration);
    exporter.exportReport();
    byteArrayOutputStream.writeTo(fileOutputStream);
}

我们必须使用XlsReportConfiguration 接口的实现,而不是使用JRExporter.setParameter 方法。在上面的示例中,我使用 XlsReportConfigurationSimpleXlsxReportConfiguration 实现来定义特定于 JRXlsxExporter 导出器的设置。


更多信息

【讨论】:

  • 我们需要将版本更新到更高版本吗?我正在使用jasperreports-3.7.6
  • @tailorBird 3.7.0 版包含此类。 JR 的最新版本是 5.5.1
  • 谢谢@Alex K。实际上我已经问过一个问题before 关于这个。我在这里找到了答案。会试试这个。
  • 5 年后,JRXlsExporterParameter.JASPER_PRINT 已被弃用,我添加了非弃用解决方案。
  • @DanBrandt 是的,这个例子是 7 年前发布的 :) 我添加了实际例子,谢谢
【解决方案2】:

这个答案是为了帮助使用 JASPER REPORT VERSION >5.6(最新版本)的用户,因此删除不推荐使用的代码。

在 >5.6 的更高版本中,JRXlsxExporter.setParameter(..) 一直是 deprecated

你应该使用

JRMapArrayDataSource dataSource = new JRMapArrayDataSource(data);

JasperReport jasperReport = JasperCompileManager.compileReport(reportJRXMLSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);

JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
File outputFile = new File("excelTest.xlsx");
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputFile));
SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration(); 
configuration.setDetectCellType(true);//Set configuration as you like it!!
configuration.setCollapseRowSpan(false);
exporter.setConfiguration(configuration);
exporter.exportReport();

【讨论】:

    【解决方案3】:

    您需要做的就是将格式放入请求路径中,如下所示:

    @RequestMapping( value = "/ActivityReport.xlsx", method = RequestMethod.GET )
    public ModelAndView generateActivityReportXLS( HttpServletRequest request, HttpServletResponse response ) {
    
    
        List<ActivityDisplay> list = activityManager.listActivities();
    
        Map<String, Object> parameterMap = new HashMap<>();
        parameterMap.put( "datasource", new JRBeanCollectionDataSource( list ) );
        return new ModelAndView( "activitiesXLSView", parameterMap );
    }
    

    【讨论】:

      【解决方案4】:

      JRXlsExporter 在 JasperReports 4.5 及更高版本中可用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-08
        • 2020-12-08
        相关资源
        最近更新 更多