I want to export a datagrid, its button works but when I open it in excel, it tells me that the extension has another format.
otherwise, if I accept the excel, the sheet shows nothing
this is the code
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();
}
}
the problem is in the btnGridviewToExcel