Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am working with excel datareader, the excel format is .xlsx, How can I convert OA time to .net datetime while importing data into dataset?Any help would be appreciated.
C#
FileStream stream = File.Open(strTempPath, FileMode.Open, FileAccess.Read);

if (Path.GetExtension(strTempPath).Equals(".xls"))
    // Reading from a binary Excel file ('97-2003 format; *.xls)
    excelReader = ExcelReaderFactory.CreateBinaryReader(stream); 
else
    //Reading from a OpenXml Excel file (2007 format; *.xlsx)
    excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); 

excelReader.IsFirstRowAsColumnNames = true;

DataTable result = new DataTable();

result = excelReader.AsDataSet(true).Tables[0];
Posted
Updated 20-Sep-12 2:48am
v3
Comments
[no name] 20-Sep-12 7:53am    
Reading the documentation for the DateTime data type might be a good place to start.
[no name] 20-Sep-12 7:55am    
Thank you,,,,BUt,Its a dynamic data, I can't just find which column is of type date time......
Richard MacCutchan 20-Sep-12 8:19am    
If you do not know which column it is, how can anyone else guess?
[no name] 20-Sep-12 8:29am    
the excel sheet has data which is uploaded by client.. how could i guess too.
Vani Kulkarni 20-Sep-12 9:45am    
Why dont you inform your client, to send data in a specified format? Provide your client with template of the excel sheet which states the data format expected to process the data.

1 solution

Use try catch block around :
C#
DateTime.FromOADate(double d)


C#
try
{  
 double d = double.Parse(b);

 // Get the converted date from the OLE automation date.
 DateTime conv = DateTime.FromOADate(d);
}
catch{}

http://msdn.microsoft.com/en-us/library/system.datetime.fromoadate(v=vs.90).aspx[^]
 
Share this answer
 
Comments
[no name] 21-Sep-12 0:26am    
It's time eating process to trace date time column and go on converting into .net date time format. What to do with some lakhs of records...?
Kuthuparakkal 21-Sep-12 5:16am    
Change your data acquisition strategy. Create some quality standards for input files, drive standardization... What else!?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900