- This tutorial explains about generating pdf and excel view in spring mvc using content negotiation view resolver. We will have a single controller that can generate excel and pdf views based on the request url.
- In the tutorial, we show you how to create a SpringBoot RestAPIs application that uses Spring JPA to get data from MySQL records and uses Apache POI library to write data to a Excel file.
- Spring MVC offers seamless integration with different view technologies, including Excel document view. When configured properly, a Spring’s view resolver can generate an Excel document from model data and send it to the client for downloading or opening by a spreadsheet program like Microsoft Excel.
- In this article, we demonstrate how to create an Excel, PDF and CSV views using Spring Boot. When configured properly, a Spring’s view resolver can generate the requested document from model data and send it to the client for downloading. The complete code can be found here. Spring MVC View Configuration.
- Aug 17, 2010 Spring MVC comes with AbstractExcelView class to export data to Excel file via Apache POI library. In this tutorial, it show the use of AbstractExcelView class in Spring MVC application to export data to Excel file for download.
This example demonstrates how to use Excel document as the view technology in a Spring MVC application. Spring supports Apache POI library which is used for reading and writing Microsoft Office document formats. In this example, we are going to use AbstractXlsView which is a convenient subclass of Spring View. This class supports Excel format.
I'am working on an excel export functionality in one of my webapps. I set up a little test case and got the download working, but the xlsx file is corrupted and don't know what else I could try. If I write the excel to file it opens without problem, so the error must occure when downloading.
The setup:
spring-mvc 3.2.7poi 3.10.1Tomcat 8.0
Controller method:
Abstract Custom View:
ExcelBuilder:
Response Header:
It confuses me that the charset will be set, when this is binary data. Could that be the problem?
2 Answers
Don't return ModelAndView but just write the excel file to the response's outputStream
Check all the streams flushed/closed
StanislavLStanislavLI would suggest to use existing solution rather than trying to deal with the response stream by yourself.
I would use AbstractExcelView instead of your AbstractPOIExcelView
to do the job. Check this tutorial using AbstractExcelView for inspiration.
For Spring 4.2 or newer use AbstractXlsView (or AbstractXlsxView) because the original AbstractExcelView
is deprecated.
Not the answer you're looking for? Browse other questions tagged javaexcelspringspring-mvc or ask your own question.
Spring MVC comes with AbstractExcelView class to export data to Excel file via Apache POI library. In this tutorial, it show the use of AbstractExcelView class in Spring MVC application to export data to Excel file for download.
1. Apache POI
Get the Apache POI library to create the excel file.
2. Controller
A controller class, generate dummy data for demonstration, and get the request parameter to determine which view to return. If the request parameter is equal to “EXCEL”, then return an Excel view (AbstractExcelView).
File : RevenueReportController.java
3. AbstractExcelView
Create an Excel view by extends the AbstractExcelView class, and override the buildExcelDocument() method to populate the data to Excel file. The AbstractExcelView is using the Apache POI API to create the Excel file detail.
Spring Mvc Download Excel Download
For detail about how to use the Apache POI , please refer to Apache POI documentation
File : ExcelRevenueReportView.java
Spring Mvc Application Examples
Alternatively, you can use the AbstractJExcelView, which is using the JExcelAPI to create the same Excel view, see this AbstractJExcelView example.
4. Spring Configuration
Create a XmlViewResolver for the Excel view.
File : spring-excel-views.xml
5. Demo
URL : http://localhost:8080/SpringMVC/revenuereport.htm?output=excel
It generates an Excel file for user to download.