Java JasperReports 5.6:不推荐使用 JRXlsExporter.setParameter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24117878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:16:35  来源:igfitidea点击:

JasperReports 5.6: JRXlsExporter.setParameter is deprecated

javajasper-reportsexportdeprecated

提问by Tobia

I have this code to export a JasperReprot to XLS:

我有这个代码可以将 JasperReprot 导出到 XLS:

        JasperPrint jprint=JasperFillManager.fillReport(expRpg, null, new JRBeanCollectionDataSource(datalist));
        JRXlsExporter exporter = new JRXlsExporter();
        exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jprint); 
        exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, outStream);
        exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); 
        exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
        exporter.exportReport();

Upgrading to JasperReports 5.6all setParameter are flagged as "deprecated" and I can not find documentation to adapt this code.

升级到JasperReports 5.6,所有 setParameter 都被标记为“已弃用”,我找不到适应此代码的文档。

How to export a report to xls with JasperReports 5.6?

如何使用JasperReports 5.6将报告导出到 xls ?

采纳答案by Sangram Jadhav

JRExporter became deprecated in 5.6. They introduced new interface Exporter and retrofitted all exporters to have ExporterInput, ReportExportConfiguration, ExporterConfiguration,ExporterOutput. See below link

JRExporter 在 5.6 中被弃用。他们引入了新的 Exporter 接口,并将所有 exporter 改装为具有 ExporterInput、ReportExportConfiguration、ExporterConfiguration、ExporterOutput。见下面的链接

http://jasperreports.sourceforge.net/api/net/sf/jasperreports/export/Exporter.html

http://jasperreports.sourceforge.net/api/net/sf/jasperreports/export/Exporter.html

This means that instead of setParameter, you need to create configuration using above mentioned classes or their child classes PDF export example. Excel export should follow same methodology

这意味着您需要使用上述类或其子类 PDF 导出示例创建配置,而不是 setParameter。Excel 导出应遵循相同的方法

JRPdfExporter exporter = new JRPdfExporter();

exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(outputStream);
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
exporter.setConfiguration(configuration);

exporter.exportReport();

Excel counterpart

Excel对应

JRXlsExporter exporter = new JRXlsExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile));
SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
configuration.setOnePagePerSheet(true);
configuration.setDetectCellType(true);
configuration.setCollapseRowSpan(false);
exporter.setConfiguration(configuration);

exporter.exportReport();

SimpleXlsReportConfiguration will have excel export related configuration. Set values as per your requirement

SimpleXlsReportConfiguration 会有excel导出相关的配置。根据您的要求设置值

回答by diego matos - keke

Thank to the above code , here is my code : Notice : Export excel with ireport, ireport 6.0 , java 7

感谢上面的代码,这是我的代码: 注意:使用 ireport、ireport 6.0、java 7 导出 excel

 Map<String, Object> parametro = new HashMap<String, Object>();
                parametro.put("USUARIO", UConstante.NAME_MINISTERIO_USER);
                parametro.put("RUTA_LOGO", PuenteFile.getRutaFiles(FacesContext.getCurrentInstance(), PuenteFile.RUTA_IMG_LOGO));
                parametro.put("PATH_SYSTEM", rutaFileSystemHD);
                parametro.put("WHERE_DATA", WHERE_REGISTRO);
                parametro.put("WHERE_PROYECTO_USUARIO", WHERE_PROYECTO_USUARIO);
                parametro.put("WHERE_ZONA", WHERE_ZONA);
                parametro.put("NAME_APP", RutaFile.NAME_APP);
                parametro.put("ID_USUARIO", getUsuario().getId());
                parametro.put("ID_PROYECTO", beanProyecto.getId());
                parametro.put("SUBREPORT_DIR", SUBREPORT_DIR);

                System.out.println(">>>>>> PARAMETROS :" + parametro.toString());

              try {
                    JasperPrint jasperPrint = JasperFillManager.fillReport(path, parametro, PgConnector.getConexion());
                    JRXlsExporter xlsExporter = new JRXlsExporter();
                    xlsExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
                    xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(PuenteFile.getRutaFiles(FacesContext.getCurrentInstance(), PuenteFile.RUTA_REPORT_FILE) + nameExcel));
                    SimpleXlsReportConfiguration xlsReportConfiguration = new SimpleXlsReportConfiguration();
                    SimpleXlsExporterConfiguration xlsExporterConfiguration = new SimpleXlsExporterConfiguration();
                    xlsReportConfiguration.setOnePagePerSheet(true);
                    xlsReportConfiguration.setRemoveEmptySpaceBetweenRows(false);
                    xlsReportConfiguration.setDetectCellType(true);
                    xlsReportConfiguration.setWhitePageBackground(false);
                    xlsExporter.setConfiguration(xlsReportConfiguration);
                    xlsExporter.exportReport();

                } catch (Exception ex) {
                    ex.printStackTrace();
                }

回答by Anton KONG

Here is my CODE:

这是我的代码:

String sourceFileName = "./jasper_report_template.jasper";
Map parameters = new HashMap();
String printFileName = null;
try {
      printFileName = JasperFillManager.fillReportToFile(sourceFileName, parameters, beanArrayDataSource);
      if(printFileName != null){
           //JasperPrintManager.printReport( printFileName, true);
            /** 1- export to PDF*/
            JasperExportManager.exportReportToPdfFile(printFileName,
                      "C://Users/zanderkong/Desktop/sample_report.pdf");
            /**3- export to Excel sheet*/
            RXlsExporter xlsExporter = new JRXlsExporter();
            xlsExporter.setExporterInput(new SimpleExporterInput(printFileName));
            xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput("C://Users/zanderkong/Desktop/sample_report.xls"));
            SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
            configuration.setOnePagePerSheet(true);
            configuration.setDetectCellType(true);
            configuration.setCollapseRowSpan(false);
            xlsExporter.setConfiguration(configuration);
            xlsExporter.exportReport();
         }
        } catch (JRException e) {
             e.printStackTrace();
          }