Using various Java exporters provided by Jasper Reports

In my previous blog, I demonstrate how to export a Jasper report with the Java class JRHtmlExporter to an HTML email. In this blog, I will include the codes I use to export a Jasper report to other commonly used file formats. In all cases, I start with a JasperPrint object from a given Jasper Reports template and create the resulting file as an attachment in a HTTP response.

PDF file

For PDF file, you may just use the static method exportReportToPdf in class net.sf.jasperreports.engine.JasperExportManager as shown below:

byte[] pdfBytes = JasperExportManager.exportReportToPdf(print);
response.setContentType(“application/pdf”);
response.setHeader(“Content-Disposition”, “attachment;filename=example.pdf”);
response.getOutputStream().write(pdfBytes);
response.flushBuffer();

MS Excel xlsx file

For MS Excel 2007 or 2010 file, use class net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter:

JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
ByteArrayOutputStream os = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, os);
exporter.exportReport();

response.setContentType(“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”);
response.setHeader(“Content-Disposition”, “attachment;filename=example.xlsx”);
response.getOutputStream().write(os.toByteArray());
response.flushBuffer();

Note the content type set in the response header.

MS Excel xls file

If you need to export your report in older Excel file format, use class net.sf.jasperreports.engine.export.JRXlsExporter similar to codes above and replace the content type in the response header with “application/vnd.ms-excel” and filename suffix to xls:

JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
ByteArrayOutputStream os = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, os);
exporter.exportReport();

response.setContentType(“application/vnd.ms-excel”);
response.setHeader(“Content-Disposition”, “attachment;filename=example.xls”);
response.getOutputStream().write(os.toByteArray());
response.flushBuffer();

CSV file

For csv files, use class net.sf.jasperreports.engine.export.JRCsvExporter

JRCsvExporter exporter = new JRCsvExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
StringBuffer buffer = new StringBuffer();
exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, buffer);
exporter.exportReport();

response.setContentType(“text/csv”);
response.setHeader(“Content-Disposition”, “attachment;filename=” + getFileName(name) + “.csv”);
response.getOutputStream().write(buffer.toString().getBytes());
response.flushBuffer();

Other file formats

The Jasper Reports API provides quite a few other exporters. Look at the Java Doc here .

About Raymond Lee
Professional Java/EE Developer, software development technology enthusiast.

Comments are closed.