Solved

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

Posted on 2011-09-14
1
452 Views
Last Modified: 2013-12-17
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 ();
}
0
Comment
Question by:conrad2010
1 Comment
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 36539004
Please try the following:

using (var dbConnection = new OleDbConnection (@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\SAMPLE.xls;Extended Properties=""Excel 8.0;HDR=Yes;"""))
{
	dbConnection.Open ();

	foreach (DataRow row in dbConnection.GetOleDbSchemaTable (OleDbSchemaGuid.Tables, null).Rows)
	{
		Console.WriteLine(row["TABLE_NAME"]);
	}
}

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now