I\'m importing data from an Excel sheet on to a DataTable
using the following code:
OleDbConnection con = new OleDbConnection(\"Provider=Microso
You could use the British Culture info to parse the date in British Format dd/MM/YYYY
CultureInfo culture = new CultureInfo("en-GB");
DateTime date = DateTime.Parse("27/12/2009", culture);
where "27/12/2009" is your date field
I also faced the same Issue. The Date column in the Excel sheet is dd/MM/yyyy. But as per my system settings the data format is MM/dd/yyyy. Therefore if the date is 31/07/2013 for example , the Date field displayed as empty string.
Using IMEX1 along with "Microsoft.ACE.OLEDB.12.0" Driver Solved the Problem.
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'"
But "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FilePath + "; Extended Properties=Excel 8.0;IMEX=1" doesn't work.
When linking Excel spreadsheets in MS Access, a problem arises when a column has mixed data types. For example, if the first row in the column is "Text" and the remaining are Numeric, the Numeric fields will be formatted as Text and #Error out. Likewise with Dates and non-Dates mixed in the same column.
There are some nice attempts at answers up there, but here is the simple solution to read these Mixed data types without a data type mismatch error:
Try adding "IMEX=1" to your MS Access SQL, such as:
SELECT Type, Width, Weight
FROM [Excel 8.0;IMEX=1;DATABASE=C:\TEMP\MySpreadsheet.xls].MyExcelTable
Thanks, Brian Jasmer
How many rows do you have, and are all the cells in the date column valid dates? Sometimes the OleDB routines will incorrectly identify a column as text if there is some inconsistency in the cells in the first 8 rows. (8 is the default number of rows that is read to determine data types)
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
How about filling the dataset programmatically instead of via a data adapter?
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties=Excel 8.0");
Dim _myDataSet As New DataSet
Dim con As New OleDb.OleDbConnection
con.Open()
Dim cmd As New OleDb.OleDbCommand(" SELECT * FROM [" + "Sheet1" + "$]", con)
Using dr = cmd.ExecuteReader()
While dr.Read
Dim row = _myDataSet.Tables(0).NewRow()
With dr.Item("excel date column").ToString
Dim dt As New Date(CInt(.Substring(6)), CInt(.Substring(3, 2)), CInt(.Substring(0, 2)))
row.Item("dataset datecolumn") = dt
End With
\*'populate other columns here' *\
_myDataSet.Tables(0).Rows.Add(row)
End While
End Using