'Get Dropdown item value while reading Excel file - C#
I'm reading an Excel spreadsheet using the microsoft.office.interop.excel library in C#. I need to first determine that a cell in the sheet contains a Dropdown and then read the value of the Dropdown.
I'm running through the rows of the sheet and parsing values, but it's possible that a cell contains a Dropdown with a selection instead of a text value and I'm having problems trying to get the Dropdown from a cell in order to read it's selected value.
The pseudo code below is an example of what I'm trying:
for(int row = 1; row <= rowCount; row++)
{
for(int col = 1; col <= colCount; col++)
{
//Have the row and column number of the individual cell here
Range range = worksheet.Cells.Item[row,col]; //Get the thing in the cell?
Type type = range.GetType(); //Try to get a type so I can compare it against a Dropdown?
string menuVal = range.Value(); //Both this and range.Value2() is null
}
}
What I'd like to do is something like this fantasy code:
for( -row loop- )
{
for( -col loop- )
{
if (Current Cell contains a Dropdown)
{
Dropdown menu = Get the Dropdown in this cell;
string value = Get the 'Dropdown' value;
}
}
}
Can someone please help me out here? I haven't been able to find any documentation on how to GET the value - plenty on how to create the Dropdown itself though.
Edit: Changed 'Menu' to 'Dropdown'. Dropdown is the actual class that's being used in the code.
Solution in a nutshell: The Dropdown class from microsoft.office.interop should -NOT- be used if you care about trying to actually retrieve the value of the dropdown programmatically. This has been either deprecated or is just something Microsoft doesn't want you using. Instead you should use the method that sets up the Dropdown validation by using a separate worksheet that contains the values for the dropdown menu. You can then read the Value of the Dropdown cell as though it's just text.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|