Exporting DataGrid to Excel

Discussion in 'ASP .Net' started by Guest, Feb 9, 2004.

  1. Guest

    Guest 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.
     
    Guest, Feb 9, 2004
    #1
    1. Advertisements

  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



    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.
    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. Advertisements

  3. Guest

    Guest 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
     
    Guest, Apr 3, 2004
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.