I'm importing data from an Excel sheet on to a DataTable
using the following code:
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties=Excel 8.0");
con.Open();
_myDataSet = new DataSet();
OleDbDataAdapter myCommand = new OleDbDataAdapter(" SELECT * FROM [" + "Sheet1" + "$]", con);
myCommand.Fill(_myDataSet);
con.Close();
I have a Date
column in the Excel sheet in the format dd/MM/yyyy
. The above code is failing when the date is dd/MM/yyyy
(eg. 27/12/2009
). How to specify the date format?
EDIT (adding more details):
It is not throwing any exception. Data is imported to the DataSet
until the row where an invalid Date
format is encountered. I have the date as dd/MM/yyyy
in Excel sheet. When I import using OleDbDataAdapter
, it is expecting the date in the Excel sheet to be in MM/dd/yyyy
. No naturally when it encounters a date such as 27/2/2009
it stops the process of importing, though no error/exception is thrown. So I'm having only partial results in DataTable
.
Please help.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…