Solved

JSP export to Excel

Posted on 2009-05-05
2
466 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now