Solved

JSP export to Excel

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction (All good things must come to an end (http://en.wikipedia.org/wiki/All_Good_Things...)) The original MySQL API (http://php.net/manual/en/book.mysql.php) has gone away, deprecated by PHP in Version 5.5, and removed from PHP in all curre…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

747 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

13 Experts available now in Live!

Get 1:1 Help Now