private void BindGrid() { List <ChamadosModel> listChamados = new List <ChamadosModel>(); listChamados = cb.ConsultaTodosChamado(cm); GridRel.DataSource = listChamados; GridRel.DataBind(); }
public void ExportGridToExcel() { Response.Clear(); Response.Buffer = true; Response.AppendHeader("content-disposition", "attachment; filename=ListaChamados.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; using (var sw = new StringWriter()) { var hw = new HtmlTextWriter(sw); GridRel.AllowPaging = false; BindGrid(); foreach (GridViewRow row in GridRel.Rows) { foreach (Control rowControl in row.Controls) { PrepareControlForExport(rowControl); } } GridRel.RenderControl(hw); //Response.Write(@"<style> .textmode { } </style>"); var headerHtml = sw.ToString() .Substring(sw.ToString().Substring(0, sw.ToString().IndexOf("<th", StringComparison.Ordinal)).Length, sw.ToString().LastIndexOf("</th>", StringComparison.Ordinal) + 9 - sw.ToString().Substring(0, sw.ToString().IndexOf("<th", StringComparison.Ordinal)).Length); var header = new StringBuilder(); header.Append("<th style='background-color: green; color:white;'> Id Chamado </th>"); header.Append("<th style='background-color: green; color:white;'> Problema </th>"); header.Append("<th style='background-color: green; color:white;'> Area </th>"); header.Append("<th style='background-color: green; color:white;'> Empresa </th>"); header.Append("<th style='background-color: green; color:white;'> Descricao </th>"); header.Append("<th style='background-color: green; color:white;'> Status </th>"); header.Append("<th style='background-color: green; color:white;'> Data Abertura </th>"); header.Append("<th style='background-color: green; color:white;'> Data Fechamento </th>"); header.Append("<th style='background-color: green; color:white;'> Telefone </th>"); header.Append("<th style='background-color: green; color:white;'> Operador </th>"); header.Append("<th style='background-color: green; color:white;'> Mensagem Final </th>"); var htmlFinal = sw.ToString().Replace(headerHtml, header.ToString()); htmlFinal = htmlFinal.Substring(5); htmlFinal = htmlFinal.Substring(0, htmlFinal.Length - 6); Response.Output.Write(htmlFinal); Response.Flush(); Response.End(); } }