Esempio n. 1
0
        protected virtual object GetFieldOrPropertyValue(object rowObject, string name)
        {
            var rowValue = FormatterUtils.GetFieldOrPropertyValue(rowObject, name);

            if (rowValue is DateTimeOffset)
            {
                return(FormatterUtils.ConvertFromDateTimeOffset((DateTimeOffset)rowValue));
            }

            else if (rowObject is Dictionary <string, object> )
            {
                return((rowObject as Dictionary <string, object>)[name]);
            }

            else if (FormatterUtils.IsExcelSupportedType(rowValue))
            {
                return(rowValue);
            }

            else if ((rowValue is IEnumerable <object>))
            {
                return(rowValue);
            }
            // return string.Join(",", rowValue as IEnumerable<object>);

            return(rowValue == null || DBNull.Value.Equals(rowValue)
                ? string.Empty
                : rowValue.ToString());
        }
 public bool IsExcelSupportedType(object expression)
 {
     return(FormatterUtils.IsExcelSupportedType(expression));
 }
        /// <summary>
        /// Get the <c>ExcelColumnInfo</c> for all members of a class.
        /// </summary>
        /// <param name="itemType">Type of item being serialised.</param>
        /// <param name="data">The collection of values being serialised. (Not used, provided for use by derived
        /// types.)</param>
        public virtual ExcelColumnInfoCollection GetExcelColumnInfo(Type itemType, object data, string namePrefix = "", bool isComplexColumn = false)
        {
            var fieldInfo = new ExcelColumnInfoCollection();

            if (itemType.Name.StartsWith("Dictionary"))
            {
                var prefix = namePrefix + "_Dict_";
                fieldInfo.Add(new ExcelColumnInfo(prefix, null, new ExcelColumnAttribute(), null));
                return(fieldInfo);
            }


            var fields     = GetSerialisableMemberNames(itemType, data);
            var properties = GetSerialisablePropertyInfo(itemType, data);


            // Instantiate field names and fieldInfo lists with serialisable members.
            foreach (var field in fields)
            {
                var prop = properties.FirstOrDefault(p => p.Name == field);

                if (prop == null)
                {
                    continue;
                }

                Type propertyType = prop.PropertyType;
                if (propertyType.IsGenericType &&
                    propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    propertyType = propertyType.GetGenericArguments()[0];
                }

                ExcelColumnAttribute attribute = FormatterUtils.GetAttribute <ExcelColumnAttribute>(prop);
                if (attribute != null)
                {
                    string prefix = string.IsNullOrEmpty(namePrefix) == false ? $"{namePrefix}:{prop.Name}" : prop.Name;

                    if (propertyType.Name.StartsWith("List"))
                    {
                        Type typeOfList = FormatterUtils.GetEnumerableItemType(propertyType);

                        //if (FormatterUtils.IsSimpleType(typeOfList))
                        //{
                        //    fieldInfo.Add(new ExcelColumnInfo(prefix, typeOfList, attribute, null));
                        //}
                        //else
                        if (typeOfList.FullName.EndsWith("CustomFieldModel") || typeOfList.Name.StartsWith("OverrideProperty"))
                        {
                            prefix += "_CustomField_";
                            fieldInfo.Add(new ExcelColumnInfo(prefix, null, attribute, null));
                        }
                        else
                        {
                            prefix += "_List_";
                            fieldInfo.Add(new ExcelColumnInfo(prefix, null, attribute, null));

                            //ExcelColumnInfoCollection columnCollection = GetExcelColumnInfo(typeOfList, null, prefix, true);
                            //foreach (var subcolumn in columnCollection)
                            //    fieldInfo.Add(subcolumn);
                        }
                    }
                    else if (propertyType.Name.EndsWith("CustomFieldModel") || propertyType.Name.StartsWith("OverrideProperty"))
                    {
                        prefix += "_CustomField_Single_";
                        fieldInfo.Add(new ExcelColumnInfo(prefix, null, attribute, null));
                    }
                    else if (propertyType.Name.StartsWith("Dictionary"))
                    {
                        prefix += "_Dict_";
                        fieldInfo.Add(new ExcelColumnInfo(prefix, null, attribute, null));
                    }
                    else if (!FormatterUtils.IsSimpleType(propertyType))
                    {
                        ExcelColumnInfoCollection columnCollection = GetExcelColumnInfo(propertyType, null, prefix, true);
                        foreach (var subcolumn in columnCollection)
                        {
                            fieldInfo.Add(subcolumn);
                        }
                    }
                    else
                    {
                        string propertyName = isComplexColumn ? $"{namePrefix}:{field}" : field;
                        string displayName  = propertyName;

                        if (attribute.DoNotUsePropertyName)
                        {
                            attribute.Header = namePrefix;
                        }

                        bool columnAlreadyadded = fieldInfo.Any(a => a.PropertyName == propertyName);
                        if (!columnAlreadyadded)
                        {
                            if (FormatterUtils.IsExcelSupportedType(propertyType))
                            {
                                fieldInfo.Add(new ExcelColumnInfo(propertyName, propertyType, attribute, null));
                            }
                            else
                            {
                                fieldInfo.Add(new ExcelColumnInfo(propertyName, typeof(string), attribute, null));
                            }
                        }
                    }
                }
            }

            PopulateFieldInfoFromMetadata(fieldInfo, itemType, data);

            return(fieldInfo);
        }