Simple Excel export for a Spring based web application
Have you ever had the need to provide a simple Excel export for existing data within your web application? Are you already thinking of reporting engines, complex binary formats and complicated merging of data.
This example provides a very simple example how the problem can be solved within 5 minutes.
Jxls Simple Excel reporting library
The example is based on Jxls which is a small Java library that allows for an easy Excel report generation. For simple lists of data it already has an integrated report that can be used without further customisation.
So as always first include the dependency (e.g. within a Maven based project) via
1 2 3 4 5 |
<dependency> <groupId>org.jxls</groupId> <artifactId>jxls-poi</artifactId> <version>1.0.11</version> </dependency> |
Take your data
Jxls is able to generate the report based on POJOs (or simple Entities). So just take your Spring repository or any other list of data. The example is based on a simple Person class that you can see below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class Person { private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } |
So for a Spring based example a JPARepository can be used to fetch all data from a database.
1 |
List<Person> persons = personRepository.findAll(); |
Generate the Reporting
The simplicity now is based on the fact that Jxls includes a template that generates an Excel file based on a list of data. For this list the headers must be defined, the list should be present and the names of the properties of the POJO is needed. And then simply by using the SimpleExporter an Excel file will be generated.
1 2 |
List<String> headers = Arrays.asList("First Name", "Last Name"); new SimpleExporter().gridExport(headers, persons, "firstName, lastName, ", OUTPUTSTREAM); |
The result is directly written to an OutputStream and so we will directly use the functionality in a REST Controller.
Serve via a REST endpoint
So the complete example for exporting a simple Excel via and HTTP URL looks as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@RequestMapping(value = "/export", method = RequestMethod.GET) public void export(HttpServletResponse response) { List<Person> persons = personRepository.findAll(); List<String> headers = Arrays.asList("First Name", "Last Name"); try { response.addHeader("Content-disposition", "attachment; filename=People.xlsx"); response.setContentType("application/vnd.ms-excel"); new SimpleExporter().gridExport(headers, persons, "firstName, lastName, ", response.getOutputStream()); response.flushBuffer(); } catch (IOException e) { log.warn(e.getMessage(), e); } } |
And voila
As you can see in the screenshot a simple Excel is generated. The example can now be extended by using custom templates, more complicated data and so on.
Leave a Comment