You will need to create a new DataTable and save the date as string. Since if you export it as DateTime object, Excel will change is format as DateTime does not have any Format.
HTML
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
runat="server" AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging">
<Columns>
<asp:BoundField DataField="ContactName" HeaderText="Contact Name" ItemStyle-Width="150px" />
<asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="100px" />
<asp:BoundField DataField="BirthDate" HeaderText="BirthDate" ItemStyle-Width="100px"
DataFormatString="{0:dd/MM/yyyy}" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnExport" runat="server" Text="Export To Excel" OnClick="ExportToExcel" />
Namespaces
using System.IO;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("ContactName", typeof(string)),
new DataColumn("City", typeof(string)),
new DataColumn("BirthDate",typeof(DateTime)) });
dt.Rows.Add("Maria", "Boise", new DateTime(1990, 12, 14));
dt.Rows.Add("Ana Trujillo", "Warszawa", new DateTime(1985, 9, 22));
dt.Rows.Add("Antonio Moreno", "Mannheim", new DateTime(1991, 1, 17));
dt.Rows.Add("Thomas Hardy", "Madrid", new DateTime(1987, 6, 5));
GridView1.DataSource = dt;
GridView1.DataBind();
ViewState["Data"] = dt;
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.BindGrid();
}
protected void ExportToExcel(object sender, EventArgs e)
{
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);
//To Export all pages
GridView1.AllowPaging = false;
DataTable dt = (DataTable)ViewState["Data"];
DataTable dt2 = new DataTable();
for (int i = 0; i < dt.Columns.Count; i++)
{
dt2.Columns.Add(dt.Columns[i].ColumnName, typeof(string));
}
for (int i = 0; i < dt.Rows.Count; i++)
{
dt2.ImportRow(dt.Rows[i]);
dt2.Rows[i]["BirthDate"] = Convert.ToDateTime(dt2.Rows[i]["BirthDate"]).ToString("dd/MM/yyyy");
}
this.GridView1.DataSource = dt2;
this.GridView1.DataBind();
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
Screenshot
