Spring Mvc Download Excel File

  1. Mvc Download File Example
  2. Spring Mvc Download Excel File Icon
  3. Spring Mvc Download Excel File
  4. Spring Mvc File Download Example
  5. Spring Mvc Download Excel File Example

Export as excel is one of the most wanted feature in an enterprise application. In this tutorial let us learn about export as excel feature using Spring MVC framework. If you are a beginner, go through the Spring MVC tutorial before taking this. We will be using Spring 3 annotation based approach. In Spring MVC application, to download a resource such as a file to the browser, you need to do the following in your controller. Use the void return type for your request-handling method and add HttpServletResponse as an argument to the method. Set the response’s content type to the file’s. 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.

Details
Written by Nam Ha Minh
Last Updated on 25 June 2019 | Print Email
In this article, we are going to show you how to implement file download functionality in a Spring MVC application. The solution is similar to the one described in the article: Send files from servlet to client for downloading, but is implemented in a Spring MVC application.The following picture depicts workflow of the sample application we are going to build:Project structure (Eclipse project):The file to be downloaded in this application is SpringProject.zip file which resides in the downloads directory which is relative to the application’s directory.

1. Code of download page

Create index.jsp file under WebContent directory with the following HTML code:This page simply shows a link “Click here to download file” with URL points to the relative path: download.do. We’ll configure Spring controller class to handle this URL.


2. Code of Spring controller class

Create FileDownloadController.java file under the source package net.codejava.spring with the following code:This is a typical Spring controller class which is annotated by Spring MVC annotation types. The method doDownload() will receive requests from the client, read the file on server and send it to the client for downloading. Note that, unlike traditional Spring controller’s methods, the method doDownload()does not return a view name, because our purpose is to send a file to the client. The method exits as soon as the file is completely transferred to the client.


3. Code of Spring configuration file

Create spring-mvc.xml file under WebContentWEB-INF directory with the following content:This is a deadly simple Spring configuration file which tells the framework to scan the package net.codejava.spring for annotated types (element <context:component-scan />). Of course your application will have some bean definitions, but for the purpose of this application, such configuration is enough to work.


4. Code of web.xml

The Spring dispatcher servlet is configured to handle requests in the web.xml file as follows:

Mvc Download File Example

5. Required jar files

Add the following jar files into the WebContentWEB-INFlib directory:
    1. commons-logging-1.1.1.jar
    2. spring-beans-3.2.1.RELEASE.jar
    3. spring-context-3.2.1.RELEASE.jar
    4. spring-core-3.2.1.RELEASE.jar
    5. spring-expression-3.2.1.RELEASE.jar
    6. spring-web-3.2.1.RELEASE.jar
    7. spring-webmvc-3.2.1.RELEASE.jar
The Commons Logging jar files can be downloaded from Apache Commons Logging, other jar files come from Spring framework 3.2.1 RELEASE download.


6. Testing the application

Deploy the application on localhost Tomcat server, type the following URL into browser’s address bar:

http://localhost:8080/FileDownloadSpringMVC/

The download page is displayed:Click on the link, the browser will ask to download the file:You can download Eclipse project for this application as well as deployable WAR file in the attachment section below.NOTES:One may ask why not just putting a file somewhere on the server and give the users a link to download it? Of course that will work, however that is a static way. By handling the file to be downloaded programmatically, we can obtain the following benefits:
    • Delivering the files dynamically, based on user’s requests.
    • Controlling access to the files: who can download and when the download is available.
    • Hiding the actual location of files on the server.

Spring Mvc Download Excel File Icon

Excel

Spring Mvc Download Excel File


Related File Download Tutorials:

Other Spring Tutorials:


About the Author:

Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook.
Attachments:
[Deployable WAR file]3341 kB
[Eclipse project]3344 kB
Active2 years, 9 months ago

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.

Spring Mvc File Download Example

The setup:

spring-mvc 3.2.7poi 3.10.1Tomcat 8.0

Controller method:

Abstract Custom View:

Spring Mvc Download Excel File

ExcelBuilder:

Response Header:

It confuses me that the charset will be set, when this is binary data. Could that be the problem?

user3734130user3734130

2 Answers

Don't return ModelAndView but just write the excel file to the response's outputStream

Check all the streams flushed/closed

StanislavLStanislavLDownload
52.9k8 gold badges46 silver badges81 bronze badges

I 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.

The Student Soul
1,0701 gold badge7 silver badges10 bronze badges
betatester07betatester07

Spring Mvc Download Excel File Example

Not the answer you're looking for? Browse other questions tagged javaexcelspringspring-mvc or ask your own question.