none
Getting value from an Excel range is returning null although range is found RRS feed

  • Question

  • I open an Excel document followed by the relevant sheet and then loop through all properties in a class.

    If the property name matches the range name e.g. a cell is named FacilityAmount in the spreadsheet and the property is named FacilityAmount I want to get the value out of the specified range.

    I know that the range name is found but the valueArray is always null or 0. The value inside the spreadsheet is 100,000.

            foreach (PropertyInfo property in properties)
            {
                try
                {
                    string propertyName = property.Name;
                    Microsoft.Office.Interop.Excel.Range rng = worksheet.get_Range(propertyName, Type.Missing);
                    object valueArray = rng.get_Value(XlRangeValueDataType.xlRangeValueDefault);
    
                    //Do something with valueArray but it's returning null?
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    //We have to leave this catch blank as some cells don't have range names (intentional).
                    //We could put a warning that the range name did not exist but it's too detrimental to the user experience
                }
            }

    What am I missing here? C#, Excel, ASP.NET-CORE, Excel Interop 

    Tuesday, April 16, 2019 2:15 PM