Beispiel #1
0
        /// <summary>
        ///     Generates columns for all public properties on the type
        /// </summary>
        /// <returns></returns>
        internal List <WorksheetColumn <T> > AutoGenerateColumns()
        {
            var columns = new List <WorksheetColumn <T> >();

            List <KeyValuePair <PropertyInfo, ExcelTableColumnAttribute> > propertyAttributePairs = typeof(T).GetExcelTableColumnAttributes <T>();

            foreach (KeyValuePair <PropertyInfo, ExcelTableColumnAttribute> propertyAttributePair in propertyAttributePairs)
            {
                PropertyInfo property = propertyAttributePair.Key;
                ExcelTableColumnAttribute mappingAttribute = propertyAttributePair.Value;

                bool isNullableProperty = property.PropertyType.IsNullable();

                string header = !string.IsNullOrEmpty(mappingAttribute.ColumnName) ? mappingAttribute.ColumnName : Regex.Replace(property.Name, "[a-z][A-Z]", m => $"{m.Value[0]} {m.Value[1]}");

                var column = new WorksheetColumn <T>
                {
                    Header          = header,
                    Map             = GetGetter <T>(property.Name),
                    ConfigureColumn = c => c.AutoFit(),
                    ConfigureHeader = c => { c.Style.Font.Bold = !isNullableProperty; }
                };
                columns.Add(column);
            }

            return(columns);
        }
Beispiel #2
0
        /// <summary>
        ///     Generates columns for all public properties on the type
        /// </summary>
        /// <returns></returns>
        internal IList <WorksheetColumn <T> > AutoGenerateColumns()
        {
            var columns = new List <WorksheetColumn <T> >();

            Type type = typeof(T);

            PropertyInfo[] properties = type.GetProperties();

            foreach (PropertyInfo property in properties)
            {
                var mappingAttribute = (ExcelTableColumnAttribute)property.GetCustomAttributes(typeof(ExcelTableColumnAttribute), true).FirstOrDefault();

                if (mappingAttribute != null)
                {
                    string header = !string.IsNullOrEmpty(mappingAttribute.ColumnName) ? mappingAttribute.ColumnName : Regex.Replace(property.Name, "[a-z][A-Z]", m => $"{m.Value[0]} {m.Value[1]}");

                    var column = new WorksheetColumn <T>
                    {
                        Header          = header,
                        Map             = GetGetter <T>(property.Name),
                        ConfigureColumn = c => c.AutoFit()
                    };
                    columns.Add(column);
                }
            }

            return(columns);
        }