Ejemplo n.º 1
0
        private List <ColumnInfo> GetColumns(IRow headerRow, Type type)
        {
            //
            // Column mapping priority:
            // Map<T> > ColumnAttribute > naming convention > column filter.
            //

            var sheetName    = headerRow.Sheet.SheetName;
            var columns      = new List <ColumnInfo>();
            var columnsCache = new List <object>(); // Cached for export usage.

            // Prepare a list of ColumnInfo by the first row.
            foreach (ICell header in headerRow)
            {
                // Custom mappings via attributes.
                var column = GetColumnInfoByAttribute(header, type);

                // Naming convention.
                if (column == null && HasHeader && MapHelper.GetCellType(header) == CellType.String)
                {
                    var s = header.StringCellValue;

                    if (!string.IsNullOrWhiteSpace(s))
                    {
                        column = GetColumnInfoByName(s.Trim(), header.ColumnIndex, type);
                    }
                }

                // Column filter.
                if (column == null)
                {
                    column = GetColumnInfoByFilter(header, _columnFilter);

                    if (column != null) // Set default resolvers since the column is not mapped explicitly.
                    {
                        column.Attribute.TryPut  = _defaultPutResolver;
                        column.Attribute.TryTake = _defaultTakeResolver;
                    }
                }

                if (column == null)
                {
                    continue;                 // No property was mapped to this column.
                }
                if (header.CellStyle != null)
                {
                    column.HeaderFormat = header.CellStyle.DataFormat;
                }
                columns.Add(column);
                columnsCache.Add(column);
            }

            var typeDict = TrackedColumns.ContainsKey(sheetName)
                ? TrackedColumns[sheetName]
                : TrackedColumns[sheetName] = new Dictionary <Type, List <object> >();

            typeDict[type] = columnsCache;

            return(columns);
        }
Ejemplo n.º 2
0
        private List <ColumnInfo <T> > GetColumns <T>(IRow headerRow)
        {
            //
            // Column mapping priority:
            // Map<T> > ColumnAttribute > naming convention > DefaultResolverType.
            //

            var sheetName    = headerRow.Sheet.SheetName;
            var columns      = new List <ColumnInfo <T> >();
            var columnsCache = new List <object>(); // Cached for export usage.

            // Prepare a list of ColumnInfo by the first row.
            foreach (ICell header in headerRow)
            {
                // Custom mappings via attributes.
                var column = GetColumnInfoByAttribute <T>(header);

                // Naming convention.
                if (column == null && HasHeader && MapHelper.GetCellType(header) == CellType.String)
                {
                    var s = header.StringCellValue;

                    if (!string.IsNullOrWhiteSpace(s))
                    {
                        column = GetColumnInfoByName <T>(s.Trim(), header.ColumnIndex);
                    }
                }

                // DefaultResolverType
                if (column == null)
                {
                    column = GetColumnInfoByResolverType <T>(header, DefaultResolverType);
                }

                if (column == null)
                {
                    continue;                 // No property was mapped to this column.
                }
                if (header.CellStyle != null)
                {
                    column.HeaderFormat = header.CellStyle.DataFormat;
                }
                columns.Add(column);
                columnsCache.Add(column);
            }

            var typeDict = TrackedColumns.ContainsKey(sheetName)
                ? TrackedColumns[sheetName]
                : TrackedColumns[sheetName] = new Dictionary <Type, List <object> >();

            typeDict[typeof(T)] = columnsCache;

            return(columns);
        }
Ejemplo n.º 3
0
        private List<ColumnInfo> GetTrackedColumns(string sheetName, Type type)
        {
            if (!TrackedColumns.ContainsKey(sheetName)) return null;

            IEnumerable<ColumnInfo> columns = null;

            var cols = TrackedColumns[sheetName];
            if (cols.ContainsKey(type))
            {
                columns = cols[type].OfType<ColumnInfo>();
            }

            return columns?.ToList();
        }
Ejemplo n.º 4
0
        private List <ColumnInfo <T> > GetTrackedColumns <T>(string sheetName)
        {
            if (!TrackedColumns.ContainsKey(sheetName))
            {
                return(null);
            }

            IEnumerable <ColumnInfo <T> > columns = null;

            var cols = TrackedColumns[sheetName];
            var type = typeof(T);

            if (cols.ContainsKey(type))
            {
                columns = cols[type].OfType <ColumnInfo <T> >();
            }

            return(columns?.ToList());
        }