Tom Pearson

Tom Pearson

  • NA
  • 8
  • 0

help on export of datagrid to excel

Mar 25 2008 11:40 AM

ok...i need to simple drop all html formats on the export?

here is the code i use:

[code]

private void BtnExport_Click(object sender, System.EventArgs e)

{

Response.Clear();

Response.Buffer = true;

Response.ContentType = "application/vnd.ms-excel";

Response.Charset = "";

this.EnableViewState = false;

System.IO.StringWriter oStringWriter = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

this.ClearControls(gridPhysician);

gridPhysician.RenderControl(oHtmlTextWriter);

gridPhysician.AllowPaging = false;

gridPhysician.AllowSorting = false;

Response.Write(oStringWriter.ToString());

Response.End();

}

private void ClearControls(Control ctrl)

{

for (int i = ctrl.Controls.Count - 1; i >= 0; i--)

{

ClearControls(ctrl.Controls[i]);

}

if (!(ctrl is TableCell))

{

if (ctrl.GetType().GetProperty("SelectedItem") != null)

{

LiteralControl literal = new LiteralControl();

ctrl.Parent.Controls.Add(literal);

try

{

literal.Text = (string)ctrl.GetType().GetProperty("SelectedItem").GetValue(ctrl, null);

}

catch

{

}

ctrl.Parent.Controls.Remove(ctrl);

}

else

if (ctrl.GetType().GetProperty("Text") != null)

{

LiteralControl literal = new LiteralControl();

ctrl.Parent.Controls.Add(literal);

literal.Text = (string)ctrl.GetType().GetProperty("Text").GetValue(ctrl, null);

ctrl.Parent.Controls.Remove(ctrl);

}

}

return;

}

[/code]