Wednesday, 8 August 2012

Generate Excel file with single sheet in JasperReport

Yes, its quite easy to generate a excel report using Ireport . There are many blog guru's ,authors, developers has posted ample of solutions on this topic. But , still some time we are facing this problem.

Generating excel report is easy , but our requirement was all report should be come on a single sheet (not multiple sheet). We had few existing codes. But the problem was we were not about to find our exact solution. Finally after lots of testing & research got the solution.I hope it will help you.

The program code for generating Excel file from Ireport :-

JRResultSetDataSource jrds = new JRResultSetDataSource(rs);
JasperPrint print = JasperFillManager.fillReport(rptPath, hmp, jrds);
            ByteArrayOutputStream baos=new ByteArrayOutputStream();
            JRXlsExporter exporterXLS = new JRXlsExporter();
            exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, print);
            exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, baos);
            //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.CHARACTER_ENCODING, "UTF-8");
            resp.setHeader("Content-Disposition", "attachment; filename="+OutputFileName+".xls");

 Now , you just simple remove the below line which has setParameter().When you are setting parameter for your JRXLSReporter.

The Parameter is JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE

The line i removed is //exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);

Hope it will helpfull.