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.setHeader(“Content-Disposition”, “attachment;filename=example.pdf”);

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

response.setHeader(“Content-Disposition”, “attachment;filename=example.xlsx”);

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/” 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);

response.setHeader(“Content-Disposition”, “attachment;filename=example.xls”);

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

response.setHeader(“Content-Disposition”, “attachment;filename=” + getFileName(name) + “.csv”);

Other file formats

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

Using Spring 3.1 Cache Abstraction and Ehcache

Spring 3.1 supports a new cache abstraction layer which allows you to implement caching in a web application beyond the L2 and query cache in Hibernate. This blog provides a quick walkthrough on how to setup and apply caching  in your codes using Spring and Ehcache.

Getting the jars

First, we need to add the spring context support and ehcache jar files to the project. I am using Maven and include the following dependencies in the pom file:



Configure Spring

As with any other Spring components, we need to configure some beans in a XML file. At a minimum, we will need the followings:

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”;

<cache:annotation-driven cache-manager=”cacheManager”/>

<bean id=”cacheManager”>
<property name=”cacheManager” ref=”ehcache”/>

<!– Ehcache library setup –>
<bean id=”ehcache”
<property name=”configLocation” value=”classpath:ehcache.xml”/>


Caching is setup via annotation, as configured in the tag cache:annotation-driven. Next a cache manager is defined and is referenced to a factory bean for instantiating Ehcache. The bean org.springframework.cache.ehcache.EhCacheManagerFactoryBean configLocation property is set to the ehcache.xml setup files where we will define the caches used by the application.

The ehcache.xml config file

I recommend starting with the ehcache-failsafe.xml found in the ehcache-core jar file and add the caches required by the application. An example for this blog is included below:

<!– Sample cache named sampleCache1
This cache contains a maximum in memory of 10000 elements, and will expire
an element if it is idle for more than 5 minutes and lives for more than
10 minutes.

If there are more than 10000 elements it will overflow to the
disk cache,
<cache name=”sampleCache1″

It is also necessary to set the directory to store the files when caches overflow into disk storage. To do this, update the path attribute in the diskStore tag:

 <diskStore path=”${CACHE_DIR}”/>

Note the use of variable CACHE_DIR above. This will allow us to define the directory using the Java JVM variable (-DCACHE_DIR) on different environments.

Adding Cache Annotations

Now we are ready to do something useful. To add caching to a method, use the @Cacheable annotation as shown below. I typically would add caching in the service layer.

    @Cacheable(value=”sampleCache1″, key=”#id”)
public Product load(String id) {“load product with id” + id);
return repository.load(id);

I recommend setting the key explicitly instead of relying on the default key generator. Alternatively, you could configure your own key generator.

To evict an object in the cache, use @CacheEvict annotation. For example, we would have to evict the object in the cache in the update method.

    @CacheEvict(value=”sampleCache1″, key=””, beforeInvocation=false /*default*/, allEntries=false /*default*/)
public void update(Product product) {

@CacheEvict annotation provides 2 handy attributes: beforeInvocation can be set to evict the object in the cache before or after (default) the method is executed. allEntries can be used to evict all objects in the cache. Only the object with the key is evicted by default.