Question : How to read a date column in excel using C#

I have a column in excel storing information as date like "1-Jan-2010". I want to read this date and store it in database.
I am done with reading the excel, facing problem specifically for date field. While reading the date it comes returns a number, which is not at all linked to the date.
An alternative is to treat it as string by appending " ' " (i.e. to treat it as string and then do string manipulations) to all rows before i actually read it, but i dont want to do this.

Please suggest.

Answer : How to read a date column in excel using C#

Your first code is fail as excel method to store data for date field is different that what it displayed to user.
Your second method is bound to fail due to two three reason, first maybe if the range you selected doesnt contain all fields with the date format.
Solution->open the excel file in excel, highlight the column of cells that should be of a "date" type, then goto format-->cells, and set it in excel to treat the cells as only a date type. save the file.

May be date format are there for available dates and not for empty or other cell that lie in the range your specified in the code. second you are converting the stored value (as excel stored it internally) directly into string. This value appear correctly if used in excel as excel can manupulate this value but after covnerting to string it is no longer a date type of excel. Finally if some field is emtpy then?
DateTime excelDate = (DateTime)my_range.get_Value(Type.Missing);
DateTime excelDateTime = Convert.ToDateTime(dataset.Tables[0].Rows[0]["DateTimeExcelFieldName"].ToString();

Verify the column is set as date type of some cells before we can conclude something else.

some code that may help you,

Range myRange = workSheet.get_Range("Y" + rowindex, "Y" + rowindex).;

object excelDate = myRange.get_value(Type.Missing);

DateTime goodDate = ConvertExcelDateToDate(excelDate);

DateTime ConvertExcelDateToDate(object excelDate)
       {
           DateTime date = (DateTime)excelDate;
                  return date;
       }

Finally check below link that may help you a bit,

http://msdn.microsoft.com/en-us/library/1ad4d8d6.aspx

Random Solutions  
 
programming4us programming4us