Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08-05-2008, 18:03:51
AngelGod AngelGod is offline
Un Nuevo Amigo
 
Registrado: nov 2006
Posts: 24
AngelGod Valoración +2
Talking Como Exportar Gridview a Excel Usando WF con C#?

Holas Camaradas de vb-mundo.


alguien de ustedes les ha pasado tambien que tienen que exportar un gridview a Excel usando WindowsForm usando C# como lenguaje,

me podrian dar alguna idea de como lograr eso?

aqui les pongo un ejemplo pero para Web

StringWriter miStringWrite = null;
HtmlTextWriter miHtmlWrite = null;

try

{

if (this.GridView1.Rows.Count > 0 && Session["Datos"] != null)
{

Response.Clear();

Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.ContentType = "application/vnd.xls";

Response.Charset = "UTF-8";
Response.ContentEncoding = Encoding.Default;

miStringWrite = new StringWriter();
miHtmlWrite = new HtmlTextWriter(miStringWrite);

this.GridView1.EnableViewState = false;
this.GridView1.AllowPaging = false;

this.GridView1.DataSource = (DataSet)Session["Datos"];
this.GridView1.DataBind();

this.GridView1.CssClass = "";this.GridView1.RenderControl(miHtmlWrite);
Response.Write(miStringWrite.ToString());

Response.End();

}

}

catch (Exception ex)
{

Response.Write(ex.Message);

}

finally

{

miStringWrite.Flush();

miHtmlWrite.Flush();

miStringWrite.Close();

miHtmlWrite.Close();

}

Muchas gracias.
Responder Con Cita