DateTime format mismatch on importing from Excel Sheet

后端 未结 10 2113
旧巷少年郎
旧巷少年郎 2020-12-17 21:36

I\'m importing data from an Excel sheet on to a DataTable using the following code:

OleDbConnection con = new OleDbConnection(\"Provider=Microso         


        
相关标签:
10条回答
  • 2020-12-17 22:08

    Pulling in Date or other mixed-data column items in the past has bit me with Excel.

    It seems that their provider "peeks" ahead XX rows (depending on Provider version) to determine what the column type is at runtime. The prior answers that apply properties to your connection have helped me in the past, but do not help when you have BLANK dates and/or different values in the column itself - it confuses the Excel driver.

    What I recommend is that you use FileHelpers or another third-party library instead. Infragistics Excel component has treated me very well, while FileHelpers is opensource.

    0 讨论(0)
  • 2020-12-17 22:11

    maybe try formatting the date column in excel then import?

    0 讨论(0)
  • 2020-12-17 22:11

    You may want to check that the current culture you are running supports UK style dates.Here is a link on how to check and change this at the thread level http://support.microsoft.com/kb/306162

    The OleDb connection string seems to support ";Locale Identifier=". I think the eb-GB identifier is 2057 (Don't quote me ;)), you could also give that a try.

    As for the exceptions not being thrown i think during the fill you may want to look into the RowUpdated events as per this link http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdataadapter.rowupdated(VS.80).aspx.

    This is how the Fill method of the DataSet works it will quit on the row where there is a problem, using the above events you may have some options on ignoring the row or reformatting.

    0 讨论(0)
  • 2020-12-17 22:12

    One thing you should always specify in your connection string is IMEX=1:

    Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties=\"Excel 8.0;IMEX=1\"
    

    It helps with parsing columns that contain both numbers and strings. Might help with date parsing as well, but then you would have to manually convert all dates with:

    System.IFormatProvider format = new System.Globalization.CultureInfo("en-US", true);
    DateTime d = DateTime.Parse(dataSet.Tables[0].Rows[i]["MyDate"] as string,format);
    
    0 讨论(0)
提交回复
热议问题