Link to home
Start Free TrialLog in
Avatar of conrad2010
conrad2010

asked on

C# ADO.NET get all tab names in Excel spreadsheet

I am trying to get all the tab names that have data from an Excel spreadsheet.

Somewhere I found this code and want to adapt it to loop through all tabs (worksheets) and store it in a drop down combobox:

OleDbConnection dbConnection = new OleDbConnection (@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\SAMPLE.XLS;Extended Properties=""Excel 8.0;HDR=Yes;""");
dbConnection.Open ();
try
{
    // Get the name of the first worksheet:
    DataTable dbSchema = dbConnection.GetOleDbSchemaTable (OleDbSchemaGuid.Tables, null);
    if (dbSchema == null || dbSchema.Rows.Count < 1)
    {
        throw new Exception ("Error: Could not determine the name of the first worksheet.");
    }
    string SheetNameOne = dbSchema.Rows [0] ["TABLE_NAME"].ToString ();
}
finally
{
    dbConnection.Close ();
}
ASKER CERTIFIED SOLUTION
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial