java - 在 Linux 上使用 JasperReports 获取损坏的 Excel 文件

标签 java linux jasper-reports export-to-excel opensuse

我使用此代码通过 JasperReports 4.6 将报告导出为 Excel 文件:

  File reportFile = new File(externalContextAuthenticationConfiguration.getReportTempFolderUrl());
    File outputFile = File.createTempFile("reportOutput", ".XLS", reportFile);
    JRXlsExporter exporterXLS = new JRXlsExporter();
    exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, print);
    exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_FILE, outputFile);
    exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
    exporterXLS.setParameter(JRXlsExporterParameter.IS_COLLAPSE_ROW_SPAN, Boolean.TRUE);
    exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.TRUE);
    exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
    exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, Boolean.TRUE);

    exporterXLS.setParameter(JRXlsExporterParameter.CHARACTER_ENCODING, "UTF-8");
    exporterXLS.exportReport();
    return outputFile.getAbsolutePath();

此代码在 Windows 中运行良好,但当项目转到 OpenSuse Linux 时,创建的 Excel 文件如下图所示:

enter image description here

有人知道问题出在哪里吗?

最佳答案

终于找到问题了: 问题不在 JasperReport 中。 它在 Excel 扩展中正确创建报告,但是当应用服务器将其发送到客户端时,问题发生了

我们使用此代码将报告发送给客户:

    response.setHeader("Content-Transfer-Encoding", "Cp1256");
    response.setContentType("application/vnd.ms-excel-download");

但 application/vnd.ms-excel-download 仅适用于 Windows Excel,它会破坏 Linux 中的 Excel 文件报告。

现在,我使用这段代码来压缩它,然后将它发送到客户端,这样它就可以在 Windows 和 Linux 服务器上运行:

        File zipFile = new File(fileName.replace("XLS", "ZIP"));
        FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
        ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
        addFileToZip("", fileName, zipOutputStream, false);
        zipOutputStream.flush();
        zipOutputStream.close();

关于java - 在 Linux 上使用 JasperReports 获取损坏的 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27941038/

相关文章:

linux - 无法连接到 Linux Mint 中的 Oracle 11g XE Apex Web 界面

jasper-reports - 通过url传递参数

java - hashCode() 是否在任何地方使用?

java - 我怎样才能很好地混合使用 Hibernate 和 Swing

linux - 如果已有同名文件,如何将文件复制到文件夹 (Linux BASH)

spring - 如何将 jasperreports_extension.properties 与 Spring 框架集成

jasper-reports - 如果记录溢出最后一个,如何将记录放入 JasperReports 的新页面?

java - 如何控制图像的大小?

java - Swing LayoutManagers 是否足够?

php - 运行 script.php 时出错 : Port 9000 is busy