Problem transfering datagrid to Excel

Discussion in 'ASP .Net' started by nkunkov@plusfunds.com, May 24, 2005.

  1. Guest

    Hi,
    I have read a lot of articles in this newsgroup about how to solve this
    problem but found no solution. I'm trying to export a C# datagrid to
    Excel file.
    Here is my code that I have also found on google:

    MyDataGrid.EnableViewState = false;
    MyPage.Response.Clear();
    MyPage.Response.Buffer = true;
    MyPage.Response.AddHeader( "Content-disposition",
    "filename="+reportName);
    MyPage.Response.ContentType="application/vnd.ms-excel";
    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    MyDataGrid.RenderControl(htmlWriter);
    MyPage.Response.Write(stringWriter.ToString());
    MyPage.Response.End();

    The above works great when my datagrid has a few rows.
    When I get a few hundred rows, Excel wouldn't open and I get page can't
    be displayed error. It looks like it's a known problem and there are a
    lot of questions like that in this newsgroup. The problem is that I
    couldn't find an answer. Did anyone solve this?
    I'm new to C# and .Net so bear with me if it's a stupid question.
    Please, let me know if I can work around this somehow.
    Thanks in advance.
    NK
    , May 24, 2005
    #1
    1. Advertising

  2. why the X-post? You are running into an excel limitation. About the easiest
    thing you can do is page the datagrid so that it renders 20 rows and caches
    the rest.

    --
    Regards,
    Alvin Bruney - ASP.NET MVP

    [Shameless Author Plug]
    The Microsoft Office Web Components Black Book with .NET
    Now available @ www.lulu.com/owc, Amazon.com etc
    <> wrote in message
    news:...
    > Hi,
    > I have read a lot of articles in this newsgroup about how to solve this
    > problem but found no solution. I'm trying to export a C# datagrid to
    > Excel file.
    > Here is my code that I have also found on google:
    >
    > MyDataGrid.EnableViewState = false;
    > MyPage.Response.Clear();
    > MyPage.Response.Buffer = true;
    > MyPage.Response.AddHeader( "Content-disposition",
    > "filename="+reportName);
    > MyPage.Response.ContentType="application/vnd.ms-excel";
    > StringWriter stringWriter = new StringWriter();
    > HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    > MyDataGrid.RenderControl(htmlWriter);
    > MyPage.Response.Write(stringWriter.ToString());
    > MyPage.Response.End();
    >
    > The above works great when my datagrid has a few rows.
    > When I get a few hundred rows, Excel wouldn't open and I get page can't
    > be displayed error. It looks like it's a known problem and there are a
    > lot of questions like that in this newsgroup. The problem is that I
    > couldn't find an answer. Did anyone solve this?
    > I'm new to C# and .Net so bear with me if it's a stupid question.
    > Please, let me know if I can work around this somehow.
    > Thanks in advance.
    > NK
    >
    Alvin Bruney [MVP - ASP.NET], May 26, 2005
    #2
    1. Advertising

  3. Guest

    Alvin, thanks for you answer.
    I will try to look at caching, I'm not sure yet how to implement it.
    If you can point me to an example I'd greatly appreciate it.
    I also worked out a different solution that lets me generate a file
    while I generate a page.
    I was wondering if you could take a look at the code below and let me
    know if I have some glaring problems. The code works, I just want to
    know if it's ok to use it or if has potential to cause problems for the
    application.


    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    MyDataGrid.RenderControl(htmlWriter);
    string FileName = "myfilename" + ".xls";
    string temp_html_folder = ConfigurationSettings.AppSettings.Get
    ("temp_folder_path");
    string sFullPath = temp_html_folder + FileName;
    FileInfo file = new FileInfo(sFullPath);
    if (file.Exists==true)
    file.Delete();
    TextWriter sWriter = File.CreateText(sFullPath);
    sWriter.WriteLine(stringWriter.ToString()) ;
    tmlWriter.Close();
    sWriter.Flush();
    sWriter.Close();


    Is this a right way to output contents of the datagrid to a file?
    Any potential problems with this code?

    Your help is greatly appreciated.
    Thanks
    NK

    Alvin Bruney [MVP - ASP.NET] wrote:
    > why the X-post? You are running into an excel limitation. About the easiest
    > thing you can do is page the datagrid so that it renders 20 rows and caches
    > the rest.
    >
    > --
    > Regards,
    > Alvin Bruney - ASP.NET MVP
    >
    > [Shameless Author Plug]
    > The Microsoft Office Web Components Black Book with .NET
    > Now available @ www.lulu.com/owc, Amazon.com etc
    > <> wrote in message
    > news:...
    > > Hi,
    > > I have read a lot of articles in this newsgroup about how to solve this
    > > problem but found no solution. I'm trying to export a C# datagrid to
    > > Excel file.
    > > Here is my code that I have also found on google:
    > >
    > > MyDataGrid.EnableViewState = false;
    > > MyPage.Response.Clear();
    > > MyPage.Response.Buffer = true;
    > > MyPage.Response.AddHeader( "Content-disposition",
    > > "filename="+reportName);
    > > MyPage.Response.ContentType="application/vnd.ms-excel";
    > > StringWriter stringWriter = new StringWriter();
    > > HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    > > MyDataGrid.RenderControl(htmlWriter);
    > > MyPage.Response.Write(stringWriter.ToString());
    > > MyPage.Response.End();
    > >
    > > The above works great when my datagrid has a few rows.
    > > When I get a few hundred rows, Excel wouldn't open and I get page can't
    > > be displayed error. It looks like it's a known problem and there are a
    > > lot of questions like that in this newsgroup. The problem is that I
    > > couldn't find an answer. Did anyone solve this?
    > > I'm new to C# and .Net so bear with me if it's a stupid question.
    > > Please, let me know if I can work around this somehow.
    > > Thanks in advance.
    > > NK
    > >
    , May 26, 2005
    #3
  4. you may want to wrap the delete call in an exception block. Deleting files
    that are in use, locked or inaccessible will throw an exception causing a
    memory leak or a crashed app. that's all i see for now.

    --
    Regards,
    Alvin Bruney - ASP.NET MVP

    [Shameless Author Plug]
    The Microsoft Office Web Components Black Book with .NET
    Now available @ www.lulu.com/owc, Amazon.com etc
    <> wrote in message
    news:...
    > Alvin, thanks for you answer.
    > I will try to look at caching, I'm not sure yet how to implement it.
    > If you can point me to an example I'd greatly appreciate it.
    > I also worked out a different solution that lets me generate a file
    > while I generate a page.
    > I was wondering if you could take a look at the code below and let me
    > know if I have some glaring problems. The code works, I just want to
    > know if it's ok to use it or if has potential to cause problems for the
    > application.
    >
    >
    > StringWriter stringWriter = new StringWriter();
    > HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    > MyDataGrid.RenderControl(htmlWriter);
    > string FileName = "myfilename" + ".xls";
    > string temp_html_folder = ConfigurationSettings.AppSettings.Get
    > ("temp_folder_path");
    > string sFullPath = temp_html_folder + FileName;
    > FileInfo file = new FileInfo(sFullPath);
    > if (file.Exists==true)
    > file.Delete();
    > TextWriter sWriter = File.CreateText(sFullPath);
    > sWriter.WriteLine(stringWriter.ToString()) ;
    > tmlWriter.Close();
    > sWriter.Flush();
    > sWriter.Close();
    >
    >
    > Is this a right way to output contents of the datagrid to a file?
    > Any potential problems with this code?
    >
    > Your help is greatly appreciated.
    > Thanks
    > NK
    >
    > Alvin Bruney [MVP - ASP.NET] wrote:
    >> why the X-post? You are running into an excel limitation. About the
    >> easiest
    >> thing you can do is page the datagrid so that it renders 20 rows and
    >> caches
    >> the rest.
    >>
    >> --
    >> Regards,
    >> Alvin Bruney - ASP.NET MVP
    >>
    >> [Shameless Author Plug]
    >> The Microsoft Office Web Components Black Book with .NET
    >> Now available @ www.lulu.com/owc, Amazon.com etc
    >> <> wrote in message
    >> news:...
    >> > Hi,
    >> > I have read a lot of articles in this newsgroup about how to solve this
    >> > problem but found no solution. I'm trying to export a C# datagrid to
    >> > Excel file.
    >> > Here is my code that I have also found on google:
    >> >
    >> > MyDataGrid.EnableViewState = false;
    >> > MyPage.Response.Clear();
    >> > MyPage.Response.Buffer = true;
    >> > MyPage.Response.AddHeader( "Content-disposition",
    >> > "filename="+reportName);
    >> > MyPage.Response.ContentType="application/vnd.ms-excel";
    >> > StringWriter stringWriter = new StringWriter();
    >> > HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    >> > MyDataGrid.RenderControl(htmlWriter);
    >> > MyPage.Response.Write(stringWriter.ToString());
    >> > MyPage.Response.End();
    >> >
    >> > The above works great when my datagrid has a few rows.
    >> > When I get a few hundred rows, Excel wouldn't open and I get page can't
    >> > be displayed error. It looks like it's a known problem and there are a
    >> > lot of questions like that in this newsgroup. The problem is that I
    >> > couldn't find an answer. Did anyone solve this?
    >> > I'm new to C# and .Net so bear with me if it's a stupid question.
    >> > Please, let me know if I can work around this somehow.
    >> > Thanks in advance.
    >> > NK
    >> >

    >
    Alvin Bruney [MVP - ASP.NET], May 29, 2005
    #4
    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. RSB
    Replies:
    1
    Views:
    408
    David Jessee
    Nov 11, 2004
  2. mimisam
    Replies:
    3
    Views:
    2,036
    Roedy Green
    Apr 30, 2004
  3. Replies:
    3
    Views:
    456
  4. =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=

    Problem with Excel reports ::::Excel 2003 Migration To Excel 2007

    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=, Oct 5, 2007, in forum: ASP .Net
    Replies:
    15
    Views:
    1,481
    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=
    Oct 24, 2007
  5. Problem transfering datagrid to Excel

    , May 24, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    107
Loading...

Share This Page