Exemplo n.º 1
0
        protected void ExportToExcel(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=ReporteValoraciones.xls");
            Response.Charset     = "";
            Response.ContentType = "application/vnd.ms-excel";
            using (StringWriter sw = new StringWriter())
            {
                HtmlTextWriter hw = new HtmlTextWriter(sw);

                //To Export all pages
                GvValoraciones.AllowPaging = false;
                GvValoraciones.DataBind();

                GvValoraciones.HeaderRow.BackColor = Color.White;
                foreach (TableCell cell in GvValoraciones.HeaderRow.Cells)
                {
                    cell.BackColor = GvValoraciones.HeaderStyle.BackColor;
                }
                foreach (GridViewRow row in GvValoraciones.Rows)
                {
                    row.BackColor = Color.White;
                    foreach (TableCell cell in row.Cells)
                    {
                        if (row.RowIndex % 2 == 0)
                        {
                            cell.BackColor = GvValoraciones.AlternatingRowStyle.BackColor;
                        }
                        else
                        {
                            cell.BackColor = GvValoraciones.RowStyle.BackColor;
                        }
                        cell.CssClass = "textmode";
                    }
                }

                GvValoraciones.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();
            }
        }