How can I use named range in Excel with OleDB?

2019-07-20 05:48发布

问题:

I'm trying to extract data from a specific named range in Excel with ASP .NET/C#. Here is an exemple of what I'm trying to extract.

What I want is "B", "C", "D" by using the name "RANGE_NAMED". Is it possible to do this with OleDB ?

Best regards,

Alex.

回答1:

You could try this code

using(OleDbConnection c = new OleDbConnection(con))
{
    c.Open();
    string selectString = "SELECT * FROM [RANGE_NAMED]";
    using(OleDbCommand cmd1 = new OleDbCommand(selectString))
    {
          cmd1.Connection = c;
        var result = cmd1.ExecuteReader();
        while(result.Read())
        {
              Console.WriteLine(result[0].ToString());
        }
    }
}


回答2:

Ok, It's was obvious and I don't know why it didn't work the first time...

SELECT * FROM RANGE_NAMED

And I get B, C, D.