Tags: | Categories: Blog Posted by admin on 3/9/2009 5:54 PM | Comments (6)

As many asked, this is the code of how to export a datagrid/gridview to Excel.

public void ExportDataGrid(System.Web.UI.WebControls.DataGrid dg)

{

System.Web.HttpContext context = System.Web.HttpContext.Current;

//export to excel

void ExportDataGrid(System.Web.UI.WebControls.DataGrid dg)

{

System.Web.HttpContext context = System.Web.HttpContext.Current;

//export to excel

HttpContext context = System.Web.HttpContext.Current;

//export to excel

//export to excel

context.Response.Clear();

context.Response.Buffer= true;

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

context.Response.Charset = "";

m_pPage.EnableViewState = false;

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

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

this.ClearControls(dg);

dg.RenderControl(oHtmlTextWriter);

context.Response.Write(oStringWriter.ToString());

context.Response.End();

}

private void ClearControls(Control control)

{

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

{

ClearControls(control.Controls[i]);

}

if (!(control is TableCell))

{

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

{

LiteralControl literal = new LiteralControl();

control.Parent.Controls.Add(literal);

try

true;

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

context.Response.Charset = "";

m_pPage.EnableViewState = false;

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

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

this.ClearControls(dg);

dg.RenderControl(oHtmlTextWriter);

context.Response.Write(oStringWriter.ToString());

context.Response.End();

}

private void ClearControls(Control control)

{

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

{

ClearControls(control.Controls[i]);

}

if (!(control is TableCell))

{

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

{

LiteralControl literal = new LiteralControl();

control.Parent.Controls.Add(literal);

try

"application/vnd.ms-excel";

context.Response.Charset = "";

m_pPage.EnableViewState = false;

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

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

this.ClearControls(dg);

dg.RenderControl(oHtmlTextWriter);

context.Response.Write(oStringWriter.ToString());

context.Response.End();

}

private void ClearControls(Control control)

{

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

{

ClearControls(control.Controls[i]);

}

if (!(control is TableCell))

{

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

{

LiteralControl literal = new LiteralControl();

control.Parent.Controls.Add(literal);

try

"";

m_pPage.EnableViewState = false;

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

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

this.ClearControls(dg);

dg.RenderControl(oHtmlTextWriter);

context.Response.Write(oStringWriter.ToString());

context.Response.End();

}

private void ClearControls(Control control)

{

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

{

ClearControls(control.Controls[i]);

}

if (!(control is TableCell))

{

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

{

LiteralControl literal = new LiteralControl();

control.Parent.Controls.Add(literal);

try

false;

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

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

this.ClearControls(dg);

dg.RenderControl(oHtmlTextWriter);

context.Response.Write(oStringWriter.ToString());

context.Response.End();

}

private void ClearControls(Control control)

{

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

{

ClearControls(control.Controls[i]);

}

if (!(control is TableCell))

{

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

{

LiteralControl literal = new LiteralControl();

control.Parent.Controls.Add(literal);

try

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

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

this.ClearControls(dg);

dg.RenderControl(oHtmlTextWriter);

context.Response.Write(oStringWriter.ToString());

context.Response.End();

}

private void ClearControls(Control control)

{

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

{

ClearControls(control.Controls[i]);

}

if (!(control is TableCell))

{

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

{

LiteralControl literal = new LiteralControl();

control.Parent.Controls.Add(literal);

try

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

this.ClearControls(dg);

dg.RenderControl(oHtmlTextWriter);

context.Response.Write(oStringWriter.ToString());

context.Response.End();

}

private void ClearControls(Control control)

{

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

{

ClearControls(control.Controls[i]);

}

if (!(control is TableCell))

{

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

{

LiteralControl literal = new LiteralControl();

control.Parent.Controls.Add(literal);

try

this.ClearControls(dg);

dg.RenderControl(oHtmlTextWriter);

context.Response.Write(oStringWriter.ToString());

context.Response.End();

}

private void ClearControls(Control control)

{

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

{

ClearControls(control.Controls[i]);

}

if (!(control is TableCell))

{

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

{

LiteralControl literal = new LiteralControl();

control.Parent.Controls.Add(literal);

try

private void ClearControls(Control control)

{

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

{

ClearControls(control.Controls[i]);

}

if (!(control is TableCell))

{

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

{

LiteralControl literal = new LiteralControl();

control.Parent.Controls.Add(literal);

try

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

{

ClearControls(control.Controls[i]);

}

if (!(control is TableCell))

{

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

{

LiteralControl literal = new LiteralControl();

control.Parent.Controls.Add(literal);

try

if (!(control is TableCell))

{

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

{

LiteralControl literal = new LiteralControl();

control.Parent.Controls.Add(literal);

try

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

{

LiteralControl literal = new LiteralControl();

control.Parent.Controls.Add(literal);

try

LiteralControl literal = new LiteralControl();

control.Parent.Controls.Add(literal);

try

try

{

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

}

catch

string)control.GetType().GetProperty("SelectedItem").GetValue(control,null);

}

catch

catch

{

}

control.Parent.Controls.Remove(control);

}

else

else

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

{

LiteralControl literal = new LiteralControl();

control.Parent.Controls.Add(literal);

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

control.Parent.Controls.Remove(control);

}

}

return;

}

Happy coding

Cheers

Al

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

{

LiteralControl literal = new LiteralControl();

control.Parent.Controls.Add(literal);

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

control.Parent.Controls.Remove(control);

}

}

return;

}

Happy coding

Cheers

Al

LiteralControl literal = new LiteralControl();

control.Parent.Controls.Add(literal);

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

control.Parent.Controls.Remove(control);

}

}

return;

}

Happy coding

Cheers

Al

string)control.GetType().GetProperty("Text").GetValue(control,null);

control.Parent.Controls.Remove(control);

}

}

return;

}

Happy coding

Cheers

Al

return;

}

Happy coding

Cheers

Al

blog comments powered by Disqus