Export to Excel C#

Discussion in 'ASP .Net Datagrid Control' started by Ugo, Jun 4, 2004.

  1. Ugo

    Ugo Guest

    Hi,
    I am having a problem exporting a Datagrid to excel in asp.net. For
    some reason I am getting a blank excel page. I set up in IIS the Mime for
    ..xls. My code is below any help would be appreciated..

    Thanks

    CODE:

    private void ExportToExcel()
    {
    Response.Clear();
    Response.ContentType = "application/vnd.ms-excel";
    Response.ContentEncoding = System.Text.Encoding.Default;
    Response.Charset = "";
    this.EnableViewState = false;
    StringWriter tw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(tw);
    DataGrid dg = new DataGrid();
    dg.DataSource = (DataSet) Cache["dataset"];
    dg.DataBind();
    dg.RenderControl(hw);
    Response.Write(tw.ToString());
    Response.Flush() ;
    Response.Close();
    }
    Ugo, Jun 4, 2004
    #1
    1. Advertising

  2. You might want to compare your code to the sample here to see what is
    different about your approach:

    HOW TO: Export Data in a DataGrid on an ASP . NET WebForm to Microsoft Excel

    http://support.microsoft.com/default.aspx?scid=kb;en-us;317719

    "Ugo" <> wrote in message
    news:%...
    > Hi,
    > I am having a problem exporting a Datagrid to excel in asp.net.
    > For
    > some reason I am getting a blank excel page. I set up in IIS the Mime for
    > .xls. My code is below any help would be appreciated..
    >
    > Thanks
    >
    > CODE:
    >
    > private void ExportToExcel()
    > {
    > Response.Clear();
    > Response.ContentType = "application/vnd.ms-excel";
    > Response.ContentEncoding = System.Text.Encoding.Default;
    > Response.Charset = "";
    > this.EnableViewState = false;
    > StringWriter tw = new StringWriter();
    > HtmlTextWriter hw = new HtmlTextWriter(tw);
    > DataGrid dg = new DataGrid();
    > dg.DataSource = (DataSet) Cache["dataset"];
    > dg.DataBind();
    > dg.RenderControl(hw);
    > Response.Write(tw.ToString());
    > Response.Flush() ;
    > Response.Close();
    > }
    >
    >
    Ken Cox [Microsoft MVP], Jun 4, 2004
    #2
    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. =?Utf-8?B?RGF2aWQgVmFsbGU=?=

    Invalid export DLL or export format

    =?Utf-8?B?RGF2aWQgVmFsbGU=?=, Oct 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    3,614
    =?Utf-8?B?RGF2aWQgVmFsbGU=?=
    Oct 29, 2003
  2. =?Utf-8?B?U3JpZGhhcg==?=

    using Microsoft Excel image for Export to Excel button

    =?Utf-8?B?U3JpZGhhcg==?=, Dec 9, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    3,098
    =?Utf-8?B?U3JpZGhhcg==?=
    Dec 9, 2005
  3. =?Utf-8?B?SGVtYW50IFNpcGFoaW1hbGFuaQ==?=

    Export to Excel (Default File Type - Excel)

    =?Utf-8?B?SGVtYW50IFNpcGFoaW1hbGFuaQ==?=, Apr 24, 2006, in forum: ASP .Net
    Replies:
    15
    Views:
    17,512
    syed hassan
    May 21, 2009
  4. Grey
    Replies:
    4
    Views:
    1,965
    Mark Rae [MVP]
    Oct 17, 2007
  5. Maarten Porters
    Replies:
    1
    Views:
    426
    Florian Gilcher
    Jul 28, 2008
Loading...

Share This Page