4

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.

Screenshot from my Excel file

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

Best regards,

Alex.

Alex
  • 2,927
  • 8
  • 37
  • 56

2 Answers2

4

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());
        }
    }
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • As I understand the query is only picking up the text data from the range. Can this be used to perform `HasFormula` on the cells in the range? – aspiring Jun 10 '15 at 07:50
  • If you refer to [this](https://msdn.microsoft.com/en-us/library/office/microsoft.office.interop.excel.range.hasformula.aspx) then I think no because this is not used via Interop but just OleDb – Steve Jun 10 '15 at 07:59
  • I have seen that link, and just wondering whether there's such properties within `OleDb`. If in anyway we could serialized the range taken into the above `result` and deserialize it into an excel range in C# while reading via the reader. I am just thinking out loud - if you can point out any relevant info, that's great. – aspiring Jun 10 '15 at 08:08
  • I can’t get this to work. It skips the while loop. I only have one value in my named range, does that effect it? – user8608110 Apr 18 '18 at 17:35
0

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.

Alex
  • 2,927
  • 8
  • 37
  • 56