Tuesday, August 6, 2013

Struts Export JSP to Excel Tutorial

01.<%@page contentType="text/html" pageEncoding="UTF-8"%>
02.<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
03.<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
04.<html>
05.<head>
06.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
07.<title>User Details</title>
08.</head>
09.<body>
10.<table cellpadding="3" cellspacing="3" border="1">
11.<tr>
12.<th>
13.User Name
14.</th>
15.<th>
16.Email Id
17.</th>
18.<th>
19.Location
20.</th>
21.</tr>
22.<logic:iterate id="data" name="ExcelForm" property="userList">
23.<tr>
24.<td>
25.<bean:write name="data" property="userName" />
26.</td>
27.<td>
28.<bean:write name="data" property="emailId" />
29.</td>
30.<td>
31.<bean:write name="data" property="location" />
32.</td>
33.</tr>
34.</logic:iterate>
35.</table>
36.<a href="exportUser.jsp" >Excel</a>
37.</body>
38.</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.
01.<%@page contentType="application/vnd.ms-excel" pageEncoding="UTF-8"%>
02. 
03.<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
04.<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
05.<html>
06.<head>
07.<title>User Details</title>
08.</head>
09.<body>
10.<table cellpadding="3" cellspacing="3" border="1">
11.<tr>
12.<th>
13.User Name
14.</th>
15.<th>
16.Email Id
17.</th>
18.<th>
19.Location
20.</th>
21.</tr>
22.<logic:iterate id="data" name="ExcelForm" property="userList">
23.<tr>
24.<td>
25.<bean:write name="data" property="userName" />
26.</td>
27.<td>
28.<bean:write name="data" property="emailId" />
29.</td>
30.<td>
31.<bean:write name="data" property="location" />
32.</td>
33.</tr>
34.</logic:iterate>
35.</table>
36.</body>
37.</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.
1.<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.

No comments:

Post a Comment