troubleshooting Question

Finding the Name of the First Sheet in an Excel File

Avatar of kiranboi
kiranboi asked on
Visual Basic.NET
2 Comments1 Solution236 ViewsLast Modified:
Hi all,

Below is the code I have been using to pull all info from a specified Excel Sheet. Its working fine as long as the sheet I want data from is called "Sheet1".
I now need to be able to programatically determine the name of the first sheet in the Excel file as this my dynamically change. Can someone let me know how to do this?

Many Thanks.

Dim sourceConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & importString & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
        Dim SheetName As String = "Sheet1"
        Dim columnsmade As Boolean = False
        If Not SheetName.EndsWith("$") Then
            SheetName &= "$"
        End If
        Dim sourceSQL As String = "SELECT * FROM [" & SheetName & "]"
        Dim sourceCon As New System.Data.OleDb.OleDbConnection(sourceConStr)
        Dim sourceCommand As New System.Data.OleDb.OleDbCommand(sourceSQL, sourceCon)
        Dim sourceReader As System.Data.OleDb.OleDbDataReader
        sourceCon.Open()
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros