0

please can any one help for my requirement I am writing jsp code to get data from bean and display that data in table format in addition to that i need to put download option at the bottom to download the displayed table in "csv" or "Excelsheet" format.

my code will be like this

     <%@page contentType="text/html" pageEncoding="UTF-8"%>
     <%@ page import = "com.preva.vo.StoppageDetails"%>
     <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
     <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
      <html>
      <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

       <link href="css/cal.css" rel="stylesheet" type="text/css" />
    <link href="css/sty.css" rel="stylesheet" type="text/css" />
    <link href="css/tabborder.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
             <jsp:include page="Header.jsp" />

        <table align=center border=0 cellspacing=0 cellpadding=0>
        <tr ><td colSpan=5 align=center><b>Overspeed Details</b></td></tr>
        <tr ><td colspan=5 align=center>
        <b><%=request.getParameter("vehicleId") %></b></td></tr>
        <tr><td>From &nbsp;
         <%=session.getAttribute("fromdate") %> 
          &nbsp;to&nbsp;              
        <%=session.getAttribute("startdate") %></td></tr>

        </table><br></br>

 <table class='rptTbl_sortable' width='80%' align=center  border='0'>

      <thead>
      <tr class="rptHdrRow">
     <th id="index" class="rptHdrCol_sort" nowrap>DeviceID</th>
     <th id="date" class="rptHdrCol_sort" nowrap>Date</th>
     <th id="time" class="rptHdrCol_sort" nowrap>Speed</th>
     <th id="statusdesc" class="rptHdrCol_sort" nowrap>Status</th>
             <th id="address" class="rptHdrCol_sort" nowrap>Address</th>
    </tr>
      </thead>
     <tbody>

       <c:forEach items="${sessionScope.overspeeddetails}" var="overspeeddetailsvar">
       <tr class="rptBodyRowOdd">
         <td><c:out value="${overspeeddetailsvar.deviceID}">     </c:out></td>
     <td><c:out value="${overspeeddetailsvar.TIMESTAMP}"></c:out></td>  
      <td><c:out value="${overspeeddetailsvar.speed}"></c:out></td>
      <td><c:out value="${overspeeddetailsvar.statuscode}"></c:out></td>
      <td><c:out value="${overspeeddetailsvar.address}"></c:out></td>

     </tr>
            <tr class="rptBodyRowEven">
         <td><c:out value="${overspeeddetailsvar.deviceID}"></c:out></td>
            <td><c:out value="${overspeeddetailsvar.TIMESTAMP}"></c:out></td>   
         <td> <c:out value="${overspeeddetailsvar.speed}"></c:out></td>
       <td><c:out value="${overspeeddetailsvar.statuscode}"></c:out></td>
       <td><c:out value="${overspeeddetailsvar.address}"></c:out></td>

         </tr>  </c:forEach>    
    </tbody>            
  </table> 
    </body>
    </html>
Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
hari
  • 33
  • 2
  • 3
  • 10
  • you can use a javascript plugin for that. You can try [datatable](http://datatables.net/extras/tabletools/) – coding_idiot Oct 31 '13 at 03:40
  • you can use display tag see my [answer](http://stackoverflow.com/a/19678408/1031945) – Aniket Kulkarni Oct 31 '13 at 05:16
  • Hi @Aniket i am trying u modify "paging.banner.item_name" property in display tag code but it is not working i.e i given String value as "vehicle"but is still showing as item.can u help me – hari Nov 12 '13 at 07:15
  • @hari : ``. Can you tell me the name of error? – Aniket Kulkarni Nov 12 '13 at 07:20
  • It is not giving any error but it is displaying items instead of vehicles in banner i.e 198 items found,displaying ...shows in banner – hari Nov 12 '13 at 07:50
  • @Aniket I have some problem can u please help me export option in display tag is working but it is not asking me to save or open it simply display all the data in browser – hari Nov 28 '13 at 09:56
  • @hari : post the question and tell where are you stuck? – Aniket Kulkarni Nov 28 '13 at 09:59
  • @Aniket i posted question just check and tell me link is http://stackoverflow.com/questions/20263513/download-link-option-is-not-working-in-display-tag – hari Nov 28 '13 at 10:28

2 Answers2

0

Because your data are stored in session, you can simply create new JSP for CSV, which will contain:

  • modified version of the <c:forEach>
  • text/csv as content type declared in <@ page directive
  • setting of the Content-disposition header to attachment

This page will be linked from your main JSP. For the excel binary format, you will probably need some library for that. From the architecture point of view, this is a typical example of the MVC pattern: you have one controller, which prepares the model (the overspeeddetails in your case), and multiple views: html table, csv and excel file.

Kojotak
  • 2,020
  • 2
  • 16
  • 29
0

Let it point to some servlet which writes the PDF file to the outputstream of the response and, importantingly, sets the content disposition header to 'attachment'. This will cause the webbrowser pop a 'save as' dialogue (the 'download option' as you call yourself).

You may get some ideas out of this: more

Lijo
  • 6,498
  • 5
  • 49
  • 60