0

I just started C# today, and i have trouble reading an excel file.

Here's what i did :

OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + @";Extended Properties=""Excel 12.0;HDR=YES;""");

OleDbCommand command = new OleDbCommand("SELECT * FROM [Sheet$]", connection);

connection.Open();

OleDbDataReader reader = command.ExecuteReader();

And i have an exception saying it can't find "Sheet$" (i can't copy/paste the exception, because the message is in french, and i don't know yet how to have generic messages in english)

Could someone tell me what did i do wrong ?

I followed what they say in tutorials or like here Reading Excel files from C#

Thanks, really !

2
  • 1
    Is the name of the Sheet 'Sheet$'? Have you tried Sheet1? Commented Jul 21, 2010 at 15:21
  • The name is Sheet. I thought the $ was mandatory, it's in all examples. But with [Sheet] the exception says that it can't find "Sheet". Commented Jul 21, 2010 at 15:59

1 Answer 1

1

You can get a list of "Tables" (worksheets or named ranges) from your Excel file:

DataTable schema = connection.GetSchema(OleDbMetaDataCollectionNames.Tables);

The TABLE_NAME field is the one you should use in your query. If the value has single quotes around it, you'll need to include those. For example, my file has worksheets named "GP" and "Kimberly Clark". In the schema table, they appear as "GP$" and "'Kimberly Clark$'". In a query, I would reference them as "[GP$]" or "['Kimberly Clark$']".

Sign up to request clarification or add additional context in comments.

1 Comment

i restarted VS and my code worked, i don't know why. But what you said is really useful, thanks !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.