Spring MVC Excel Pdf ViewResolver Example

This tutorial shows you a Spring MVC Excel Pdf ViewResolver Example by using Spring MVC Content negotiation to generate multiple views of the same resource. It means that this example will generate a excel view (.xls or .xlsx format) or pdf view.

Table of contents:
1. Maven Dependencies
2. Project structure
3. Spring MVC Excel Pdf View Configuration
4. Excel Views
5. PDF View
6. HTML View
7. Deploy Spring MVC Excel Pdf ViewResolver Example

Other interesting posts you may like

Now, we start to build Spring MVC Excel Pdf ViewResolver Example step by step

Maven Dependencies
We need to use the following dependencies that supports to create excel document and pdf document.

Project structure
Spring MVC Excel Pdf ViewResolver Example project

Spring MVC Excel Pdf View Configuration
We are using the configureViewResolvers method to register the views such as XlsView, XlsxView, XlsxStreamingView and ItextPdfView.

This is the same Spring XML configuration as above.

Notices: we need to use the DispatcherServlet to mapping the request to the correct controller methods. It is the same DispatcherServlet of the previous Spring MVC so it is not mentioned here or you can see it in the source code attachment.

Controller Endpoint
This Fruit POJO is used to add data to the Excel and Pdf document.

The FruitsController is responsible to create list of Fruit elements for Model which be used to display on the Excel and Pdf document.

Excel Views
The Excel document is with 2 extension format, the .xls is the old format and the .xlsx is the new foramt. The apache POI library is responsible to create excel files.

Implement XlsView using AbstractXlsView
The XlsView is extended from AbstractXlsView. Then we override the buildExcelDocument method to create the excel document.

Implement XlsxView using AbstractXlsxView

Instead of extending the AbstractXlsView, the XlsxView is extended from AbstractXlsxView. The rest part of XlsxView implementation is the same XlsView above.

Implement XLSX Streaming View

If you need to process large excel documents you should use AbstractXlsxStreamingView. The StreamingView will improve the performance of large documents. Notice that some clients are incompatible with this streaming way. So you should consider to use.

PDF view

The new com.itextpdf:itextpdf library is not supported by default. We will create an abstract class and extending from the AbstractView to resolve this issue. The abstract class should be implemented such as below.

The buildPdfMetadata and buildPdfDocument methods are responsible to create the Pdf document. And the renderMergedOutputModel(..) method are responsible to write the pdf document to the response.

Then, we need to create new class ItextPdfView that extends from the AbstractPdfView class and override the buildPdfDocument method to create the real pdf document.

HTML View
The text/html is the default view and this view will be displayed using the same URI.

Deploy Spring MVC Excel Pdf ViewResolver Example

Building project with maven then deploy file war on application server or servlet container (Tomcat 8 for example). Access the address URL http://localhost:8080/excel-pdf-view-example/fruits and the screen will display such as the picture

Spring MVC Excel Pdf ViewResolver Example

Access the address URL http://localhost:8080/excel-pdf-view-example/fruits.xls then the browser render the fruits.xls file. Or ccess the address URL http://localhost:8080/excel-pdf-view-example/fruits.xlsx then the browser render the fruits.xlsx file as below

Spring MVC Excel Pdf ViewResolver Example xls

Access the address URL http://localhost:8080/excel-pdf-view-example/fruits.pdf then the browser render the fruits.pdf file as below

Spring MVC Excel Pdf ViewResolver Example pdf

That’s it on how to create Spring MVC Excel Pdf ViewResolver Example. The next post will show you how to view JSON and XML using Spring MVC Content negotiation.

Download complete source code of example, please click link below

Spring-MVC-Excel-Pdf-ViewResolver-Example.zip (91 downloads)

Source code on Github https://github.com/javabycode/spring-mvc-excel-pdf-viewresolver-example

Leave a Comment

*

Please share it if you found this useful
Hide Buttons