Search code examples
c#asp.netexceloledbextract

How can I use named range in Excel with OleDB?


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.


Solution

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