Export to Excel in ASP.NET

0

I would like to know how to export from Asp.net a DataGrid to Excel (if you know of any other way please explain it), I get an error, it is commented on the line where it appears:

    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)
    {

    }
    
asked by CarlosR93 26.12.2016 в 21:18
source

2 answers

0
___ erkimt ___ Export to Excel in ASP.NET ______ qstntxt ___

I would like to know how to export from Asp.net a DataGrid to Excel (if you know of any other way please explain it), I get an error, it is commented on the line where it appears:

 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 */
        }
    
______ ___ azszpr41074

This is a code to export a very similar gridview that works for me:

This is the result:

This is how it is implemented

Code 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 */
        }
    
______ azszpr170388 ___

If the GridView control does not have AJAX controls as a parent this should work:

%pre%

If the grid is inside AJAX controls:

  • Place the button or link that performs the export outside the UpdatePanel control.
  • Add UpdatePanel the PostBackTrigger trigger that points to the button that performs the export.
  • Call the ScriptManager.RegisterPostBackControl () method by passing the export button as a parameter.
  • Lastly, you can make another page .aspx to export without using UpdatePanel .

I hope it's useful.

    
___
answered by 26.12.2016 в 22:57
0

If the GridView control does not have AJAX controls as a parent this should work:

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();

If the grid is inside AJAX controls:

  • Place the button or link that performs the export outside the UpdatePanel control.
  • Add UpdatePanel the PostBackTrigger trigger that points to the button that performs the export.
  • Call the ScriptManager.RegisterPostBackControl () method by passing the export button as a parameter.
  • Lastly, you can make another page .aspx to export without using UpdatePanel .

I hope it's useful.

    
answered by 04.06.2018 в 15:36