mjmcinto
mjmcinto

Reputation: 113

Reading Excel file w/ADO.net - no data (or tables)

This is my first attempt to read an Excel 2007 file via ADO.net, and I must be missing something b/c when I try to run the query, I get an exception. When I started looking, it's b/c the table (worksheet) isn't there. Can someone please tell me what I'm doing wrong?

Here is my code:

        string cs = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=My File.xlsx;Extended Properties=""Excel 12.0;IMEX=1;""";

        using (OleDbConnection con = new OleDbConnection(cs))
        {

            con.Open();

            string query = "SELECT * FROM [Sheet1$]";

            OleDbCommand cmd = new OleDbCommand(query, con); 

            OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);

            DataTable dt = new DataTable();

            DataTable worksheets = con.GetSchema("Tables");


            adapter.Fill(dt);
            .
            .
            .
         }

Upvotes: 1

Views: 1014

Answers (1)

JayG
JayG

Reputation: 632

Take a look at the accepted answer here

The First Column of the excel file to put in string variable C#?

It works for Excel 2003 but I think it could easily be adapted to work with 2007.

Upvotes: 1

Related Questions