Solved

JSP export to Excel

Posted on 2009-05-05
2
468 Views
Last Modified: 2012-08-14
I have a JSP where I get data from database and display as a report but now I added a href to have  a view in excel .  I have added set content type and set header but no luck - excel file is not opening.

 Below is my code .  

Please help




<table width="98%" border="1" cellspacing="0" align="center">
    <tr align="center">
     <FONT color="#333399" size="4"><STRONG>Summary Report</STRONG></FONT>
	  </tr>
   </table>	
  <div id="blankout">
   <p></p>
 
 
  <center>
 
 
<a type="application/vnd.ms-excel" href="ref_rev_sum_rpt.jsp?msexcel=ref_rev_sum_rpt.xls">
	View in MS Excel </a>  
 <%
 String type=request.getParameter("msexcel");
 
if(type!=null && type.equalsIgnoreCase("msexcel")){
response.setHeader("Content-Disposition", "attachment;filename=ref_rev_sum_rpt.xls");
response.setContentType("application/msexcel");
}
%>
 
 
 
</center>

Open in new window

0
Comment
Question by:Nikki1
2 Comments
 
LVL 2

Accepted Solution

by:
irf44z earned 500 total points
ID: 24310058
1. Looks like there is a bug in your code: if(type!=null && type.equalsIgnoreCase("msexcel")){
     I think, the variable "type" holds the file name and not the constant "msexcel".
     Probably, you need to change it to:
         if (type != null && type.trim().length() > 0) {

2. The Content Type for EXCEL should be "application/vnd.ms-excel"

3. Also, place the scriptlet at the top of the JSP file (May not be mandatory, but just in case)
<%
  String type=request.getParameter("msexcel");
  if (type != null && type.trim().length() > 0) {
    response.setHeader("Content-Disposition", "attachment;filename=ref_rev_sum_rpt.xls");
    response.setContentType("application/vnd.ms-excel");
  }
%>
0
 

Author Closing Comment

by:Nikki1
ID: 31578092
It worked fine with the small tweak in if statement . Thank you
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A Change in PHP Behavior with Session Write Short Circuit (http://php.net/manual/en/book.session.php#116217) (Winter 2014)** With the release of PHP 5.6 the session handler changed in a way that many think should be considered a bug.  See the note …
Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
The viewer will learn how to dynamically set the form action using jQuery.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question