public static void List2Excel3 <T>(HttpResponse responsePage, IList <T> lista, String Titulo, String nombre, List <ReportColumnHeader> columnsNames, DataTable dt = null) { var filename = nombre + ".xls"; DataTable DataTablelista; if (dt != null) { DataTablelista = dt; } else { DataTablelista = CollectionHelper.ConvertTo(lista); } List <DataColumn> lstIndexremoves = new List <DataColumn>(); for (int i = 0; i < DataTablelista.Columns.Count; i++) { DataColumn col = DataTablelista.Columns[i]; ReportColumnHeader mcolumn = columnsNames.Find(x => x.BindField == col.ColumnName); if (mcolumn == null) { lstIndexremoves.Add(col); } else { DataTablelista.Columns[i].ColumnName = mcolumn.HeaderName; } } for (int i = 0; i < lstIndexremoves.Count; i++) { DataTablelista.Columns.Remove(lstIndexremoves[i]); } var dgGrid = new DataGrid(); //dgGrid.GridLines = GridLines.Both; dgGrid.DataSource = DataTablelista; //dgGrid.HeaderStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#333333"); //dgGrid.HeaderStyle.ForeColor = System.Drawing.Color.White; dgGrid.DataBind(); foreach (DataGridItem gvrow in dgGrid.Items) { for (int k = 0; k < gvrow.Cells.Count; k++) { // gvrow.Cells[k].Style.Add("Height", "10"); //gvrow.Cells[k].Attributes.Add("class", "cost"); //string nombreColumna = dgGrid.Columns[k].HeaderText; //if (nombreColumna == "CodSector") //{ // var s = 2; //} gvrow.Cells[k].Style.Value = "mso-number-format:\\@;"; } } dgGrid.EnableViewState = false; var sb = new StringBuilder(); var SW = new StringWriter(sb); var htw = new HtmlTextWriter(SW); var Page = new Page(); var form = new HtmlForm(); Page.EnableEventValidation = false; Page.DesignerInitialize(); Page.Controls.Add(form); form.Controls.Add(dgGrid); Page.RenderControl(htw); responsePage.Clear(); responsePage.Buffer = true; responsePage.ContentType = "application/vnd.ms-excel"; responsePage.AddHeader("Content-Disposition", "attachment;filename=" + filename); responsePage.Charset = "UTF-8";// string.Empty; responsePage.Cache.SetCacheability(HttpCacheability.NoCache); responsePage.ContentEncoding = System.Text.Encoding.Default; int inicioTabla = 0; int finTabla = 0; string contenido = sb.ToString(); inicioTabla = contenido.IndexOf("<table"); finTabla = contenido.IndexOf("</table>"); contenido = contenido.Substring(inicioTabla, (finTabla + 8) - inicioTabla); responsePage.Write(contenido); // responsePage.Write(sb.ToString()); responsePage.End(); }
public static void List2Excel <T>(HttpResponseBase responsePage, IList <T> lista, String Titulo, String nombre, List <ReportColumnHeader> columnsNames, DataTable dt = null) { var filename = nombre + ".xls"; DataTable DataTablelista; if (dt != null) { DataTablelista = dt; } else { DataTablelista = CollectionHelper.ConvertTo(lista); } List <DataColumn> lstIndexremoves = new List <DataColumn>(); for (int i = 0; i < DataTablelista.Columns.Count; i++) { DataColumn col = DataTablelista.Columns[i]; ReportColumnHeader mcolumn = columnsNames.Find(x => x.BindField == col.ColumnName); if (mcolumn == null) { lstIndexremoves.Add(col); } else { DataTablelista.Columns[i].ColumnName = mcolumn.HeaderName; } } for (int i = 0; i < lstIndexremoves.Count; i++) { DataTablelista.Columns.Remove(lstIndexremoves[i]); } var dgGrid = new DataGrid(); dgGrid.GridLines = GridLines.Both; dgGrid.DataSource = DataTablelista; dgGrid.HeaderStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#333333"); dgGrid.HeaderStyle.ForeColor = System.Drawing.Color.White; dgGrid.DataBind(); var sb = new StringBuilder(); var Page = new Page(); var SW = new StringWriter(sb); var htw = new HtmlTextWriter(SW); var inicio = "<div> " + "<table border=0 cellpadding=0 cellspacing=0>" + "<tr>" + "<td colspan=10 align=center><h1>Consulta de " + Titulo + "</h1></td>" + "</tr>" + "<tr>" + "<td colspan=1 align=left style='font-weight:bold'>Fecha de generación:</td>" + "<td colspan=2 align=left>" + DateTime.Now.ToLongDateString() + "</td>" + "</tr>" + "</table>" + "</div>"; htw.Write(inicio); var form = new HtmlForm(); responsePage.ContentType = "application/vnd.ms-excel"; responsePage.AddHeader("Content-Disposition", "attachment;filename=" + filename); responsePage.Charset = string.Empty; responsePage.ContentEncoding = Encoding.Default; dgGrid.EnableViewState = false; Page.EnableEventValidation = false; Page.DesignerInitialize(); Page.Controls.Add(form); form.Controls.Add(dgGrid); Page.RenderControl(htw); responsePage.Clear(); responsePage.Buffer = true; responsePage.Write(sb.ToString()); responsePage.End(); }