Click here to Skip to main content
15,885,546 members
Articles / ADO.NET
Tip/Trick

Listing Sheets in an Excel File

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
21 Feb 2018CPOL 5.9K   1   1
A simple way to use ADO.NET to get a list of sheets in an Excel file

Introduction

This tip describes one way to programmatically get the list of sheets in an Excel file. I do it this way because I like ADO.NET and this basic technique (with some minor changes) can be used with pretty much any database that has an ADO.NET provider.

Background

A few days ago, someone asked about getting a list of sheets in an Excel file, but seemed unwilling to actually write any code to do it.

Using the Code

I'm providing only a simple static method that can be called from other code that actually does something with the list.

The user of this method has to know what OleDb Providers that can access Excel (usually the ACE engine or the JET engine) are installed on the system. The user can then create an appropriate connection string to pass in (connectionstrings.com is a good resource for finding details).

The Method

The method is actually an enumerator, and I hope that makes it easy to use.

For more information, look into the System.Data.Common.DbConnection.GetSchema methods.

C#
public static System.Collections.Generic.IEnumerable<string>
EnumerateSheets
(
  string ConnectionString
)
{
  System.Data.DataTable dt = null ;

  using
  (
    System.Data.Common.DbConnection db
  =
    new System.Data.OleDb.OleDbConnection ( ConnectionString )
  )
  {
    db.Open() ;

    try
    {
      dt = db.GetSchema ( "TABLES" ) ;
    }
    finally
    {
      db.Close() ;
    }
  }

  if ( dt != null )
  {
    dt.DefaultView.Sort = "TABLE_NAME" ;
    dt.DefaultView.RowFilter = "TABLE_NAME LIKE '*$'" ;

    for ( int i = 0 ; i < dt.DefaultView.Count ; i++ )
    {
      yield return ( dt.DefaultView [ i ] [ "TABLE_NAME" ] as string ) ;
    }          
  }
          
  yield break ;
}

History

  • 2018-02-21 -- First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
BSCS 1992 Wentworth Institute of Technology

Originally from the Boston (MA) area. Lived in SoCal for a while. Now in the Phoenix (AZ) area.

OpenVMS enthusiast, ISO 8601 evangelist, photographer, opinionated SOB, acknowledged pedant and contrarian

---------------

"I would be looking for better tekkies, too. Yours are broken." -- Paul Pedant

"Using fewer technologies is better than using more." -- Rico Mariani

"Good code is its own best documentation. As you’re about to add a comment, ask yourself, ‘How can I improve the code so that this comment isn’t needed?’" -- Steve McConnell

"Every time you write a comment, you should grimace and feel the failure of your ability of expression." -- Unknown

"If you need help knowing what to think, let me know and I'll tell you." -- Jeffrey Snover [MSFT]

"Typing is no substitute for thinking." -- R.W. Hamming

"I find it appalling that you can become a programmer with less training than it takes to become a plumber." -- Bjarne Stroustrup

ZagNut’s Law: Arrogance is inversely proportional to ability.

"Well blow me sideways with a plastic marionette. I've just learned something new - and if I could award you a 100 for that post I would. Way to go you keyboard lovegod you." -- Pete O'Hanlon

"linq'ish" sounds like "inept" in German -- Andreas Gieriet

"Things would be different if I ran the zoo." -- Dr. Seuss

"Wrong is evil, and it must be defeated." –- Jeff Ello

"A good designer must rely on experience, on precise, logical thinking, and on pedantic exactness." -- Nigel Shaw

“It’s always easier to do it the hard way.” -- Blackhart

“If Unix wasn’t so bad that you can’t give it away, Bill Gates would never have succeeded in selling Windows.” -- Blackhart

"Use vertical and horizontal whitespace generously. Generally, all binary operators except '.' and '->' should be separated from their operands by blanks."

"Omit needless local variables." -- Strunk... had he taught programming

Comments and Discussions

 
SuggestionExplanation for RowFilter Pin
TnTinMn22-Feb-18 16:00
TnTinMn22-Feb-18 16:00 
You may want to explain the the reason for the DataView.Rowfilter is to screen out named ranges to produce the Worksheet list.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.