If you’re upgrading to JasperReports 5.6+ and encountering deprecation warnings for JRXlsExporter.setParameter
, this guide will help you migrate to the updated exporter API. The old JRExporter
and setParameter
methods are deprecated in favor of a more structured approach using ExporterInput
, ExporterOutput
, and configuration classes. Here’s how to adapt your code:
Why the Deprecation?
JasperReports 5.6 introduced a revamped exporter framework to streamline configurations. Instead of using generic setParameter
calls, you now define:
ExporterInput
: The compiled report (e.g.,JasperPrint
object).ExporterOutput
: The output destination (e.g., file or stream).ReportConfiguration
: Export settings (e.g., pagination, cell detection).
Step-by-Step Solution
Here’s how to export your report to XLS with JasperReports 5.6:
1. Export to XLS Using JRXlsExporter
try {
// Generate JasperPrint object
JasperPrint jasperPrint = JasperFillManager.fillReport(
sourceFileName,
parameters,
new JRBeanCollectionDataSource(dataList)
);
// Initialize exporter
JRXlsExporter exporter = new JRXlsExporter();
// Set input (compiled report)
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
// Set output (file or stream)
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("path/to/output.xls"));
// Configure export settings
SimpleXlsReportConfiguration config = new SimpleXlsReportConfiguration();
config.setDetectCellType(true); // Equivalent to IS_DETECT_CELL_TYPE
config.setRemoveEmptySpaceBetweenRows(true); // Equivalent to IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS
config.setOnePagePerSheet(true); // Split report into multiple sheets
config.setCollapseRowSpan(false); // Preserve merged cells
config.setWhitePageBackground(false); // Remove default white background
// Apply configuration
exporter.setConfiguration(config);
// Execute export
exporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
}
Key Changes Explained
Old Parameter (Deprecated) | New Configuration Method |
---|---|
JRXlsExporterParameter.JASPER_PRINT | setExporterInput(new SimpleExporterInput(...)) |
JRXlsExporterParameter.OUTPUT_STREAM | setExporterOutput(new SimpleOutputStreamExporterOutput(...)) |
IS_DETECT_CELL_TYPE | config.setDetectCellType(true) |
IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS | config.setRemoveEmptySpaceBetweenRows(true) |
Common Pitfalls & Fixes
Loading JasperPrint
correctly is essential. If using fillReportToFile
, load the JasperPrint
object from the generated .jrprint
file:
String printFileName = JasperFillManager.fillReportToFile(sourceFileName, parameters, dataSource);
JasperPrint jasperPrint = (JasperPrint) JasperPrintManager.loadObject(printFileName);
Ensure that you use SimpleXlsReportConfiguration
for XLS-specific settings and do not confuse it with SimpleXlsExporterConfiguration
, which is meant for advanced customization.
For Java 7 compatibility, ensure your project includes the latest JasperReports 5.6+ libraries. If you are using Maven, update your pom.xml
:
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.20.0</version> <!-- Check for newer versions -->
</dependency>
SEO Keywords
JasperReports 5.6 XLS export deprecated, Migrate JRXlsExporter.setParameter, JasperReports Excel exporter example, SimpleXlsReportConfiguration setup, Fix JasperReports deprecated API
By following this guide, you’ll resolve deprecation warnings and ensure compatibility with modern JasperReports versions. For more details, refer to the official JasperReports documentation.