【问题标题】:How to export to excel in JasperReports?如何在 JasperReports 中导出到 excel?
【发布时间】:2010-09-17 16:48:37
【问题描述】:

我需要以JasperReports 导出为 excel 和 csv 格式。对于 excel,我尝试使用 JRXlsExporter 类,但它没有导出。问题是“保存并取消”弹出窗口带有未知文件类型..

 file type like "getReportDetail.do"

其中getReportDetail.do 是struts config xml 的“action”元素中的“path”属性。我通过单击 html 按钮调用“动作类”来导出 excel 来调用此 getReportDetail.do

我正在设置如下参数

reportExporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint); 
reportExporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, reportStream); 

reportExporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); 
reportExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
reportExporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
reportExporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);

其中reportStreamByteArrayOutputStream() 的对象,

reportExporterJRXlsExporter 对象

contenttyperesponse.setContentType("application/xls");

知道为什么会这样吗?

【问题讨论】:

  • 尝试先将文件保存到您的文件系统,看看生成的报告是否有问题,或者之后是否有问题。

标签: jasper-reports


【解决方案1】:

尝试设置以下标题:

Content-Type: application/vnd.ms-excel
Content-Disposition: attachment; filename=report.xls

【讨论】:

    【解决方案2】:

    这是我的代码,但请注意实现的版本。 注意:使用 ireport 导出 excel,适用于 ireport 6.0,java 7

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

    对于较低版本的 ireport 5.6

              try {
                       JasperPrint jasperPrint = JasperFillManager.fillReport(new FileInputStream(path), parametro, PgConnector.getConexion());
                        JRXlsExporter exporterXLS = new JRXlsExporter();
                        exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
                        exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
                        exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
                        exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
                        exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
                        exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_FILE_NAME,PATH_REPORT_FILE + nameExcel);
                        exporterXLS.exportReport();
                    } catch (Exception ex) {
                        ex.printStackTrace();
    
                    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-12
      相关资源
      最近更新 更多