Excel

G

Guest

Hi
I am using the below code to read an excel sheet.
It works fine, but the problem i am facing is the name of the excel sheet,
which i have hardcoded in my query as [sheet1$].

I am getting errors when the sheet name is not [sheet1$] !!!

How can i make the query Generic even if the sheet name is different????



Dim sbConn As StringBuilder = New StringBuilder
sbConn.Append("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
filePath)
sbConn.Append(";Extended Properties=")
sbConn.Append(Convert.ToChar(34))
sbConn.Append("Excel 8.0;HDR=Yes;IMEX=2")
sbConn.Append(Convert.ToChar(34))

Dim cnExcel As OleDbConnection = New
OleDbConnection(sbConn.ToString())
cnExcel.Open()
Dim cmdExcel As OleDbCommand = New OleDbCommand("select * from
[sheet1$]", cnExcel)
Dim drExcel As OleDbDataReader
Try
drExcel =
cmdExcel.ExecuteReader(CommandBehavior.CloseConnection)
Catch
lblError.Text = "Please Check the Excel Data before UPLOADING"
End Try


Thanks
vinay
 
G

Guest

Can you use an Excel object and get the sheet names and ordinal position
before opening the connection?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top