public static object[,] Range2Matrix(object objRange) { var range = (Range)objRange; var values = (object[, ])range.get_Value(Type.Missing); var formulas = (object[, ])range.Formula; if (values.GetUpperBound(0) != formulas.GetUpperBound(0) || values.GetUpperBound(1) != formulas.GetUpperBound(1) || values.GetLowerBound(0) != formulas.GetLowerBound(0) || values.GetLowerBound(1) != formulas.GetLowerBound(1)) { ExcelApiException.Throw(string.Format(ExcelApiResource.ErrorRangeToMatrix, GetExcelAddress(range))); } for (int i = values.GetLowerBound(0); i <= values.GetUpperBound(0); ++i) { for (int j = values.GetLowerBound(1); j <= values.GetUpperBound(1); ++j) { string formula = (string)formulas[i, j]; if (IsError(formula)) { values[i, j] = null; } } } return(values); }
public static void SetValue(object target, string name, object value) { try { Type type = target.GetType(); FieldInfo field = type.GetField(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase); if (field != null) { var values = new[] { ChangeType(value, field.FieldType) }; type.InvokeMember(field.Name, BindingFlags.SetField, null, target, values); } else { PropertyInfo property = type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase); if (property != null) { property.SetValue(target, ChangeType(value, property.PropertyType), new object[] { }); } else { ExcelApiException.Throw(string.Format(ExcelApiResource.ErrorClassMemberNotExist, target.GetType().FullName, name)); } } } catch (Exception exp) { string message = string.Format(ExcelApiResource.ErrorInvalidPropertyValue, "RangeConversion.SetValue", name, value); MethodErrors.Instance.Add(exp, message, false); } }