473,320 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Problem transfering datagrid to Excel

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

Nov 19 '05 #1
3 1786
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
<nk*****@plusfunds.com> wrote in message
news:11**********************@g44g2000cwa.googlegr 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

Nov 19 '05 #2
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
<nk*****@plusfunds.com> wrote in message
news:11**********************@g44g2000cwa.googlegr 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


Nov 19 '05 #3
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
<nk*****@plusfunds.com> wrote in message
news:11**********************@g14g2000cwa.googlegr 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
<nk*****@plusfunds.com> wrote in message
news:11**********************@g44g2000cwa.googlegr 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
>

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve Chatham | last post by:
I use the following code: Private Sub RbtnExport_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RbtnExport.SelectedIndexChanged Dim sFile As String =...
0
by: mutta | last post by:
I am developping an asp.net application and I have to export some data from a dataset to word and excel file. The language of this data is bulgarian. For excel I use a datagrid, and after I...
1
by: RSB | last post by:
Hi Everyone, i am using the following code to transfer a DataGrid to Excel File. Every thing works ok beside the long numerice value like 0000121900000000 gets converted to 1.219E+11. how can i...
1
by: NancyA | last post by:
I am using the following code to write data from a datagrid to an Excel file: Dim tw As New System.IO.StringWriter Dim hw As New System.Web.UI.HtmlTextWriter(tw) dg.RenderControl(hw)...
1
by: Mustufa Baig | last post by:
I have an ASP.NET website where I am showing off crystal reports to users by exporting them to pdf format. Following is the code: ---------------- 1 Private Sub ExportReport() 2 Dim oStream...
3
by: Bidarkota | last post by:
When i export DataGrid to Excel all the HTML contents are also exporting to excel. i am using stylesheets in the ASPX Page and i am getting an alert message that stylesheets are missing. i need to...
0
by: kieran | last post by:
Hi, I have an 'export to excel' button on my Datagrid. All works well except that on my Datagrid, I have sub total rows. The subtotal rows use colspan so that the subtotals are given above...
4
by: Frank | last post by:
Hello All, I ham using VS.NET 2003. Have followed instructions from http://gridviewguy.com/ArticleDetails.aspx?articleID=26 along with several other articles to no avail. I am pulling my hair...
0
by: santosh1234 | last post by:
Hi, I have a problem in importing from an excel file. I am using the connection string for oledb to convert the data from the excel file into a datatable and proceed thereafter. Code I am using...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.