我使用此代码通过 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 文件如下图所示:
有人知道问题出在哪里吗?
最佳答案
终于找到问题了: 问题不在 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/