Stack Overflow en español Asked by CarlosR93 on December 21, 2020
Quisiera saber como exportar de Asp.net un DataGrid a Excel (si saben de alguna otra forma favor de explicarla), me sale un error, esta comentado en la linea donde aparece:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.ContentType = "application/ms-excel ";
StringWriter sw = new StringWriter()
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(hw);;// <-- Aqui me sale el error:RegisterForEventValidation can only be called during Render();
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
Si el control GridView no tiene controles AJAX como padre esto debería funcionar:
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=ArchivoExportado.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.HeaderRow.Style.Add("background-color", "#F0F0F0");
foreach (TableCell tableCell in GridView1.HeaderRow.Cells)
{
tableCell.Style["background-color"] = "#ffffff";
}
foreach (GridViewRow gridviewrow in GridView1.Rows)
{
gridviewrow.BackColor = System.Drawing.Color.White;
foreach (TableCell gridviewrowtablecell in gridviewrow.Cells)
{
gridviewrowtablecell.Style["background-color"] = "#ffffff";
}
}
GridView1.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
Si la grilla está dentro de controles AJAX:
Espero resulte útil.
Answered by Christian Amado on December 21, 2020
Este es un código para exportar un gridview muy similar que me funciona:
Código C#
protected void ExportToExcel()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = true;
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();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
Answered by user22970 on December 21, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP