snippetjournal

– notes, snippet codes, and my journal

Jasper Report : XLSX and PDF using Jasper Exporter

leave a comment »

This java codes are snippet to generate jasper report using DataSource, JasperPrint, JasperFillManager, Jasper Export Manager :

Cuplikan kode java di bawah ini untuk menghasikan report dengan ekstensi xlsx dan pdf dengan menggunakan DataSource, JasperPrint, JasperFillManager, Jasper Export Manager :

To Download XLSX file / Untuk mengunduh xlsx file


private void getDownloadReportXlsx(HttpServletRequest request, HttpServletResponse response, JRDataSource jrDataSource) throws JRException, IOException {

 //uncomment this codes if u are want to use servlet output stream
 //ServletOutputStream servletOutputStream = response.getOutputStream();

 //get real path for report
 String path = request.getSession().getServletContext().getRealPath("\\WEB-INF\\reports\\xxx.jrxml");
 JasperReport jasperReport = JasperCompileManager.compileReport(path);
 JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), jrDataSource);

 JRXlsxExporter xlsxExporter = new JRXlsxExporter();
 ByteArrayOutputStream os = new ByteArrayOutputStream();

 xlsxExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
 xlsxExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "xxx.xlsx");

 //uncomment this codes if u are want to use servlet output stream
 //xlsxExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, servletOutputStream);

 xlsxExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, os);

 xlsxExporter.exportReport();

 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
 response.setHeader("Content-Disposition", "attachment; filename=xxx.xlsx");

 //uncomment this codes if u are want to use servlet output stream
 //servletOutputStream.write(os.toByteArray());

 response.getOutputStream().write(os.toByteArray());
 response.getOutputStream().flush();
 response.getOutputStream().close();
 response.flushBuffer();

 }

To generate PDF file is more simple than generate XLSX, this are the snippet codes :

Untuk menghasil file pdf lebih mudah dari pada menghasilkan file xlsx :

private void getDownloadReportPdf(HttpServletRequest request, HttpServletResponse response, JRDataSource jrDataSource) {
 try {
 String path = request.getSession().getServletContext().getRealPath("\\WEB-INF\\reports\\xxx.jrxml");
 JasperReport jasperReport = JasperCompileManager.compileReport(path);
 JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), jrDataSource);
 byte[] pdfBytes = JasperExportManager.exportReportToPdf(jasperPrint);

 //uncomment this line to make browser download the file
 //response.setContentType("application/pdf");
 //response.setHeader("Content-Disposition", "attachment;filename=xxx.pdf");

 response.getOutputStream().write(pdfBytes);
 response.getOutputStream().flush();
 response.getOutputStream().close();
 response.flushBuffer();

 } catch (JRException ex) {
 log.error(ex.getMessage(), ex.getCause());
 } catch (IOException ex) {
 log.error(ex.getMessage(), ex.getCause());
 }

}

To make the browser download file (several browser has supported default pdf viewer in their engine), simply add thisattribute to HtttpServletRespones :

Agar browser dapat mengunduh file tambahkan attribute di bawah ini :


uncomment this line to make browser download the file
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;filename=xxx.fileExtension");

if you get error servlet ouput stream that already called, you can use this trick, simply reset the response when your menu view get called.


response.reset();

this error caused because the codes using response.getOutputStream().write(Object).  when we using JSP the component that responsible for rendering JSP as HTML in browser is servlet.write using output stream resources.

so when we redirect to another view (example in spring mvc : return string_page.jsp) it will conflict the resources. because we already define the resource to write in byte


Updated :

In jasper report 6.xx version the xlsxExporter.setParameter is deprecated, so instead define jasper parameter we can define the configuration using jasper configuration :


xlsxExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
xlsxExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputReportName));
xlsxExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(os));

reference :

http://www.ramkitech.com/2011/11/jsf-jpa-jasperreports-ireport.html

http://himtech-spring.blogspot.com/2012/07/spring-mvc-with-jasperreport.html

http://kristantohans.wordpress.com/tag/tutorial-jasper-report/

https://raymondhlee.wordpress.com/category/jasper-reports/

Written by snippetjournal

June 19, 2013 at 3:40 am

Posted in Programming

Tagged with , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: