By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,508 Members | 1,915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,508 IT Pros & Developers. It's quick & easy.

Select Single Cell From Excel

P: n/a
PCK
I am trying to select a single cell from an Excel workbook. I am using the following code.

strCN = GetExcelConnection("C:\Test\Excel.xls")
strCMD = "select * from [Sheet1$A1:A1]"
oleDbCN = New OleDb.OleDbConnection
oleDbCN.ConnectionString = strCN
oleDbCN.Open()
oleDbCMD = New OleDb.OleDbCommand
oleDbCMD.Connection = oleDbCN
oleDbCMD.CommandType = CommandType.Text
oleDbCMD.CommandText = strCMD
ds = New DataSet
oleDbDA = New OleDb.OleDbDataAdapter(oleDbCMD)
oleDbDA.Fill(ds)
dt = ds.tables(0)

The table that is returned contains zero records. The name of the first column is the value from cell A1. Also if my SQL statement would have been "select * from [Sheet1$A1:A3]" then I would have got 2 rows returned with the value from A1 being the column name and the values from A2 and A3 being the two row values.

My question is how do I get the value of cell A1. Yes, I could get it from the column name, but surely that is not the best practice.
Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Wed, 10 Dec 2003 07:21:19 -0800, PCK <an*******@discussions.microsoft.com> wrote:

I am trying to select a single cell from an Excel workbook. I am using the following code.

strCN = GetExcelConnection("C:\Test\Excel.xls")
strCMD = "select * from [Sheet1$A1:A1]"
oleDbCN = New OleDb.OleDbConnection
oleDbCN.ConnectionString = strCN
oleDbCN.Open()
oleDbCMD = New OleDb.OleDbCommand
oleDbCMD.Connection = oleDbCN
oleDbCMD.CommandType = CommandType.Text
oleDbCMD.CommandText = strCMD
ds = New DataSet
oleDbDA = New OleDb.OleDbDataAdapter(oleDbCMD)
oleDbDA.Fill(ds)
dt = ds.tables(0)

The table that is returned contains zero records. The name of the first column is the value from cell A1. Also if my SQL statement would have been "select * from [Sheet1$A1:A3]" then I would have got 2 rows returned with the value from A1 being the column name and the values from A2 and A3 being the two row values.

My question is how do I get the value of cell A1. Yes, I could get it from the column name, but surely that is not the best practice.

Do you have HDR=No in your connection string? It's probably using the first row/cell value as the
column name.
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.