29/02/2012, 11:01
|
| | Fecha de Ingreso: febrero-2012
Mensajes: 2
Antigüedad: 12 años, 10 meses Puntos: 0 | |
Respuesta: Exportar a excel C# (sin librerias office) Hola,
Quizas puedas probar esto...
protected void imgBtnExportar_Click(object sender, ImageClickEventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=DATA.xls");
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Write(HTML()); //Llamada al procedimiento HTML
Response.End();
}
public string HTML()
{
Page page1 = new Page();
HtmlForm form1 = new HtmlForm();
GridView1.EnableViewState = false;
if (GridView1.DataSource != null)
{
GridView1.DataBind();
}
GridView1.EnableViewState = false;
page1.EnableViewState = false;
page1.Controls.Add(form1);
form1.Controls.Add(GridView1);
System.Text.StringBuilder builder1 = new System.Text.StringBuilder();
System.IO.StringWriter writer1 = new System.IO.StringWriter(builder1);
HtmlTextWriter writer2 = new HtmlTextWriter(writer1);
writer2.Write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title>Datos</title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n<style>\n</style>\n</head>\n<body>\n");
writer2.Write("</br></br></br></br></br></br></br></br></br></br></br></br><img src=http://10.252.196.115:8080/TME/Galeria/Atento/logo.gif width=13% height=10%>");
writer2.Write("<table><tr><td></td><td></td><td><font face=Arial size=5><center>REPORTE</center></font></td></tr></table><br>");
// writer2.Write("<table>\n<tr>\n<td></td><td class=TD width=35%><b>Fecha :</b></td><td width=65% align=left>" + System.DateTime.Now.ToString() + "</td>\n</tr>\n<tr>\n<td></td><td class=TD><b>Reporte:</b></td><td>" + ddlReporte.SelectedItem.ToString().Trim() + "</td>\n</tr>\n<tr>\n<td></td><td class=TD><b>Modulo:</b></td><td>" + Session["SCampana"].ToString().Trim() + "</td>\n</tr>\n<tr>n<td class=TD><b>Matricula:</b></td><td>" + ddlMatricula.SelectedItem.Trim() + "</td>\n</tr></table>\n<br><br>");
page1.DesignerInitialize();
page1.RenderControl(writer2);
writer2.Write("\n</body>\n</html>");
page1.Dispose();
page1 = null;
return builder1.ToString();
} |