2

I have an Excel file and the columns have names (data source is NOT controlled by me.. it's given to me by a client). Although the columns change, the column headers never change.

In the file, it's called "First Name"

How do I access the data in every cell within the same column?

1
  • Heads up: Excel column "headers" are not analogous to database column names--they are just regular cells with 'heading' text in them. So you need to read the text in the cells in the first row to get the "headers." Loop across the cells in the first row looking for one with the desired header text. Then once you find a cell whose text=column name, you know that the column below it contains the data Commented Dec 8, 2011 at 21:03

5 Answers 5

4

Open your Excel file as a database. Then you will not have to bother about the position of the columns:

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));
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This worked for me, though I had to change the build configuration from 'AnyCPU' to 'x86'. Apparently Microsoft.Jet.OLEDB.4.0 is not available when running x64.
Yes, unfortunately, Jet has no 64-bit interface.
2

I would take a look at this example from Microsoft:

How to query and display excel data by using ASP.NET, ADO.NET, and Visual C#.NET

Comments

0

I've used excelLibrary in the past and found it very easy to use.

Comments

0

I have successfully used the FileHelpers Library to read Excel files in the past.

Comments

0

You can do something like this where you use ODBC to connect to the file and download the content of a sheet.

 private bool DownloadExcelData(string fileName, ref DataTable informationDT)
            {
                // bool success
                bool success = true;

                // open the file via odbc
                string connection = ConfigurationManager.ConnectionStrings["xls"].ConnectionString;
                connection = String.Format(connection, FilePath + fileName);
                OleDbConnection conn = new OleDbConnection(connection);
                conn.Open();

                try
                {
                    // retrieve the records from the first page
                    OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Information$]", conn);
                    OleDbDataAdapter adpt = new OleDbDataAdapter(cmd);
                    adpt.Fill(informationDT);
                }
                catch { success = false; }

                // close the connection
                conn.Close();
                return success;
            }

Here are some sample ODBC connections for xls and xlsx files:

<add name="xls" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;'" />
    <add name="xlsx" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0" />

Comments

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.