value = string.Empty;
if (cell.StyleIndex != null)
{
CellFormat cf = document.WorkbookPart.WorkbookStylesPart.Stylesheet.CellFormats.ChildElements[int.Parse(cell.StyleIndex.InnerText)] as CellFormat;
if (cf.NumberFormatId == 14)
{
//value = DateTime.Parse("1900-01-01").AddDays(int.Parse(cell.CellValue.Text) - 2).ToString("yyyy-MM-dd");
value = Convert.ToString(DateTime.FromOADate(Double.Parse(cell.CellValue.Text)).ToShortDateString());
}
if (cf.NumberFormatId == 176 || cf.NumberFormatId == 177)
{
//value = DateTime.Parse("1900-01-01").AddDays(int.Parse(cell.CellValue.Text) - 2).ToString("yyyy-MM-dd");
value = Convert.ToString(DateTime.FromOADate(Double.Parse(cell.CellValue.Text)));
}
}
I am using the above code to check if there is any date column in excel and then convert the value of the cell to equivalent date.By default the NumberFormatID
is 14,but there are more date formats in excel.For a particular date format sometimes the NumberFormatID
is 165 and sometimes it is showing 176.
These are not fixed.So when i am selecting the date time format sometimes it is 176 and sometimes it is different so my code is not working.How do i differentiate a particular date and datetime format?