private void SetDisplayFormat(ExcelColumnAttribute columnAttr, ExcelDynamicColumn column)
 {
     if (columnAttr.IgnoreDisplayFormat)
     {
         column.DisplayFormat = null;
     }
     else if (!string.IsNullOrWhiteSpace(columnAttr.DisplayFormat))
     {
         column.DisplayFormat = columnAttr.DisplayFormat;
     }
 }
 private void SetAggregationFunction(ExcelColumnAttribute columnAttr, ExcelDynamicColumn column)
 {
     if (columnAttr.NoAggregate)
     {
         column.AggregateFunction = AggregateFunction.NoAggregation;
     }
     else if (columnAttr.AggregateFunction != AggregateFunction.NoAggregation)
     {
         column.AggregateFunction = columnAttr.AggregateFunction;
     }
 }
Beispiel #3
0
        private void SetCellsDisplayFormat(IXLRange range, IList <ExcelDynamicColumn> columns)
        {
            for (int i = 0; i < columns.Count; i++)
            {
                ExcelDynamicColumn column = columns[i];
                if (string.IsNullOrWhiteSpace(column.DisplayFormat) || column.DataType == null)
                {
                    continue;
                }

                if (column.DataType.IsNumeric())
                {
                    range.Cells().ElementAt(i).Style.NumberFormat.Format = column.DisplayFormat;
                }
                else if (column.DataType == typeof(DateTime) || column.DataType == typeof(DateTime?))
                {
                    range.Cells().ElementAt(i).Style.DateFormat.Format = column.DisplayFormat;
                }
            }
        }
        public IList <ExcelDynamicColumn> GetColumnsList(Type type)
        {
            if (type == null)
            {
                return(new List <ExcelDynamicColumn>());
            }

            BindingFlags flags = BindingFlags.Instance | BindingFlags.Public;

            MemberInfo[] probableExcelColumns = type.GetFields(flags)
                                                .AsEnumerable <MemberInfo>()
                                                .Concat(type.GetProperties(flags))
                                                .Where(m => !m.IsDefined(typeof(NoExcelColumnAttribute), true)).ToArray();

            IList <ExcelDynamicColumn> result = new List <ExcelDynamicColumn>();

            foreach (MemberInfo probableColumnMember in probableExcelColumns)
            {
                Type memberType = probableColumnMember is PropertyInfo p ? p.PropertyType : ((FieldInfo)probableColumnMember).FieldType;
                var  columnAttr = Extensions.CustomAttributeExtensions.GetCustomAttribute <ExcelColumnAttribute>(probableColumnMember);
                if (columnAttr != null)
                {
                    var excelColumn = new ExcelDynamicColumn(probableColumnMember.Name, memberType, columnAttr.Caption)
                    {
                        Width           = columnAttr.Width > 0 ? columnAttr.Width : (double?)null,
                        AdjustToContent = columnAttr.AdjustToContent,
                        Order           = columnAttr.Order,
                    };
                    SetAggregationFunction(columnAttr, excelColumn);
                    SetDisplayFormat(columnAttr, excelColumn);

                    result.Add(excelColumn);
                }
                else if (memberType.IsExtendedPrimitive() || memberType.IsEnum)
                {
                    result.Add(new ExcelDynamicColumn(probableColumnMember.Name, memberType));
                }
            }

            return(result.OrderBy(c => c.Order).ToList());
        }
Beispiel #5
0
        private void SetColumnsWidth(IXLRange range, IList <ExcelDynamicColumn> columns)
        {
            for (int i = 0; i < columns.Count; i++)
            {
                ExcelDynamicColumn column = columns[i];
                if (column.Width == null && !column.AdjustToContent)
                {
                    continue;
                }

                if (Type == PanelType.Vertical)
                {
                    IXLColumn excelColumn = range.Cell(1, i + 1).WorksheetColumn();
                    if (column.Width != null)
                    {
                        excelColumn.Width = column.Width.Value;
                    }
                    if (column.AdjustToContent)
                    {
                        excelColumn.AdjustToContents();
                    }
                }
                else
                {
                    IXLRow excelRow = range.Cell(i + 1, 1).WorksheetRow();
                    if (column.Width != null)
                    {
                        excelRow.Height = column.Width.Value;
                    }
                    if (column.AdjustToContent)
                    {
                        excelRow.AdjustToContents();
                    }
                }
            }
        }