Como exportar un dataGrid a Excel
Publicado por Daniela (10 intervenciones) el 20/02/2018 14:50:43
Este es el codigo que estoy usando para hacer la exportancion, lo que pasa es que cuando lo descargo y lo abro en excel este me que la extensión tiene otro formato y si le doy aceptar al el excel, la hoja no muestra nada (hoja en blanco)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
public class CustomersBe
{
public string Almacen { get; set; }
public string reftallacolor { get; set; }
public string referencia { get; set; }
public string talla { get; set; }
public string color { get; set; }
public int idcurva { get; set; }
public string CantUnidades { get; set; }
public string CantModificadas { get; set; }
public string Zona { get; set; }
public string coleccion { get; set; }
public string clasifica { get; set; }
public string silueta { get; set; }
public int Cantinv { get; set; }
public double UnidadesFactu { get; set; }
public string rotacionSemana { get; set; }
public string rotacionMes { get; set; }
public string rotacion2Meses { get; set; }
public string Sugerido { get; set; }
}
public static List<CustomersBe> lista = new List<CustomersBe>();
//private object runat;
public object server { get; private set; }
public void CargarData()
{
SqlConnection conexion = new SqlConnection();
conexion.ConnectionString = "Data Source=192.168.1.200;Initial Catalog=LAURASA;User Id=sa;Password=laurasa";
conexion.Open();
try
{
string consulta = @"select almacen,RefTallaColor,Referencia,Talla,color,IDCurva,CantUnidades,CantModificadas from Analisis_Curva WHERE Referencia = '" + ddlReferencia.SelectedValue + "' and almacen = '" + ddlAlmacen.SelectedValue + "'";
var cmd = new SqlCommand(consulta, conexion);
SqlDataReader lector = cmd.ExecuteReader();
while (lector.Read())
{
var customers = new CustomersBe();
customers.Almacen = (string)lector[0];
customers.reftallacolor = (string)lector[1];
customers.referencia = (string)lector[2];
customers.talla = (string)lector[3];
customers.color = (string)lector[4];
customers.idcurva = (int)lector[5];
customers.CantUnidades = (string)lector[6];
customers.CantModificadas = (string)lector[7];
lista.Add(customers);
}
grvAnalisis.DataSource = lista;
grvAnalisis.DataBind();
conexion.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
protected void grvAnalisis_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grvAnalisis.PageIndex = e.NewPageIndex;
CargarData();
}
protected void btnGridviewToExcel_Click(object sender, EventArgs e)
{
grvAnalisis.EnableViewState = false;
Response.Clear();
Response.Buffer = true;
Response.ContentEncoding = System.Text.ASCIIEncoding.UTF8;
Response.AddHeader("content-disposition", "attachment;filename=AnalisisDeCurva.xls");
// Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(sw);
Page page = new Page();
Form HtmlForm = new Form();
Page.Controls.Add(Form);
Form.Controls.Add(grvAnalisis);
//Page.RenderControl(hw);
grvAnalisis.AllowPaging = true;
this.CargarData();
//grvAnalisis.HeaderRow.BackColor = Color.White;
foreach(TableCell cell in grvAnalisis.HeaderRow.Cells)
{
cell.BackColor=grvAnalisis.HeaderStyle.BackColor;
}
foreach (GridViewRow row in grvAnalisis.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)btn
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = grvAnalisis.AlternatingRowStyle.BackColor;
}
else
{
//cell.BackColor = grvAnalisis.RowStyle.BackColor;
cell.BackColor = Color.White;
}
cell.CssClass = "textmode";
}
}
// grvAnalisis.RenderControl(hw);
// string style = @"<style> .textmode { } </style>";
Response.Charset = "iso-8859-1";
Response.Charset = "UTF-8";
//Response.ContentEncoding = Encoding.Default;
Response.Write(sw);
Response.Output.Write(sw.ToString());
//Response.Flush();
Response.End();
}
}
Valora esta pregunta


0