Code:
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);
}
}
}
}
Bookmarks