C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET GridView »

Export Gridview to Excel


Posted Date: 29 May 2009    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: Lalit SainiMember Level: Gold    
Rating: 1 out of 5Points: 10



The below code is use for exporting grid with all styles and colors to excel.
For this you have to remove all hyperlinks,anchors and checkboxes first

public void ExportGridToExcel(string FileName, DataGrid ToExport)
{
string strBody = FileIO.GetMailContentFile("GenericGridExport.htm");

ToExport.GridLines = GridLines.Both;
ToExport.BorderColor = System.Drawing.Color.Black;
ToExport.BorderWidth = Unit.Pixel(1);

foreach (DataGridItem i in ToExport.Items)
{
foreach (Control cnt in i.Controls)
{
RemoveHyperlinkURL(cnt);
}

if (i.ItemType == ListItemType.Header)
{
foreach (Control cnt in i.Controls)
{
RemoveAnchorURL(cnt);
}
}
}

HttpResponse objResponse = HttpContext.Current.Response;

objResponse.Clear();
objResponse.ContentType = "application/vnd.ms-excel";
objResponse.Charset = "";
objResponse.AddHeader ("content-disposition", "attachment; filename=\"" + FileName + "\"");

StringWriter txtWriter = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(txtWriter);
ToExport.RenderControl(writer);

string strGrid = txtWriter.ToString();
txtWriter.Close();

string strStyle = FileIO.GetTextFileDataVirtual("/interface/BlueStyleSheet.css");

strBody = strBody.Replace("#grid#", strGrid);
strBody = strBody.Replace("#style#", strStyle);

objResponse.Write(strBody);
objResponse.End();
}

public void ExportRepeaterToExcel(string FileName, Repeater ToExport)
{
HttpResponse objResponse = HttpContext.Current.Response;

string strBody = FileIO.GetMailContentFile("GenericGridExport.htm");

foreach (RepeaterItem i in ToExport.Items)
{
foreach (Control cnt in i.Controls)
{
RemoveHyperlinkURL(cnt);
}
}

objResponse.Clear();
objResponse.ContentType = "application/vnd.ms-excel";
objResponse.Charset = "";
objResponse.AddHeader ("content-disposition", "attachment; filename=\"" + FileName + "\"");

StringWriter txtWriter = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(txtWriter);
ToExport.RenderControl(writer);

string strGrid = txtWriter.ToString();
txtWriter.Close();

string strStyle = FileIO.GetTextFileDataVirtual("/interface/StandardStyles.css");

strBody = strBody.Replace("#grid#", strGrid);
strBody = strBody.Replace("#style#", strStyle);

objResponse.Write(strBody);
objResponse.End();
}*/

public static bool CanBindDataSet(DataSet ToCheck)
{
if (ToCheck.Tables.Count == 0)
return false;

if (ToCheck.Tables[0].Rows.Count == 0)
return false;

return true;
}

public void RemoveHyperlinkURL(Control CurrentControl)
{
if (CurrentControl == null)
return;

if (CurrentControl.GetType() == typeof(HyperLink))
{
HyperLink hyper = (HyperLink) CurrentControl;
hyper.NavigateUrl = "";
hyper.CssClass = "";
}
else
{
foreach (Control cnt in CurrentControl.Controls)
{
if (cnt.GetType() == typeof(HyperLink))
{
HyperLink hyper = (HyperLink) cnt;
hyper.NavigateUrl = "";
hyper.CssClass = "";
}
else
{
if (cnt.Controls.Count > 0)
RemoveHyperlinkURL(cnt);
}
}
}
}

public void RemoveAnchorURL(Control CurrentControl)
{
if (CurrentControl == null)
return;

if (CurrentControl.GetType() == typeof(System.Web.UI.HtmlControls.HtmlAnchor))
{
System.Web.UI.HtmlControls.HtmlAnchor hyper = (System.Web.UI.HtmlControls.HtmlAnchor) CurrentControl;
hyper.HRef = "";
//hyper.Class = "";
}
else
{
foreach (Control cnt in CurrentControl.Controls)
{
if (cnt.GetType() == typeof(System.Web.UI.HtmlControls.HtmlAnchor))
{
System.Web.UI.HtmlControls.HtmlAnchor hyper = (System.Web.UI.HtmlControls.HtmlAnchor) cnt;
hyper.HRef = "";
//hyper.CssClass = "";
}
else
{
if (cnt.Controls.Count > 0)
RemoveAnchorURL(cnt);
}
}
}
}

public void RemoveCheckBox(Control CurrentControl)
{
if (CurrentControl == null)
return;

if (CurrentControl.GetType() == typeof(CheckBox))
{
CheckBox chk = (CheckBox) CurrentControl;
chk.Visible = false;
}
else
{
foreach (Control cnt in CurrentControl.Controls)
{
if (cnt.GetType() == typeof(CheckBox))
{
CheckBox chk = (CheckBox) CurrentControl;
chk.Visible = false;
}
else
{
if (cnt.Controls.Count > 0)
RemoveCheckBox(cnt);
}
}
}
}



Responses

Author: Nisha Saini    03 Jun 2009Member Level: Bronze   Points : 1
very usefull article to export data from grid to excel. It also solve my problem of buttons which appear in excel.

Thank you very much


Author: Sayre    05 Jun 2009Member Level: Gold   Points : 1
Hi, Try this

Gridview to Excel in a minimal line.

http://dotnetspider.com/sites/450/Forum-914-GridView-To-Excel.aspx


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Excel  .  Datagrid  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Adding all Textbox Values in GridView using javascript
Previous Resource: set paging style in GridView
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use