use C# to get excel sheet names

Hi,

I have lots of excel files (*.xls) I need open and read it for further anslysis.

I use the following code to read excel sheet name. However, some excel files work some are not. for those has error. it  is because it will return extra strange sheet names which do not actually appear in the excel file.

for example an error in a excel file return me extra strange sheet names like my attached pictures. (It should be only 1 sheet3$)

I think the file does not have problem because I use Matlab to read excel sheet names. and it return me correct without any extra strange sheet names. like below

 [typ, Stru_sheet_names] = xlsfinfo('D:\VM_SSIS\xls_In\sample3.xls')

typ =

Microsoft Excel Spreadsheet

Stru_sheet_names =

    'Sheet1'    'Sheet2'    'Sheet3'    'Sheet4'    'Sheet5'    'Sheet6'    'Sheet7'    'Sheet8'

Could someone help me about this. thanks in advance



string str_conn_xls = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\VM_SSIS\\xls_In\\sample3.xls;Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1\"";

OleDbConnection objConn = null;
DataTable DT_sheet = null;

// Create and Open OleDbConnection
objConn = new OleDbConnection(str_conn_xls);
objConn.Open();

// Get the data table   
DT_sheet = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
objConn.Close();


 // get all sheet names
 for (int i = 0; i < DT_sheet.Rows.Count; i++) 
    {
      MessageBox.Show(DT_sheet.Rows[i][2].ToString());
    }

Open in new window

Snap1.jpg
Snap2.jpg
Snap3.jpg
rmtogetherAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SriVaddadiCommented:
You would all the names including the hidden names. These hidden names are created as a result of the activities on the worksheet.

Anything that ends with $ is a valid value and rest are system defined hidden values for system processing.

So you have to filter on the basis that the name should end with $ else ignore.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SriVaddadiCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.