Pages Navigation Menu

Coding is much easier than you think

Export Grid View to Excel in Servlet and Jsp

Exporting contents to excel spreadsheet is a much required functionality for almost every data driven website. In this article I am going to explain in detail on how to export gridview contents to an excel sheet via a java web application.
 
Export to Excel
 

Project Structure

 
Project Structure
 

Servlet

 

package servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Student;

public class ExportToExcel extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		ArrayList students = new ArrayList();
		students.add(new Student("Mohaideen", "CSE", 17));
		students.add(new Student("Nilafar", "IT", 16));
		students.add(new Student("Thasleema", "CSE", 16));


		request.setAttribute("students", students);

		RequestDispatcher rd = request.getRequestDispatcher("report.jsp");
		rd.forward(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		ArrayList students = new ArrayList();
		students.add(new Student("Mohaideen", "CSE", 17));
		students.add(new Student("Nilafar", "IT", 16));
		students.add(new Student("Thasleema", "CSE", 16));

		request.setAttribute("students", students);

		RequestDispatcher rd = request.getRequestDispatcher("exportExcel.jsp");
		rd.forward(request, response);
	}
}

 

Model

 

package model;

public class Student {
	private String name;
	private String department;
	private int age;

	public Student(String name, String department, int age) {
		this.name = name;
		this.department = department;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public String getDepartment() {
		return department;
	}

	public int getAge() {
		return age;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public void setAge(int age) {
		this.age = age;
	}
}

 

JSP Page

 
File : index.jsp – Used to navigate to exporttoexcel servlet action








 
File : report.jsp – This page used to display gridview filled with data of student table from server side.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.List"%>
<%@ page import="model.Student"%>


Export to Excel


Export to Excel Example

<% List students = (List) request .getAttribute("students"); for (Student std : students) { %> <% } %>
Name Department Age
<%=std.getName()%> <%=std.getDepartment()%> <%=std.getAge()%>

 
File : exportExcel.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.List"%>
<%@ page import="model.Student"%>


Export to Excel Example


Export to Excel Example

<% List students = (List) request.getAttribute("students"); if (students != null) { response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "inline; filename=" + "StudentReport.xls"); } for (Student std : students) { %> <% } %>
Name Department Age
<%=std.getName()%> <%=std.getDepartment()%> <%=std.getAge()%>

 

web.xml

 


  ExportToExcel
  
    index.jsp
  
  
    ExportToExcel
    servlet.ExportToExcel
  
  
    ExportToExcel
    /exporttoexcel
  

 

Demo

 
On running the application

Running
 
On clicking “Export To Excel” button the following page with download file options get appears

Export to excel
 

Leave a Comment

Your email address will not be published. Required fields are marked *