Excel Export in Spring MVC

Last modified on August 1st, 2014 by Joe.

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 for the web application.

If you have learned Spring MVC, there is not much to discuss, in this excel export tutorial. All you have to know is, Spring provides a class AbstractExcelView which is a view in the MVC. We need to extend this class and write our custom classes. Then configure our custom class as the view in the MVC.

  1. Extend AbstractExcelView and write a custom excel view. There is a method buildExcelDocument in this class, which we should override to build the excel.
  2. Configure the new custom class, which we have created as the view in the Spring MVC. We should imagine this new java class file as a replacement for the JSP which we generally will have in the view.

Do not call the service layer or DAO directly from the view class. Similarly, it should not contain the business logic of the application. Call to service/business should happen from the controller and the model should be built. Only the model should be sent to the view. This view class, should just construct the excel header, rows and formatting.

Maven is used to manage the dependencies. If you do not have Maven, it is better to download and setup Maven.


Domain class

package com.javapapers.spring.mvc;

public class Animal {
	private int id;
	private String animalName;
	private String animalType;
	private boolean aggressive;
	private int weight;

	public Animal(int id, String animalName, String animalType,
			boolean aggressive, int weight) {
		this.id = id;
		this.animalName = animalName;
		this.animalType = animalType;
		this.aggressive = aggressive;
		this.weight = weight;

	public int getId() {
		return id;
	public void setId(int id) {
		this.id = id;
	public String getAnimalName() {
		return animalName;
	public void setAnimalName(String animalName) {
		this.animalName = animalName;
	public String getAnimalType() {
		return animalType;
	public void setAnimalType(String animalType) {
		this.animalType = animalType;
	public boolean getAggressive() {
		return aggressive;
	public void setAggressive(boolean aggressive) {
		this.aggressive = aggressive;
	public int getWeight() {
		return weight;
	public void setWeight(int weight) {
		this.weight = weight;



This is the important class in the excel export tutorial. This serves as the View in Spring MVC by extending the Spring’s AbstractExcelView class.

package com.javapapers.spring.mvc;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class AnimalListExcelView extends AbstractExcelView {

	protected void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		HSSFSheet excelSheet = workbook.createSheet("Animal List");
		List animalList = (List) model.get("animalList");

	public void setExcelHeader(HSSFSheet excelSheet) {
		HSSFRow excelHeader = excelSheet.createRow(0);
	public void setExcelRows(HSSFSheet excelSheet, List animalList){
		int record = 1;
		for (Animal animal : animalList) {
			HSSFRow excelRow = excelSheet.createRow(record++);


Just added couple of methods from the previous MVC hello world tutorial.

package com.javapapers.spring.mvc;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

public class ZooController {

	protected AnimalService animalService = new AnimalService();

	@RequestMapping(value = "/AnimalList", method = RequestMethod.GET)
	public String getAnimals(Model model) {
		List animalList = animalService.getAnimalList();
		model.addAttribute("animalList", animalList);
		return "AnimalList";

	@RequestMapping(value = "/export", method = RequestMethod.GET)
	public ModelAndView getExcel() {
		List animalList = animalService.getAnimalList();
		return new ModelAndView("AnimalListExcel", "animalList", animalList);

	public String hello() {
		return "hello";
	@RequestMapping(value = "/hi", method = RequestMethod.GET)
	public String hi(@RequestParam("name") String name, Model model) {
		String message = "Hi " + name + "!";
		model.addAttribute("message", message);
		return "hi";


Access DAO via service layer for data.

package com.javapapers.spring.mvc;

import java.util.ArrayList;
import java.util.List;
 Our software gurus talk lot about layering
 Generally the layer will be like,
 controller -> Service -> Business -> DAO
 Just to demonstrate that we have this class
public class AnimalService {

	private static List animalList = new ArrayList();

	static {
		animalList.add(new Animal(1, "Lion", "Wild", true, 100));
		animalList.add(new Animal(2, "Tiger", "Wild", true, 90));
		animalList.add(new Animal(3, "Goat", "Domestic", true, 20));
		animalList.add(new Animal(4, "Elephant", "Wild", true, 1000));

	public List getAnimalList() {
		//internally we will call business -> DAO
		return animalList;



A JSP view in the Spring MVC

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

	<h1>Example for Spring MVC Excel Export</h1>

	<h2>Animal List</h2>

	<h3><a href="./export">Export</a></h3>

	<table border="1px" cellpadding="8px">

		<c:forEach items="${animalList}" var="animal">
				<td><c:out value="${animal.id}" /></td>
				<td><c:out value="${animal.animalName}" /></td>
				<td><c:out value="${animal.animalType}" /></td>
				<td><c:out value="${animal.aggressive}" /></td>
				<td><c:out value="${animal.weight}" /></td>



Spring Configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  <context:component-scan base-package="com.javapapers.spring.mvc" />
  <mvc:annotation-driven />
     <bean class="org.springframework.web.servlet.view.XmlViewResolver">
	   <property name="location">
	   <property name="order" value="0" />
    <property name="prefix" value="/view/" />
    <property name="suffix" value=".jsp" />
    <property name="order" value="1" />


Excel Export View Spring Configuration

<beans xmlns="http://www.springframework.org/schema/beans"
 	<bean id="AnimalListExcel"


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

  <display-name>Spring MVC Excel Export</display-name>  



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<name>Spring MVC Excel Export</name>



		<!-- Excel -->





Excel Export Spring MVC

Download Source Code

Comments on "Excel Export in Spring MVC"

  1. Praveen says:

    Good One.. Thanks for explaining in both Spring and JSP

  2. Archi says:

    thanks for this tutorial

  3. Pratik says:

    Thanks Joe , due to your articles only I am in touch with Java.

  4. Rady RIN says:

    Thank you so much!!
    but it download export.do
    How to change this file name to export.xls?

    Best Regards.

  5. Swati says:

    Thank you so much. Its really very helpful to me.

  6. shahrzad says:

    For downloading xls format, add ‘.xls’ to ‘./export’ => ‘./export.xls’ in

    and also

    here :
    @RequestMapping(value = “/export.xls”, method = RequestMethod.GET)

    Merci bcp

  7. Aleksandr says:

    Thx, so much!

  8. lazyguy786 says:

    Hi Joe,

    I had done the export to excel through ajax request

    url: “http://localhost:8080/spring/downloadExcel.htm?myid=”+”myid+”;


    @RequestMapping(value = “/downloadExcel”, method = RequestMethod.GET)
    public ModelAndView downloadExcel(@RequestParam(“myid”) int myid, HttpReq…, HttpResp..){

    Map map = new HashMap();
    map.put(“result”, result); //result is a list
    return new ModelAndView(“exportView”, map);


    public class ExportView extends AbstractExcelView{ //your excel logic}

    applicationcontext.xml :

    It’s going inside the controller But not able to hit the ExportView class
    plz help me

  9. Kiran says:

    How to group and subtotal in excel sheet using POI?

  10. Kiran says:

    create view bean and then return new ModelAndView(“beanId” , “model” , output) from your controller .

  11. michael says:

    Thanks for your example.
    I have a question,how can I change the file name in every request?
    like excel1.xls,excel2.xls
    Best Regards,

  12. Dev says:

    What if the service class is an interface ? How to get the values from DB through it ?

  13. Ankit Agarwal says:

    Thank you so much. This guidance is rally very helpful for me. Thanks

Comments are closed for "Excel Export in Spring MVC".