#region Export Grid to Excel
public static void Export(string File, GridView GridView1, string CompanyName, string ReportName, string Criteria1, string Criteria2,int FirstColSpan,int SecColSpan,int ThirdColSpan,int ForthColSpan)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + File);
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Table table = new Table();
TableRow Tr = new TableRow();
TableCell TC = new TableCell();
TC.Text = CompanyName;
TC.ColumnSpan = FirstColSpan;
TC.Font.Size = 22;
TC.Font.Bold = true;
TableCell TC1 = new TableCell();
TC1.Text = ReportName;
TC1.ColumnSpan = SecColSpan;
TC1.Font.Size = 22;
TC1.Font.Bold = true;
TC1.HorizontalAlign = HorizontalAlign.Right;
Tr.Cells.Add(TC);
Tr.Cells.Add(TC1);
TableRow Tr1 = new TableRow();
TableCell TC2 = new TableCell();
TC2.Text = Criteria1;
TC2.ColumnSpan = ThirdColSpan;
TC2.HorizontalAlign = HorizontalAlign.Left;
TC2.Font.Size = 12;
TC2.Font.Bold = true;
TableCell TC3 = new TableCell();
TC3.Text = Criteria2;
TC3.ColumnSpan = ForthColSpan;
TC3.Font.Size = 12;
TC3.Font.Bold = true;
TC3.HorizontalAlign = HorizontalAlign.Right;
Tr1.Cells.Add(TC2);
Tr1.Cells.Add(TC3);
table.Rows.Add(Tr);
table.Rows.Add(Tr1);
table.GridLines = GridView1.GridLines;
if (GridView1.HeaderRow != null)
{
PrepareControlForExport(GridView1.HeaderRow);
table.Rows.Add(GridView1.HeaderRow);
}
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
row.BackColor = System.Drawing.Color.White;
row.Attributes.Add("class", "textmode");
}
foreach (GridViewRow row in GridView1.Rows)
{
PrepareControlForExport(row);
table.Rows.Add(row);
GridViewRow oGridViewRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
}
if (GridView1.FooterRow != null)
{
PrepareControlForExport(GridView1.FooterRow);
table.Rows.Add(GridView1.FooterRow);
}
table.RenderControl(hw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
private static void PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current is LinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
}
else if (current is ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
}
else if (current is HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
}
else if (current is DropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
}
else if (current is CheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
}
if (current.HasControls())
{
Util.PrepareControlForExport(current);
PrepareControlForExport(current);
}
}
}
#endregion