Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Exporting DataGrid to Excel

Reply
Thread Tools

Exporting DataGrid to Excel

 
 
=?Utf-8?B?TmVpbA==?=
Guest
Posts: n/a
 
      02-09-2004
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.
 
Reply With Quote
 
 
 
 
Steve C. Orr [MVP, MCSD]
Guest
Posts: n/a
 
      02-09-2004
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/NewsletterA...200309so_l.asp

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



"Neil" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
 
 
 
=?Utf-8?B?cGRtMg==?=
Guest
Posts: n/a
 
      04-02-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exporting to Excel using new version of Excel Doogie ASP .Net 1 11-19-2008 09:10 PM
Exporting the datagrid to excel, text and html files. Ravindra ASP .Net 0 12-14-2005 04:14 AM
exporting an excel file from database; making changes to excel file and updating the database by importing it back Luis Esteban Valencia ASP .Net 1 01-12-2005 12:28 AM
Characters bad when exporting asp.net datagrid to excel Goran Djuranovic ASP .Net 0 12-10-2004 06:44 PM
Exporting Datagrid's data to an Excel Sheet. http://www.visual-basic-data-mining.net/forum ASP .Net 1 10-25-2004 10:24 PM



Advertisments