Try this I Hope it will solve your Issue
In .Aspx page
<asp:DataGrid ID="myGridView" Width="100%" runat="server" AutoGenerateColumns="False"
DataKeyNames="Id" AlternatingRowStyle-CssClass="alt">
<ItemStyle BackColor="#EBEBEB" />
<AlternatingItemStyle BackColor="White" />
<Columns>
<asp:TemplateColumn HeaderStyle-HorizontalAlign="Center" HeaderText="ID">
<ItemTemplate >
<asp:Label ID="lblId" runat="server" Text='<%# Eval("ID")%>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="15%" />
</asp:TemplateColumn>
<asp:TemplateColumn ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" Text='<%# Eval("Name")%>' runat="server"></asp:Label>
</ItemTemplate>mn
<ItemStyle HorizontalAlign="Center" Width="20%" />
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
In .cs page
myGridView.DataSource = dd.tables[0];
myGridView.DataBind();
ExportToExcel(myGridView);
public void ExportToExcel(System.Web.UI.Control ctl)
{
using (Control myCtl = ctl)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition", attachment);
HttpContext.Current.Response.Charset = charSet;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = content;
myCtl.Page.EnableViewState = false;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
myCtl.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
Keep smiling :):):) Happy coding:)