JasperReports 导出到 xlsx,而不是 xls

发布于 2024-12-02 06:30:53 字数 149 浏览 2 评论 0原文

我找不到如何在 JasperReports 4.1.1 中导出 .xlsx 中的文件。 类:

JRXlsExporter

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

I can't find how to export a file in .xlsx in JasperReports 4.1.1.
The class:

JRXlsExporter

has not a Xlsx equivalent. And i can't find a parameter to set the output format from xls to xlsx.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南巷近海 2024-12-09 06:30:53

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< /em> 版本库的 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 接口。在上面的示例中,我使用了 XlsReportConfigurationSimpleXlsxReportConfiguration 实现来定义特定于 JRXlsxExporter 导出器的设置。


更多信息

The JRXlsxExporter class should be used for exporting at XLSX format.

Sample of using exporter with JasperReports prior 5.5.2 version

Till JasperReports 5.5.1 this code can be used for generating report at xlsx format:

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();

Since 5.5.2 version of library the JRAbstractExporter.setParameter(JRExporterParameter, Object) method is deprecated.

Sample of using exporter with modern JasperReports versions

In this example I used JRS 6.4.1 version:

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);
}

Instead of using JRExporter.setParameter method we have to use implementation of XlsReportConfiguration interface. In example above I used SimpleXlsxReportConfiguration implementation of XlsReportConfiguration for defining settings specific to JRXlsxExporter exporter.


More information

吾性傲以野 2024-12-09 06:30:53

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

在更高版本 >5.6 中,JRXlsxExporter.setParameter(..) 已被弃用

你应该使用

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();

This answer is to help users with JASPER REPORT VERSION >5.6 (latest versions), hence remove the deprecated code.

In later version >5.6 the JRXlsxExporter.setParameter(..) has been deprecated.

You should use

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();
以往的大感动 2024-12-09 06:30:53

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

@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 );
}

All you need to do is put the format in the request path, like this:

@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 );
}

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

JRXlsExporter is available in in JasperReports 4.5 and later versions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文