Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9136847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:00:31+00:00 2026-06-17T09:00:31+00:00

I have a need to read in some client data from an Excel sheet.

  • 0

I have a need to read in some client data from an Excel sheet. I found it very quick and easy to read the *.xls as data, this way:

string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\MyExcelFile.xls;Extended Properties=\"Excel 8.0;HDR=YES\"";
using (var conn = new System.Data.OleDb.OleDbConnection(connString)) {
    conn.Open();
    System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("Select * From [SheetName$]", conn);
    OleDbDataReader reader = cmd.ExecuteReader();
    int firstNameOrdinal = reader.GetOrdinal("First Name");
    int lastNameOrdinal = reader.GetOrdinal("Last Name");
    while (reader.Read()) {
        Console.WriteLine("First Name: {0}, Last Name: {1}", 
            reader.GetString(firstNameOrdinal), 
            reader.GetString(lastNameOrdinal));
    }
}

The problem comes in at the 4th line: SELECT * FROM [SheetName$]. In this example, you need to replace "SheetName" with the name of the worksheet you are trying to read. Most of the time, the client names this the same, and all is fine.

But sometimes they don’t, and that means some manual work on our part to fix it. There is always just one sheet (in this case anyway) and so I’d rather just tell the code to “use the first sheet, whatever it is named”.

I haven’t been able to find a way to accomplish this using this technique, so I’m reaching out to the community. Thanks in advance for any advice.

PS – We have to go through a length approval process for external files (e.g. FileHelpers) and I prefer using this process because it does not require a third-party library. I’d prefer to limit answers to this method if possible.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:00:32+00:00Added an answer on June 17, 2026 at 9:00 am

    The answer here might suit your needs:

    http://social.msdn.microsoft.com/forums/en-US/adodotnetdataproviders/thread/711cf5f9-75fc-4a02-9a96-08aec48dad69/

    It uses .GetOleDbSchemaTable() to get a list of sheet names in a workbook.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read and write some data through named pipes. I have tested
I have a SqlDataReader and need to read a varbinary(max) column from it using
I have this file which I need to read the first bytes to check
I have a database table with a field that I need to read from
I have a data file, which I need to read. I know to read
i want to read some data from a text file.for example: 1 // total
I have another puzzling problem. I need to read .xls files with RODBC. Basically
I have some non-trivial computational code that need to be applied on data already
I have a request for some contract work from an organization that uses Excel
I need to read test.TXT file(tab delimited) into MATLAB. TXT file have form: Datum

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.