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); } } } }
|
| Author: Nisha Saini 03 Jun 2009 | Member 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 2009 | Member 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
|