Import Date from Excel with dd/MM/YYYY foramt

Posted by Nksingh420 under ASP.NET on 3/13/2014 | Points: 10 | Views : 1135 | Status : [Member] | Replies : 2
i have a excel sheet and i want to save data in data table and show in gridview with dd/mm/yyyy format




Responses

Posted by: A2H on: 3/13/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,
You can check the below link to get the details from Excel sheet and assign it to the GridView.
http://www.aspsnippets.com/Articles/Read-and-Import-Excel-File-into-DataSet-or-DataTable-using-C-and-VBNet-in-ASPNet.aspx
Please check the method "Import_To_Grid" method in above link. Above link have step by step explanation with source code on how to read data from excel and assign it to gridview.

Once you got the record from Excel sheet in to gridview then you can use the below code to format the DateValue according to your required format.

 <asp:GridView ID="GridView3" CellPadding="5" runat="server" ShowFooter="True" AutoGenerateColumns="false">

<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblLocalTime" runat="server" Text='<%# Convert.ToDateTime(Eval("YourDateValueColumn")).ToString("dd/MM/yyyy") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


Thanks,
A2H
My Blog

Nksingh420, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 3/13/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Sample code for Date Formatting is given below
HTML Mark Up
 <asp:GridView ID="GridView3" CellPadding="5" runat="server" ShowFooter="True" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblLocalTime" runat="server" Text='<%# Convert.ToDateTime(Eval("YourDateValueColumn")).ToString("dd/MM/yyyy") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView3.DataSource = this.Get_DateDetails();
GridView3.DataBind();
}
}

//Get your Date Value from Excel sheet here
public DataTable Get_DateDetails()
{
DataTable dt = new DataTable();
DataRow dr;
string Date = "2014/03/12,2014/03/13,2014/03/14,2014/03/15,2014/03/16";
string[] list2 = Date.Split(',');
dt.Columns.Add("YourDateValueColumn", typeof(string));
for (int i = 0; i < list2.Length; i++)
{
dr = dt.NewRow();
dr["YourDateValueColumn"] = list2[i];
dt.Rows.Add(dr);
}

return dt;
}



Thanks,
A2H
My Blog

Nksingh420, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response