Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Linux by (9.5k points)

Below is the code I have to export report as Excel file with JasperReports 4.6:

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

It works well in Windows, but in OpenSuse Linux the output excel file is not formatted. Can anyone tell me how to rectify this? 

1 Answer

0 votes
by (19.7k points)

This is not a problem of JasperReport as it creates the report in Excel extension correctly. When the app server sends it to the client, the excel format is destroyed. 

To make it work, you can use the below code to zip it and send it to the client which works well for both Windows and Linux servers. 

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


 

If you want to learn more about Linuxthen go through this Linux tutorial by Intellipaat for more insights. 

Related questions

0 votes
0 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...