• home
  • forum
  • my
  • kt
  • download
  • Struts Export JSP to Excel Tutorial

    Author: 2009-04-16 15:08:07 From:

    Export JSP Page to Excel

    In this example we will see how to export a JSP page to Excel. The user.jsp page contains the following contents.

    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.

    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.

    Export JSP Page to Excel

    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.

    Export JSP Page to Excel

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

    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

    discuss this topic to forum

    relation tutorial

    No information

    Category

      Applet Building (9)
      Application Building (9)
      Communication (1)
      Database Related (14)
      Development (13)
      EJB (14)
      Game Programming (5)
      General Java (71)
      Javabeans (4)
      JSP and Servlets (25)
      Miscellaneous (28)
      Networking (1)
      Security (3)
      Swing (13)
      WAP and WML (2)
      XML and Java (4)

    New

    Hot