DateTime format mismatch on importing from Excel Sheet

后端 未结 10 2168
旧巷少年郎
旧巷少年郎 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: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);
    

提交回复
热议问题