Meyyappan has posted 101 posts at DZone. View Full User Profile

Struts Export JSP to Excel Tutorial

06.15.2012
| 20224 views |
  • submit to reddit
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Details</title>
</head>
<body>
<table cellpadding="3" cellspacing="3" border="1">
    <tr>
        <th>
        	User Name
        </th>
        <th>
        	Email Id
        </th>
        <th>
        	Location
        </th>
    </tr>
    <logic:iterate id="data" name="ExcelForm" property="userList">
        <tr>
            <td>
                <bean:write name="data" property="userName" />
            </td>
            <td>
                <bean:write name="data" property="emailId" />
            </td>
            <td>
                <bean:write name="data" property="location" />
            </td>
        </tr>
    </logic:iterate>
</table>
<a href="exportUser.jsp" >Excel</a>
</body>
</html>

In the user.jsp page we iterate the userList using the <logic:iterate> tag. The id attribute of the <logic:iterate> tag holds an instance of the data present in the userList. userList contains a list of UserData. So the id attribute holds an instance of the UserData. Using the <bean:write> tag we display the data present in the UserData.

The exportUser.jsp page contains the following contents.

<%@page contentType="application/vnd.ms-excel" pageEncoding="UTF-8"%>

<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html>
<head>
<title>User Details</title>
</head>
<body>
<table cellpadding="3" cellspacing="3" border="1">
    <tr>
        <th>
        	User Name
        </th>
        <th>
        	Email Id
        </th>
        <th>
        	Location
        </th>
    </tr>
    <logic:iterate id="data" name="ExcelForm" property="userList">
        <tr>
            <td>
            	<bean:write name="data" property="userName" />
            </td>
            <td>
            	<bean:write name="data" property="emailId" />
            </td>
            <td>
            	<bean:write name="data" property="location" />
            </td>
        </tr>
    </logic:iterate>
</table>
</body>
</html>

The user.jsp page contains the following user details.

On clicking the Excel link in the user.jsp page. The userDetails.jsp page will be displayed in the Excel fromat.

<bean:write name="data" property="userName" />

The above mentioned tag calls the getUserName() method of the UserData class and displays the user name. A link is provided to export the exportUser.jsp page to excel. The exportUser.jsp page contains similar data like the user.jsp page except the contentType attribute of the page directive, which is set to application/vnd.ms-excel.

You can download the source code of the export Jsp to Excel example by clicking on the Download link below.

Source: Download

Source + Lib: Download

Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: