I am working with some code that uses an OleDbConnection to load data from an Excel file to a DataTable. Currently it defaults to the first Sheet but getting it’s name using the following code:
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=myFilename.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"""
DataTable = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (schemaTable.Rows.Count > 0)
return schemaTable.Rows[0]["TABLE_NAME"].ToString();
else
return "Sheet1$"
This has been working fine until recently when the Excel document (we are receiving from a third party) started containing named Ranges. I’m there are no hidden sheets that I can find.
Now
schemaTable.Rows[0]["TABLE_NAME"].ToString()
returns the name of the first Range.
Is there something different I can do with my schemaTable object to identity just the sheets and not the named Ranges in the sheet?
Is the use of OleDbConnection mandatory or you can switch to other options (DAO for instance) ?
alternative solution 1 : using interop :
Source : http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/21d3f77c-1d3d-44e0-9bd5-eca45a0affa6
Alternative solution 2 : using DAO :