?
Solved

JSP export to Excel

Posted on 2009-05-05
2
Medium Priority
?
472 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
irf44z earned 1500 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

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

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

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
This article will show, step by step, how to integrate R code into a R Sweave document
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Suggested Courses

801 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