Export to Excel (Default File Type - Excel)

G

Guest

The following piece of code is being used to export HTML to excel.

HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
HttpContext.Current.Response.AddHeader("content-disposition",
"attachment;filename=ABC.xls")
HttpContext.Current.Response.Write(strHTML)
HttpContext.Current.Response.End()

However when the user tries to save it the Default File Type is Web
Page(*.htm; *.html)
How do we change the Default File Type to Excel
 
S

Steve C. Orr [MVP, MCSD]

Try putting a Response.Clear() line at the beggining of your code block.
 
G

Guest

Steve,
Thank you for the prompt reply. Response.Clear does not help though.
Listing out the full piece of code for better clarity.

Dim strFileName As String
Dim strHTML As String
Dim strAddInfo As String

HttpContext.Current.Response.Clear()
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
strReportName = "EXPORT REPORT"
strFileName = "ABC.xls"
strAddInfo = "Additional Info"
strHTML = GetGHTML() 'This is responsible for getting report in HTML format
HttpContext.Current.Response.AddHeader("content-disposition",
"attachment;filename=" & strFileName)
HttpContext.Current.Response.Charset = ""
HttpContext.Current.Response.Write("<h4>" & strReportName & "</h4>")
HttpContext.Current.Response.Write(strAddInfo)
HttpContext.Current.Response.Write("<br><br>")
HttpContext.Current.Response.Write(strHTML)
HttpContext.Current.Response.End()
Hemant
 
J

Jeff Dillon

We have used this code:

objHttpContext.Response.Clear()
objHttpContext.Response.Buffer = True
objHttpContext.Response.ContentEncoding = System.Text.Encoding.UTF8
objHttpContext.Response.ContentType = "application/vnd.ms-excel"
objHttpContext.Response.AddHeader("Content-disposition",
"attachment;filename=export.xls")
objHttpContext.Response.Charset = ""
 
G

Guest

Thanks Jeff ,
but this still does not show the default File Type as xls when the user
tries to save the file.
Hemant
 
J

Jeff Dillon

Put this all by itself in test.aspx:

<%

Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-disposition", "attachment;filename=export.xls")
Response.Charset = ""
Response.Write("Hello world")
Response.End()

%>
 
G

Guest

Jeff,
You are correct. The piece of code that you have written does show the
Exported file as "Text (tab delimited) (*.txt) " in the "Save as type"
dropdown when the user tries to save the excel file.
However in my case the data is in tabular format as opposed to "Hello
World".
The data that I am trying to export is similar to

strHTML = "<table><tr><td>1</td><td>2</td><td>3</td></tr></table>"

HttpContext.Current.Response.Clear()
HttpContext.Current.Response.Buffer = True
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
HttpContext.Current.Response.AddHeader("Content-disposition",
"attachment;filename=export.xls")
HttpContext.Current.Response.Charset = ""
HttpContext.Current.Response.Write(strHTML)
HttpContext.Current.Response.End()

In this case when the user tries to save the exported excel the "Save as
Type" drop down shows the type as a "Web Page". This is something that I am
trying to avoid.

Thanks,
Hemant
 
J

Jeff Dillon

On ALL our machines, my code shows Save As Excel Sheet in the Save As dialog
popup.

Do you have Excel installed on your client machines?? Which is a requirement
of course, to save in that format.

Jeff
 
G

Guest

Jeff,
I have the following versions installed on my machine.
1. Excel - Microsoft Excel 2003 (11.5612.5606)
2. IE - 6.0.2900.2180 , SP2
Thanks,
Hemant
 
G

Guest

Jeff,
MIME settings are set up to open a file with xls in Excel sheet. This is
exactly what the code does. It is only when you try to save the file, the
"Save as Type" is a "Web Page" as opposed to "xls".
Is it possible to send across the link to the test report on your
application across to me? Curious to see how it works against my Test bed.
Thanks,
Hemant
 
G

Guest

Hemant,
We also having same issue with Export to Excel, tried all possible code
solutions but didn't worked out,
Curious to know if you would have got solution as you also facing same issue.
Plz let me know if have any solution..
 
G

Guest

Hari,
There is no solution that I could find out. The workaround for this is to
use the Excel Object and create an excel on the server itself, which will
have to be ultimately downloaded on the client. But this method has it's own
limitations.
Regards,
Hemant
 

ckv

Joined
Oct 16, 2006
Messages
1
Reaction score
0
Viewstate

I ran across the same issue where the default file type was HTML but worked fine on other pages. Turned out I had turned off the viewstate on the page to try to help performance. Once viewstate was turned back on it worked fine.
 
Joined
May 21, 2009
Messages
1
Reaction score
0
Hi i have used a java code in an application
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition","inline; filename=report.xls");

to export a java code to an excel. this is working fine.
When i click on the 'save' button in the popup, iam able to save the file. but when i click on 'open' button and try to save the spreadsheet once its opened, the file type i defaulted as txt, Could you please suggest ways for the same so that the default type should remain xls
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top