Jason94
Jason94

Reputation: 13610

How can I get A1 cell from a 97/03 Excel document with OleDB?

I've got a Excel 97/03 document that has "blabla" in its A1 cell in sheet "Sheet1". I thought the following should be able to extract it:

      string con = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Book1.xls;" + @"Extended Properties='Excel 8.0;HDR=Yes;'";
      using (OleDbConnection connection = new OleDbConnection(con))
      {
        connection.Open();

        OleDbDataAdapter da = new OleDbDataAdapter("Select * From [Sheet1$]", connection);

        DataTable dt = new DataTable();
        da.Fill(dt);

        dynamic cellA1 = dt.Rows[0][0].ToString();

But cellA1 is empty (""). Anyone know how to fix this, I should be able to treat it as a database and get cells from it?

Upvotes: 0

Views: 126

Answers (2)

Abdul Rehman
Abdul Rehman

Reputation: 415

"HDR=Yes;" indicates that the first row contains columnnames, not data. "HDR=No;" indicates the opposite. maybe thats the issue.

Upvotes: 1

Chawin
Chawin

Reputation: 1466

The datatable is using the first row of data as its headers, to access the A1 cell simply use the name of the first column:

dynamic cellA1 = dt.Columns[0].ToString();

Upvotes: 1

Related Questions