ASP.NET Excel download defaults to HTML with XLS extension

A

A B

Hello,

I have the following two lines in my VB.NET code to cause a datagrid to
output in Excel format:

Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-disposition","attachment;filename=HubXLOut.xls")

When I click the "Open" dialog box and then Save As ->, the default file
type selected is HTML, not XLS. The file is appropriately named
HubXLOut.xls, however. If I do not change the dropdown to Microsoft
Excel before the save, my .NET application is unable to read this file
using OLEDB. Also, if I click "Save" on the initial dialog box, the
file is saved with an XLS extension, but OLEDB cannot read the file as
an Excel file. I don't want our customers to have to go through the
extra steps of Save As -> and then having to change the dropdown to
Excel. Any suggestions?

Thanks!

AB
 
P

Peter O'Reilly

Your ASP.NET application streams the file as HTML.
Excel takes that stream and interprets in a way that it can present on the
spreadsheet, but it is still HTML content.
There's an OLEDB extension for XLS binary files, but not for HTML formatted
text.

Before your .NET application reads the file, it shoudl detect if it's a
binary or a text format.
If it's binary then either
-use (OLE) Automation to manipulate an instance of Excel to open the file,
save it as XLS and then close it
or
- use the Excel SDK available from MSDN (if it still exists) to do the file
format conversion.

The first option is much easier to implement than the second.

Good luck
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top