15

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.

AgostinoX
  • 7,477
  • 20
  • 77
  • 137

4 Answers4

28

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

Alex K
  • 22,315
  • 19
  • 108
  • 236
  • Do we need to update the version to the higher one? I am using `jasperreports-3.7.6` – Nidheesh Mar 26 '14 at 06:42
  • 1
    @tailorBird The version 3.7.0 contains this class. The last version of *JR* is 5.5.1 – Alex K Mar 26 '14 at 09:50
  • Thanks @Alex K. Actually I have asked a question [before](http://stackoverflow.com/questions/22497343/jrxlsexporter-invalid-row-number-65536-outside-allowable-range) regarding this. I found an answer here. Will try this. – Nidheesh Mar 26 '14 at 10:51
  • 5 years later, the JRXlsExporterParameter.JASPER_PRINT has been deprecated, I have add non deprecated solution. – Petter Friberg Jan 20 '16 at 17:03
  • setParameter() is deprecated since version 5.6. Look at the post below – Dan Brandt May 27 '19 at 15:40
  • 2
    @DanBrandt Yes, this example was posted 7 years ago :) I added actual example, thanks – Alex K May 27 '19 at 19:08
18

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();
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
2

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 );
}
seenukarthi
  • 8,241
  • 10
  • 47
  • 68
0

JRXlsExporter is available in in JasperReports 4.5 and later versions.

Chi Nguyen
  • 89
  • 1
  • 8