How to export table from the jsp to an excel sheet

I have displayed in the jsp
<table>
      <TR>
      <TD><%=i+1 %></TD>
      <TD><%= Obect.getRollNo()%></TD>
      <TD><%=Obect.getName()%></TD>
      <TD><%= Obect.getClass()%></TD>
      <TD><%= Obect.getSchool()%></TD>
      </TR>
      <% i++;
}%>
</table>

How can I export to an excel sheel when clicked on a button

Thank you.
mkarthik415Asked:
Who is Participating?
 
Sathish David Kumar NArchitectCommented:

clock the button that will goto one JSP in that jsp u define this and writeable contenet in  that jsp

<%
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"x.xls\"");
%>
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
a_bCommented:
In the JSP when the button is clicked, read the values of the table in a loop and write that to a comma seperated values file. That is the simplest solution.
0
 
mkarthik415Author Commented:
i have created a new jsp which has this code
<%
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"x.xls\"");
%>

when click on a button new excel file open but empty data how do I put my tables in his jsp so that the excel file has that data
0
 
Sathish David Kumar NArchitectCommented:
u will put this in ur jsp karthik !!

For example
ur jsp have
table
t1 t2
t3 t4

if its like that means it will save in ur excel file ...

u dynamically created one jsp that report some thing(table ) and that Jsp u will put this code !!
0
 
mkarthik415Author Commented:
When put it like what u have suggested dravidnsr, I get the data but with no column or rows in excel sheet
x.xls
0
 
Sathish David Kumar NArchitectCommented:
r u using
follwing thing in ur jsp

 <style name="header" fontColor="green" fontWeight="bold"
         foreground="#C9C9C9" fillPattern="solid-foreground"/>
  <style name="highlight" border="medium-dashed" borderColor="blue"/>

then its return as no borders only ....

remove this code in ur jsp and try !!
0
 
Sathish David Kumar NArchitectCommented:
<cell > realated anything in jsp ????????
0
 
mkarthik415Author Commented:
there is nothing related to cell or style tag in my jsp
0
 
mrcoffee365Commented:
You should follow a_b's advice.  The best way to produce a good-quality csv file (or excel file, for that matter) is to create the file and return a link to that rather than have the .jsp file return the .csv or .xls file directly.

If you still prefer to return the .csv or .xls file in your .jsp file directly, then you need to have the submit button change the behavior of the page to return different http headers (that is the one thing that is more or less correct from dravidnsr) and return the data in comma-separated format.

This question is asked and answered a lot on EE.  You can see answers with some code samples here:
http://www.experts-exchange.com/Programming/Languages/Java/Q_22814071.html
Save csv file to the server and return the link:
http://www.experts-exchange.com/Programming/Languages/Java/J2EE/JSP/Q_22491802.html
0
 
mkarthik415Author Commented:
TY
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.