Exporting DataGrid to Excel

Discussion in 'ASP .Net' started by =?Utf-8?B?TmVpbA==?=, Feb 9, 2004.

  1. 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.
    =?Utf-8?B?TmVpbA==?=, Feb 9, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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.
    Steve C. Orr [MVP, MCSD], Feb 9, 2004
    #2
    1. Advertising

  3. 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
    =?Utf-8?B?cGRtMg==?=, Apr 3, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ravindra
    Replies:
    0
    Views:
    763
    Ravindra
    Dec 14, 2005
  2. http://www.visual-basic-data-mining.net/forum

    Exporting Datagrid's data to an Excel Sheet.

    http://www.visual-basic-data-mining.net/forum, Oct 25, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    579
    Steve C. Orr [MVP, MCSD]
    Oct 25, 2004
  3. Goran Djuranovic
    Replies:
    0
    Views:
    351
    Goran Djuranovic
    Dec 10, 2004
  4. Luis Esteban Valencia
    Replies:
    1
    Views:
    1,371
    Carl Prothman [MVP]
    Jan 12, 2005
  5. Doogie
    Replies:
    1
    Views:
    443
    sloan
    Nov 19, 2008
Loading...

Share This Page