Hi comunidadmexi...,
Refer below code.
HTML
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Export" runat="server" OnClick="OnExport" />
Namespaces
C#
using System.Data;
using System.IO;
VB.Net
Imports System.Data
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "Frédérique Citeaux", "Finland");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Martine Rancé", "France");
dt.Rows.Add(4, "Lúcia Carvalho", "Brazil");
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
protected void OnExport(object sender, EventArgs e)
{
try
{
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);
this.GridView1.AllowPaging = false;
this.BindGrid();
this.GridView1.HeaderRow.BackColor = System.Drawing.Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = System.Drawing.Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = System.Drawing.Color.Green;
cell.ForeColor = System.Drawing.Color.White;
}
else
{
cell.BackColor = System.Drawing.Color.Blue;
cell.ForeColor = System.Drawing.Color.White;
}
cell.CssClass = "textmode";
}
}
this.GridView1.RenderControl(hw);
string style = "<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
catch (Exception ex)
{
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() _
{
New DataColumn("Id"),
New DataColumn("Name"),
New DataColumn("Country")
})
dt.Rows.Add(1, "Frédérique Citeaux", "Finland")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Martine Rancé", "France")
dt.Rows.Add(4, "Lúcia Carvalho", "Brazil")
Me.GridView1.DataSource = dt
Me.GridView1.DataBind()
End Sub
Protected Sub OnExport(sender As Object, e As EventArgs)
Try
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
Using sw As New StringWriter()
Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
Me.GridView1.AllowPaging = False
Me.BindGrid()
Me.GridView1.HeaderRow.BackColor = Drawing.Color.White
For Each cell As TableCell In GridView1.HeaderRow.Cells
cell.BackColor = GridView1.HeaderStyle.BackColor
Next
For Each row As GridViewRow In GridView1.Rows
row.BackColor = Drawing.Color.White
For Each cell As TableCell In row.Cells
If row.RowIndex Mod 2 = 0 Then
cell.BackColor = Drawing.Color.Green
cell.ForeColor = Drawing.Color.White
Else
cell.BackColor = Drawing.Color.Blue
cell.ForeColor = Drawing.Color.White
End If
cell.CssClass = "textmode"
Next
Next
Me.GridView1.RenderControl(hw)
Dim style As String = "<style> .textmode { } </style>"
Response.Write(style)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.End()
End Using
Catch ex As Exception
End Try
End Sub
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
Screenshot