DateTime format mismatch on importing from Excel Sheet

后端 未结 10 2160
旧巷少年郎
旧巷少年郎 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:03

    You can use this function to format whatever date format you received to the format you need.

    Here is the code:

    Public Shared Function ConvertToDate(ByVal dateString As String, ByRef result As DateTime) As Boolean
    Try
    
    'Here is the date format you desire to use
    Dim supportedFormats() As String = New String() {”dd/MM/yyyy”}
    
    'Now it will be converted to what the machine supports
    result = DateTime.ParseExact(dateString, supportedFormats,System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None)
    
    Return True
    Catch ex As Exception
    Return False
    End Try
    End Function
    

提交回复
热议问题