Heres some code I use to connect to an excel data range :-
Using m_connexcel As OleDbConnection = New OleDbConnection(String.Format("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source={0};" & _
"Extended Properties=""Excel 8.0;HDR=NO""", SPREADSHEET_NAME))
m_connexcel.Open()
Using cmd As OleDbCommand = New OleDbCommand(String.Format("SELECT * FROM [{0}]", DATARANGE), m_connexcel)
Using oRDR As OleDbDataReader = cmd.ExecuteReader
While (oRDR.Read)
If Not oRDR.IsDBNull(oRDR.GetOrdinal("F1")) Then
MsgBox(oRDR.GetString(oRDR.GetOrdinal("F1")))
End If
End While
End Using
End Using
m_connexcel.Close()
End Using
Set 'HDR' in the connection string to 'YES' if you want to use the first row as the field names, otherwise the fields will be called F1, F2, F3 etc...
BlueJam
Member
441 Points
84 Posts
Re: How to Read Excel File AT VB.NET
Aug 06, 2008 09:00 AM|LINK
Heres some code I use to connect to an excel data range :-
Using m_connexcel As OleDbConnection = New OleDbConnection(String.Format("Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source={0};" & _ "Extended Properties=""Excel 8.0;HDR=NO""", SPREADSHEET_NAME)) m_connexcel.Open() Using cmd As OleDbCommand = New OleDbCommand(String.Format("SELECT * FROM [{0}]", DATARANGE), m_connexcel) Using oRDR As OleDbDataReader = cmd.ExecuteReader While (oRDR.Read) If Not oRDR.IsDBNull(oRDR.GetOrdinal("F1")) Then MsgBox(oRDR.GetString(oRDR.GetOrdinal("F1"))) End If End While End Using End Using m_connexcel.Close() End Using