Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Problem transfering datagrid to Excel

Reply
Thread Tools

Problem transfering datagrid to Excel

 
 
nkunkov@plusfunds.com
Guest
Posts: n/a
 
      05-24-2005
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

 
Reply With Quote
 
 
 
 
Alvin Bruney [MVP - ASP.NET]
Guest
Posts: n/a
 
      05-26-2005
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
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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
>



 
Reply With Quote
 
 
 
 
nkunkov@plusfunds.com
Guest
Posts: n/a
 
      05-26-2005
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
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > 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
> >


 
Reply With Quote
 
Alvin Bruney [MVP - ASP.NET]
Guest
Posts: n/a
 
      05-29-2005
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
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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
>> <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) oups.com...
>> > 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
>> >

>



 
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
Problem with Excel reports ::::Excel 2003 Migration To Excel 2007 =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?= ASP .Net 15 10-24-2007 01:34 PM
argument transfering in JNI, problem in calling Java GUI tony_lincoln@yahoo.com Java 3 08-20-2005 01:14 PM
Problem transfering datagrid to Excel nkunkov@plusfunds.com ASP .Net Datagrid Control 1 05-26-2005 05:22 PM
How to Stop Long Numeric to Scientific Conversion if Transfering data from DataGrid to Excel. RSB ASP .Net 1 11-11-2004 12:58 AM
Help needed in File transfering and socket problem mimisam Java 3 04-30-2004 06:14 PM



Advertisments