Click here to Skip to main content
15,907,392 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I need to know what this code is doing in c#

C#
private List<string> GetExcelSheetName(OleDbConnection connection)
{
   List<string> listSheetNames = new List<string>();
   try
   {
      DataTable dtSheet = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

      foreach (DataRow drSheet in dtSheet.Rows)
      {
         if (drSheet["TABLE_NAME"].ToString().Contains("$"))//checks whether row contains '_xlnm#_FilterDatabase' or sheet name(i.e. sheet name always ends with $ sign)
         {
            listSheetNames.Add(drSheet["TABLE_NAME"].ToString());
         }
      }
   }
   catch (Exception ex)
   {
      throw new Exception("Could not read Excel sheet names\nDetails: " + ex.Message);
   }
   return listSheetNames;
}


Thanks
John
Posted
Updated 27-Nov-13 23:08pm
v2
Comments
BulletVictim 28-Nov-13 5:15am    
What exactly do you not understand in this code?

1 solution

The code gets a list of all the sheet names in an excel document by connecting via an OleDBConnection.
 
Share this answer
 
Comments
CPallini 28-Nov-13 5:08am    
5.
Member 10408451 28-Nov-13 5:20am    
Yes, you are correct. But I am trying to use debugging to understand the code at the line
connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

It is showing different parameters in OleDbSchemaGuid.Tables

what does these represents???


Thanks
John
Pheonyx 28-Nov-13 6:05am    
Okay, so what do you mean by "It is showing different parameters in OleDbSchemaGuid.Tables"

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900