I am using EPPlus to read excel sheets and load the data into datatable to perform further operations and then save back the modified data to the excel file.
The below code checks if the cell value is a float value then it converts the float value to datetime.
The code works fine when the cell value is a date eg: Invoice Date = 42009 , but it converts the not a date value like eg : amount = 10 to a date.
Is there any way in EPPlus library from which i can determine the format of the cell (i.e General/date/number etc) ?
float floatValue;
if (float.TryParse(Convert.ToString(oSheet.Cells[i, j].Value), out floatValue)
&& Convert.ToString(oSheet.Cells[i, j].Style.Numberformat.Format).Contains("[$-409]d\\-mmm\\-yy;@"))
{
dr[j - 1] = String.Format("{0:d-MMM-yy}", DateTime.FromOADate(floatValue));
}
else
{
DateTime date;
if (DateTime.TryParse(Convert.ToString(oSheet.Cells[i, j].Value), out date))
{
dr[j - 1] = String.Format("{0:d-MMM-yy}", date);
}
else
{
dr[j - 1] = Convert.ToString(oSheet.Cells[i, j].Value).Trim();
}
}