Export JSP Page to Excel
<%@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.
|