• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1674
  • Last Modified:

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.
0
mkarthik415
Asked:
mkarthik415
4 Solutions
 
Sathish David Kumar NCommented:

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 NCommented:
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 NCommented:
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 NCommented:
<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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now