Cocoa Dev
Cocoa Dev

Reputation: 9541

C# and read values from an Excel file

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?

Upvotes: 2

Views: 3155

Answers (5)

uadrive
uadrive

Reputation: 1269

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" />

Upvotes: 0

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112259

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));
    }
}

Upvotes: 4

Paige Cook
Paige Cook

Reputation: 22555

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

Upvotes: 0

Lester
Lester

Reputation: 4403

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

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245389

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

Upvotes: 2

Related Questions