/// pass gridveiw into this funtion
private void ExportGridView(GridView gv)
{
string attachment = "attachment; filename=ArchiveOrderList.xls"; //name of excel filename
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
//gvOrderInfo.Parent.Controls.Add(frm);
//gvOrderInfo.Parent.Controls.Add(frm);
this.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv);
frm.RenderControl(htw);
//GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
private void PrepareGridViewForExport(Control gv)
{
LinkButton lb = new LinkButton();
Literal l = new Literal();
string name = String.Empty;
for (int i = 0; i < gv.Controls.Count; i++)
{
if (gv.Controls[i].GetType() == typeof(LinkButton))
{
l.Text = (gv.Controls[i] as LinkButton).Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(DropDownList))
{
l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(CheckBox))
{
l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
gv.Controls.Remove(gv.Controls[i]);
gv.Controls.AddAt(i, l);
}
else if (gv.Controls[i].GetType() == typeof(ImageButton))
{
continue;
}
if (gv.Controls[i].HasControls())
{
PrepareGridViewForExport(gv.Controls[i]);
}
}
}
public override void VerifyRenderingInServerForm(Control control)
{
if (control.ID == GridView1.Id) // here GridView1 is the id of gridview of page
{
//Doing nothing
return;
}
base.VerifyRenderingInServerForm(control);
}
Discover innovative solutions, best practices, and cutting-edge technologies in enterprise architecture
Saturday, April 11, 2009
Export GridView into Excel
below i am describing how to export a grid data..
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment