示例#1
0
        public LoadFromCollection(ExcelRangeBase range, IEnumerable <T> items, LoadFromCollectionParams parameters) : base(range, parameters)
        {
            _items             = items;
            _members           = parameters.Members;
            _bindingFlags      = parameters.BindingFlags;
            _headerParsingType = parameters.HeaderParsingType;
            var type = typeof(T);

            if (_members == null)
            {
                _members = type.GetProperties(_bindingFlags);
            }
            else
            {
                if (_members.Length == 0)   //Fixes issue 15555
                {
                    throw (new ArgumentException("Parameter Members must have at least one property. Length is zero"));
                }
                foreach (var t in _members)
                {
                    if (t.DeclaringType != null && t.DeclaringType != type)
                    {
                        _isSameType = false;
                    }
                    //Fixing inverted check for IsSubclassOf / Pullrequest from tomdam
                    if (t.DeclaringType != null && t.DeclaringType != type && !TypeCompat.IsSubclassOf(type, t.DeclaringType) && !TypeCompat.IsSubclassOf(t.DeclaringType, type))
                    {
                        throw new InvalidCastException("Supplied properties in parameter Properties must be of the same type as T (or an assignable type from T)");
                    }
                }
            }
        }
示例#2
0
        public LoadFromCollection(ExcelRangeBase range, IEnumerable <T> items, LoadFromCollectionParams parameters) : base(range, parameters)
        {
            _items             = items;
            _bindingFlags      = parameters.BindingFlags;
            _headerParsingType = parameters.HeaderParsingType;
            var type      = typeof(T);
            var tableAttr = type.GetFirstAttributeOfType <EpplusTableAttribute>();

            if (tableAttr != null)
            {
                ShowFirstColumn = tableAttr.ShowFirstColumn;
                ShowLastColumn  = tableAttr.ShowLastColumn;
                ShowTotal       = tableAttr.ShowTotal;
            }
            if (parameters.Members == null)
            {
                var columns = SetupColumns();
                _columns = columns.ToArray();
            }
            else
            {
                _columns = parameters.Members.Select(x => new ColumnInfo {
                    MemberInfo = x
                }).ToArray();
                if (_columns.Length == 0)   //Fixes issue 15555
                {
                    throw (new ArgumentException("Parameter Members must have at least one property. Length is zero"));
                }
                foreach (var columnInfo in _columns)
                {
                    if (columnInfo.MemberInfo == null)
                    {
                        continue;
                    }
                    var member = columnInfo.MemberInfo;
                    if (member.DeclaringType != null && member.DeclaringType != type)
                    {
                        _isSameType = false;
                    }

                    //Fixing inverted check for IsSubclassOf / Pullrequest from tomdam
                    if (member.DeclaringType != null && member.DeclaringType != type && !TypeCompat.IsSubclassOf(type, member.DeclaringType) && !TypeCompat.IsSubclassOf(member.DeclaringType, type))
                    {
                        throw new InvalidCastException("Supplied properties in parameter Properties must be of the same type as T (or an assignable type from T)");
                    }
                }
            }
        }
示例#3
0
        /// <summary>
        /// Load a collection into the worksheet starting from the top left row of the range.
        /// </summary>
        /// <typeparam name="T">The datatype in the collection</typeparam>
        /// <param name="collection">The collection to load</param>
        /// <param name="paramsConfig"><see cref="Action{LoacFromCollectionParams}"/> to provide parameters to the function</param>
        /// <example>
        /// <code>
        /// sheet.Cells["C1"].LoadFromCollection(items, c =>
        /// {
        ///     c.PrintHeaders = true;
        ///     c.TableStyle = TableStyles.Dark1;
        /// });
        /// </code>
        /// </example>
        /// <returns>The filled range</returns>
        public ExcelRangeBase LoadFromCollection <T>(IEnumerable <T> collection, Action <LoadFromCollectionParams> paramsConfig)
        {
            var param = new LoadFromCollectionParams();

            paramsConfig.Invoke(param);
            if (collection is IEnumerable <IDictionary <string, object> > )
            {
                if (param.Members == null)
                {
                    return(LoadFromDictionaries(collection as IEnumerable <IDictionary <string, object> >, param.PrintHeaders, param.TableStyle));
                }
                return(LoadFromDictionaries(collection as IEnumerable <IDictionary <string, object> >, param.PrintHeaders, param.TableStyle, param.Members.Select(x => x.Name)));
            }
            var func = new LoadFromCollection <T>(this, collection, param);

            return(func.Load());
        }
示例#4
0
        /// <summary>
        /// Load a collection into the worksheet starting from the top left row of the range.
        /// </summary>
        /// <typeparam name="T">The datatype in the collection</typeparam>
        /// <param name="Collection">The collection to load</param>
        /// <param name="PrintHeaders">Print the property names on the first row. Any underscore in the property name will be converted to a space. If the property is decorated with a <see cref="DisplayNameAttribute"/> or a <see cref="DescriptionAttribute"/> that attribute will be used instead of the reflected member name.</param>
        /// <param name="TableStyle">Will create a table with this style. If set to TableStyles.None no table will be created</param>
        /// <param name="memberFlags">Property flags to use</param>
        /// <param name="Members">The properties to output. Must be of type T</param>
        /// <returns>The filled range</returns>
        public ExcelRangeBase LoadFromCollection <T>(IEnumerable <T> Collection, bool PrintHeaders, TableStyles TableStyle, BindingFlags memberFlags, MemberInfo[] Members)
        {
            if (Collection is IEnumerable <IDictionary <string, object> > )
            {
                if (Members == null)
                {
                    return(LoadFromDictionaries(Collection as IEnumerable <IDictionary <string, object> >, PrintHeaders, TableStyle));
                }
                return(LoadFromDictionaries(Collection as IEnumerable <IDictionary <string, object> >, PrintHeaders, TableStyle, Members.Select(x => x.Name)));
            }
            var param = new LoadFromCollectionParams
            {
                PrintHeaders = PrintHeaders,
                TableStyle   = TableStyle,
                BindingFlags = memberFlags,
                Members      = Members
            };
            var func = new LoadFromCollection <T>(this, Collection, param);

            return(func.Load());
        }