By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,969 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,969 IT Pros & Developers. It's quick & easy.

Saving datagrid as Excel file

P: n/a
RSH
Hi,

Im using the code below to render a datagrid as an Excel file. It seems to
work fine when the user clicks on the Save button. But when they click on
the open button, it renders correctly but when the user attempts to save the
document from the browser...it saves it as an HTML file. Is there any way
to disable the Open button and force the user to save the file? Or some
other trick to make sure the file saves as a type xls?

Response.Clear()

Response.Buffer = True

Response.ContentType = "application/vnd.ms-excel"

Response.Charset = ""

Response.AddHeader("Content-Disposition",
"attachment;filename=ClientAlertExport" & Now() & ".xls")

Dim SW As System.IO.StringWriter = New System.IO.StringWriter

Dim HTW As System.Web.UI.HtmlTextWriter = New
System.Web.UI.HtmlTextWriter(SW)

DataGrid1.RenderControl(HTW)

Response.Write(SW.ToString())

Response.End()

Thanks,

Ron
Jun 5 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
It's because you're writing to the disk with HTML
check out this code sample:
http://www.aspnet101.com/aspnet101/a...=ExportDGExcel

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://iWritePro.com

"RSH" <wa*************@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi,

Im using the code below to render a datagrid as an Excel file. It seems
to work fine when the user clicks on the Save button. But when they click
on the open button, it renders correctly but when the user attempts to
save the document from the browser...it saves it as an HTML file. Is
there any way to disable the Open button and force the user to save the
file? Or some other trick to make sure the file saves as a type xls?

Response.Clear()

Response.Buffer = True

Response.ContentType = "application/vnd.ms-excel"

Response.Charset = ""

Response.AddHeader("Content-Disposition",
"attachment;filename=ClientAlertExport" & Now() & ".xls")

Dim SW As System.IO.StringWriter = New System.IO.StringWriter

Dim HTW As System.Web.UI.HtmlTextWriter = New
System.Web.UI.HtmlTextWriter(SW)

DataGrid1.RenderControl(HTW)

Response.Write(SW.ToString())

Response.End()

Thanks,

Ron


Jun 5 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.