473,499 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataGrid Contents to Excel - Advanced Methods

Hi,

I am trying to export the contents of a DataGrid to Excel. I have already
found the following articles:

http://support.microsoft.com/default...b;en-us;317719
http://www.c-sharpcorner.com/Code/20...ridToExcel.asp

These methods _only_ work if the following is true:

1.) Paging and sorting is disabled; (produces error stating that controls
must reside within form tag and specify runat=server)
2.) Columns are autogenerated (unless see 3);
3.) If columns are not autogenerated template columns in the form of the
following causes errors; (cannot alter controls that contain <% %>;

<asp:TemplateColumn HeaderText="Day">
<ItemTemplate>
<%# DataBinder.Eval(Container, "DataItem.DayOfWeek") %>
</ItemTemplate>
</asp:TemplateColumn>

The Code I am using is at the bottom of this post.

I was wondering how to go about the following - or if there is a 3rd party
control that does this.

Either:

1.) Pass the original datasource to some kind of custom control. This would
also be given the properties of the datasource (in my case strongly typed
collections) and the control would then loop through using reflection to
Retrieve the values and generate a new HtmlTable that is then rendered to
the Response stream.

2.) Contruct a new grid using the original and only constructing columns
specified (not sure how to manually create a grid yet.)

The first I beleive is far more extensible as it allows my object model and
any available property in it to be exported to excel, but obviously this
will have a longer dev time. Is this even possible.

Any help would be greatly appreciated.

TIA

MattC
protected void ExportToExcel(DataGrid grid)
{
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = String.Empty;
this.EnableViewState = false;

System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new
System.Web.UI.HtmlTextWriter(oStringWriter);

this.ClearControls(grid);
grid.RenderControl(oHtmlTextWriter);

Response.Write(oStringWriter.ToString());

Response.End();
}

protected void ClearControls(Control control)
{
for (int i=control.Controls.Count -1; i>=0; i--)
{
ClearControls(control.Controls[i]);
}

if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text =
(string)control.GetType().GetProperty("SelectedIte m").GetValue(control,null);
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else
if (control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text =
(string)control.GetType().GetProperty("Text").GetV alue(control,null);
control.Parent.Controls.Remove(control);
}
}
return;
}

Nov 19 '05 #1
1 1131
pls ignore.

MattC

"MattC" <m@m.com> wrote in message
news:u7*************@TK2MSFTNGP15.phx.gbl...
Hi,

I am trying to export the contents of a DataGrid to Excel. I have already
found the following articles:

http://support.microsoft.com/default...b;en-us;317719
http://www.c-sharpcorner.com/Code/20...ridToExcel.asp

These methods _only_ work if the following is true:

1.) Paging and sorting is disabled; (produces error stating that controls
must reside within form tag and specify runat=server)
2.) Columns are autogenerated (unless see 3);
3.) If columns are not autogenerated template columns in the form of the
following causes errors; (cannot alter controls that contain <% %>;

<asp:TemplateColumn HeaderText="Day">
<ItemTemplate>
<%# DataBinder.Eval(Container, "DataItem.DayOfWeek") %>
</ItemTemplate>
</asp:TemplateColumn>

The Code I am using is at the bottom of this post.

I was wondering how to go about the following - or if there is a 3rd party
control that does this.

Either:

1.) Pass the original datasource to some kind of custom control. This
would
also be given the properties of the datasource (in my case strongly typed
collections) and the control would then loop through using reflection to
Retrieve the values and generate a new HtmlTable that is then rendered to
the Response stream.

2.) Contruct a new grid using the original and only constructing columns
specified (not sure how to manually create a grid yet.)

The first I beleive is far more extensible as it allows my object model
and
any available property in it to be exported to excel, but obviously this
will have a longer dev time. Is this even possible.

Any help would be greatly appreciated.

TIA

MattC
protected void ExportToExcel(DataGrid grid)
{
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = String.Empty;
this.EnableViewState = false;

System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new
System.Web.UI.HtmlTextWriter(oStringWriter);

this.ClearControls(grid);
grid.RenderControl(oHtmlTextWriter);

Response.Write(oStringWriter.ToString());

Response.End();
}

protected void ClearControls(Control control)
{
for (int i=control.Controls.Count -1; i>=0; i--)
{
ClearControls(control.Controls[i]);
}

if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text =
(string)control.GetType().GetProperty("SelectedIte m").GetValue(control,null);
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else
if (control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text =
(string)control.GetType().GetProperty("Text").GetV alue(control,null);
control.Parent.Controls.Remove(control);
}
}
return;
}

Nov 19 '05 #2

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

Similar topics

1
10900
by: Ugo | last post by:
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...
1
1847
by: Luis Esteban Valencia | last post by:
Here is my aspx code: ============= <%@ Page language="c#" Codebehind="WebForm3.aspx.cs" AutoEventWireup="false" Inherits="PDM.excel.WebForm3" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0...
2
1734
by: Daniel Walzenbach | last post by:
Hi, I have a question regarding the DataGrid control. If paging is enabled the grid binds the data, sets the paging on the top/bottom (or however it is set up) and throws away unnecessary...
0
1708
by: Tim_k | last post by:
Does anyone have an example of how to export a datagrid to an Excel pivot table? The code below exports the grid contents to Excel using the Response object. I'd like to expand it to show the...
3
1793
by: nkunkov | last post by:
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...
3
4335
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...
5
1812
by: Dave | last post by:
In a VB.NET application, I have a datagrid that I export to an Excel spreadsheet like this: ' Set the content type to Excel Response.ContentType = "application/vnd.ms-excel" Dim tw As New...
4
3915
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...
3
1820
by: Mike | last post by:
Hello, I am attempting to export the contents of a datagrid on my webform (using Excel automation) through ASP.NET. I have added the Excel object library to my VS ASP.NET project but I am...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7225
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5479
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4920
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.