JasperReports 6.x动态设置Excel工作表名及4.0.2升级至6.5.1适配
JasperReports 6.x Excel导出适配与动态工作表名设置
Hey there! Upgrading from JasperReports 4.0.2 to 6.5.1 brings a bunch of key changes to Excel exporting—most notably the deprecated JExcelApiExporter being replaced by JRXlsExporter. Let's walk through how to adapt your code and dynamically set sheet names properly.
1. Core API Changes to Keep in Mind
First, say goodbye to the old setParameter() approach—JasperReports 6.x uses a cleaner configuration-based system instead of legacy parameter classes. Here's the critical shift:
- Old Exporter:
net.sf.jasperreports.engine.export.JExcelApiExporter(relied on JExcel API/jxl.jar) - New Exporter:
net.sf.jasperreports.engine.export.JRXlsExporter(uses Apache POI under the hood, so you'll need POI dependencies in your project) - Parameter Replacement:
JRXlsExporterParameteris fully deprecated; use configuration classes likeSimpleXlsReportConfigurationinstead.
2. Dynamic Sheet Name Setup (Code Example)
Let's compare the old and new code to set a custom sheet name:
Old 4.0.2 Code (Deprecated)
JRAbstractExporter exporter = new net.sf.jasperreports.engine.export.JExcelApiExporter(); exporter.setParameter(JRXlsExporterParameter.SHEET_NAME, "My Custom Sheet"); // ... other parameters and export logic
New 6.x Code
// 1. Fill your report first (this part stays mostly the same) JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource); // 2. Initialize the new XLS exporter JRXlsExporter exporter = new JRXlsExporter(); // 3. Create and configure report settings SimpleXlsReportConfiguration xlsConfig = new SimpleXlsReportConfiguration(); xlsConfig.setSheetName("My Custom Sheet"); // This sets your dynamic sheet name // Add other export tweaks if needed: // xlsConfig.setFreezeRow(0); // Freeze header row // xlsConfig.setWrapText(true); // Enable text wrapping in cells // 4. Hook up input, config, and output exporter.setExporterInput(new SimpleExporterInput(jasperPrint)); exporter.setConfiguration(xlsConfig); exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(new File("output.xlsx"))); // 5. Run the export exporter.exportReport();
3. Handling Multiple Sheets (If You Need It)
If you want to export multiple reports into separate sheets of the same Excel file, use a configuration map to assign names to each sheet:
// Prepare multiple filled reports JasperPrint salesReport = JasperFillManager.fillReport(salesJrxml, params, salesDataSource); JasperPrint inventoryReport = JasperFillManager.fillReport(inventoryJrxml, params, inventoryDataSource); // Create input items for each report List<ExporterInputItem> inputItems = new ArrayList<>(); inputItems.add(new SimpleExporterInputItem(salesReport)); inputItems.add(new SimpleExporterInputItem(inventoryReport)); // Initialize exporter and set input JRXlsExporter exporter = new JRXlsExporter(); exporter.setExporterInput(new SimpleExporterInput(inputItems)); // Configure sheet names for each report SimpleXlsReportConfiguration salesConfig = new SimpleXlsReportConfiguration(); salesConfig.setSheetName("2024 Sales Data"); SimpleXlsReportConfiguration inventoryConfig = new SimpleXlsReportConfiguration(); inventoryConfig.setSheetName("Current Inventory"); // Map each input item to its configuration Map<ExporterInputItem, XlsReportConfiguration> configMap = new HashMap<>(); configMap.put(inputItems.get(0), salesConfig); configMap.put(inputItems.get(1), inventoryConfig); exporter.setConfigurationMap(configMap); // Set output and export exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(new File("multi-sheet-report.xlsx"))); exporter.exportReport();
4. Extra Upgrade Tips
- Dependency Cleanup: Remove
jxl.jar(used by the old exporter) and make sure you have Apache POI dependencies (jasperreports 6.x usually pulls in compatible POI versions, but double-check your build file). - Formatting Checks: Some old formatting behaviors (like cell merging, styling) might behave slightly differently with POI, so test your existing reports to ensure they look right.
- Other Settings: All old parameters (like
IS_ONE_PAGE_PER_SHEET,IS_DETECT_CELL_TYPE) are now setter methods inSimpleXlsReportConfiguration—just use the correspondingsetXxx()method.
内容的提问来源于stack exchange,提问作者user3311231




