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
Spring MVC 4 File Download Example. Download a file in Spring MVC Application by writing it's content to HttpServletResponse output stream, setting required headers.
Spring Mvc Download Excel File From Database
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 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 Free diary download personal diary.
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.
Microsoft Mvc Download
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
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 Download Excel 2017
Access the address URL http://localhost:8080/excel-pdf-view-example/fruits.pdf then the browser render the fruits.pdf file as below
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 (389 downloads) Source code on Github https://github.com/javabycode/spring-mvc-excel-pdf-viewresolver-example Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |