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.
 

 

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<Student> students = new ArrayList<Student>();
		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<Student> students = new ArrayList<Student>();
		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

<html>
<head>
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=exporttoexcel">
</head>
<body>
</body>
</html>

 
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"%>
<html>
<head>
<title>Export to Excel</title>
</head>
<body>
<h3>Export to Excel Example</h3>
<form action="exporttoexcel" method="post">
	<table cellpadding="1" cellspacing="1" border="1" bordercolor="gray">
		<tr>
			<td><b>Name</b></td>
			<td><b>Department</b></td>
			<td><b>Age</b></td>
		</tr>
		<%
			List<Student> students = (List<Student>) request
					.getAttribute("students");
			for (Student std : students) {
		%>
		<tr>
			<td><%=std.getName()%></td>
			<td><%=std.getDepartment()%></td>
			<td><%=std.getAge()%></td>
		</tr>
		<%
			}
		%>
	</table>
	<BR /> <input type="submit" value="Export To Excel" />
</form>
</body>
</html>

 
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"%>
<html>
<head>
<title>Export to Excel Example</title>
</head>
<body>
<h3>Export to Excel Example</h3>
<table cellpadding="1" cellspacing="1" border="1" bordercolor="gray">
	<tr>
		<td>Name</td>
		<td>Department</td>
		<td>Age</td>
	</tr>
	<%
	List<Student> students = (List<Student>) request.getAttribute("students");
		if (students != null) {
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-Disposition", "inline; filename="
					+ "StudentReport.xls");
		}
		for (Student std : students) {
	%>
	<tr>
		<td><%=std.getName()%></td>
		<td><%=std.getDepartment()%></td>
		<td><%=std.getAge()%></td>
	</tr>
	<%
		}
	%>
</table>
</body>
</html>

 

web.xml

 

<web-app .. version="3.0">
  <display-name>ExportToExcel</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>ExportToExcel</servlet-name>
    <servlet-class>servlet.ExportToExcel</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ExportToExcel</servlet-name>
    <url-pattern>/exporttoexcel</url-pattern>
  </servlet-mapping>
</web-app>

 

Demo

 
On running the application

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

Export to excel
 

About Mohaideen Jamil


    %d bloggers like this: