Exporting DataGrid to Excel


G

Guest

Hi
I'm currently exporting my datagrid to excel, taking advantage of the fact that excel can render html, the problem is that when i click the button to export it opens in browser, I want the button to launch Excel and leave the browser as is

Any help appreciate

p.s. I know that this can be achieved by changing the settings on the client machine and de-selecting the 'browse in same window' for .xls extensions but this is not acceptable for my clients.
 
Ad

Advertisements

S

Steve C. Orr [MVP, MCSD]

Some code like this might do the trick for you, depending on the technique
you're using.
Response.AddHeader("Content-Disposition","attachment;filename=myfile.csv");

Here's a detailed tutorial on the subject of exporting to Excel from
ASP.NET:
http://www.aspnetpro.com/NewsletterArticle/2003/09/asp200309so_l/asp200309so_l.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net



Neil said:
Hi,
I'm currently exporting my datagrid to excel, taking advantage of the fact
that excel can render html, the problem is that when i click the button to
export it opens in browser, I want the button to launch Excel and leave the
browser as is.
Any help appreciated

p.s. I know that this can be achieved by changing the settings on the
client machine and de-selecting the 'browse in same window' for .xls
extensions but this is not acceptable for my clients.
 
Ad

Advertisements

G

Guest

I have looked high and low for a simple solution to export a DataGrid to MS Excel in ASP.NET. I weeded out this code from Microsoft support. Simply create a button called ExportToExcel and place this code in the Click event and rename DataGrid1 in the code to whatever your datagrid is called. When you run the page and click your button you will be prompted to open excel in ie or save whatever data is in your datagrid to an excel file of your choice. I hope this saves beginner programmers such as myself some time and effort

sub ExportToExcel_Click(sender As Object, e As EventArgs

' Set the content type to Excel
Response.ContentType = "application/vnd.ms-excel
' Remove the charset from the Content-Type header
Response.Charset = "
' Turn off the view state
Me.EnableViewState = Fals

Dim tw As New System.IO.StringWriter(
Dim hw As New System.Web.UI.HtmlTextWriter(tw
' Get the HTML for the control
DataGrid1.RenderControl(hw
' Write the HTML back to the browser
Response.Write(tw.ToString()
' End the response
Response.End(
End I

End Su
 

Top