You can simplify this by doing the following:
/// <summary>
/// Render
/// </summary>
/// <param name="writer"></param>
protected override void Render(HtmlTextWriter writer)
{
if (Session[ExcelExport] != null && bool.Parse(Session[ExcelExport].ToString()))
{
List<Control> flatList = Utilities.FindControls(this);
foreach (Control item in flatList)
{
if (item.GetType() == typeof(Image))
{
item.Visible = false;
}
if (item.GetType() == typeof(System.Web.UI.DataVisualization.Charting.Chart))
{
item.Visible = false;
}
}
StringBuilder html = null;
using (System.IO.StringWriter stringWrite = new System.IO.StringWriter())
{
using (RewriteLinkHtmlTextWriter htmlWrite = new RewriteLinkHtmlTextWriter(stringWrite))
{
base.Render(htmlWrite);
html = new StringBuilder(stringWrite.ToString());
}
}
DownloadExcel(html.ToString());
}
else
{
base.Render(writer);
}
}