Ver Mensaje Individual
  #3 (permalink)  
Antiguo 20/06/2016, 08:00
ErnestoTorres04
 
Fecha de Ingreso: junio-2016
Mensajes: 4
Antigüedad: 7 años, 10 meses
Puntos: 0
Respuesta: Como exportar un grid view a excel asp.net 2.0 (Solución)

Hola que tal, yo uso este codigo
private void exportar()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename="+txtNumer.Text+".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);

To Export all pages
GridView1.AllowPaging = false;
this.llenaGrid();

GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}

GridView1.RenderControl(hw);

style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}