2015-01-14 35 views
1

我用這個代碼報告導出爲的Excel文件,JasperReports的4.6入門損壞的Excel與JasperReports的文件在Linux上

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

有誰知道問題是什麼?

+0

你檢查結果文件,*微軟Office *? –

+0

是的,當我用MS Office打開它也是損壞 – OmiD

回答

0

最後我發現問題: 問題不在JasperReport中。 它正確地創建在Excel擴展的報告,但是當應用服務器發送給客戶端的問題發生

我們用這個代碼將報告發送給客戶端:

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

但應用/ vnd.ms- excel下載僅適用於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(); 
+0

嗨,我不知道這個邏輯。您將文件下載爲zip文件,然後在本地將其重命名爲(手動)xlsx?我得到同樣的問題,我注意到,當您將擴展名更改爲zip時,xlsx下載的文件無法正常解壓縮。 ... – martinnovoty