示例#1
0
        private IEnumerable <RowInfo <T> > Take <T>(ISheet sheet, int maxErrorRows, Func <T> objectInitializer = null) where T : class
        {
            if (sheet == null || sheet.PhysicalNumberOfRows < 1)
            {
                yield break;
            }

            var firstRowIndex = sheet.FirstRowNum;
            var firstRow      = sheet.GetRow(firstRowIndex);

            var targetType = typeof(T);

            if (targetType == typeof(object)) // Dynamic type.
            {
                targetType = GetDynamicType(sheet);
                MapHelper.LoadDynamicAttributes(Attributes, DynamicAttributes, targetType);
            }

            // Scan object attributes.
            MapHelper.LoadAttributes(Attributes, targetType);

            // Read the first row to get column information.
            var columns = GetColumns(firstRow, targetType);

            // Detect column format based on the first non-null cell.
            Helper.LoadDataFormats(sheet, HasHeader ? firstRowIndex + 1 : firstRowIndex, columns, TypeFormats);

            if (TrackObjects)
            {
                Objects[sheet.SheetName] = new Dictionary <int, object>();
            }

            // Loop rows in file. Generate one target object for each row.
            var errorCount = 0;

            foreach (IRow row in sheet)
            {
                if (maxErrorRows > 0 && errorCount >= maxErrorRows)
                {
                    break;
                }
                if (HasHeader && row.RowNum == firstRowIndex)
                {
                    continue;
                }

                var obj = objectInitializer == null?Activator.CreateInstance(targetType) : objectInitializer();

                var rowInfo = new RowInfo <T>(row.RowNum, obj as T, -1, string.Empty);
                LoadRowData(columns, row, obj, rowInfo);

                if (rowInfo.ErrorColumnIndex >= 0)
                {
                    errorCount++;
                    rowInfo.Value = default(T);
                }
                if (TrackObjects)
                {
                    Objects[sheet.SheetName][row.RowNum] = rowInfo.Value;
                }

                yield return(rowInfo);
            }
        }
示例#2
0
        private IRow PopulateFirstRow(ISheet sheet, List <ColumnInfo> columns, Type type)
        {
            var row = sheet.CreateRow(sheet.FirstRowNum);

            // Use existing column populate the first row.

            if (columns != null)
            {
                foreach (var column in columns)
                {
                    var cell = row.CreateCell(column.Attribute.Index);

                    if (!HasHeader)
                    {
                        continue;
                    }

                    SetCell(cell, column.Attribute.Name ?? column.HeaderValue, column, true);
                }

                return(row);
            }

            // If no column cached, populate the first row with attributes and object properties.

            MapHelper.LoadAttributes(Attributes, type);

            var attributes = Attributes.Where(p => p.Value.Property != null && p.Value.Property.ReflectedType == type);
            var properties = new List <PropertyInfo>(type.GetProperties(MapHelper.BindingFlag));

            // Firstly populate for those have Attribute specified.
            foreach (var pair in attributes)
            {
                var pi        = pair.Key;
                var attribute = pair.Value;
                if (pair.Value.Index < 0)
                {
                    continue;
                }

                var cell = row.CreateCell(attribute.Index);
                if (HasHeader)
                {
                    cell.SetCellValue(attribute.Name ?? pi.Name);
                }
                properties.Remove(pair.Key); // Remove populated property.
            }

            var index = 0;

            // Then populate for those do not have Attribute specified.
            foreach (var pi in properties)
            {
                var attribute = Attributes.ContainsKey(pi) ? Attributes[pi] : null;
                if (attribute?.Ignored == true)
                {
                    continue;
                }

                while (row.GetCell(index) != null)
                {
                    index++;
                }
                var cell = row.CreateCell(index);
                if (HasHeader)
                {
                    cell.SetCellValue(attribute?.Name ?? pi.Name);
                }
                else
                {
                    new ColumnAttribute {
                        Index = index, Property = pi
                    }.MergeTo(Attributes);
                }
                index++;
            }

            return(row);
        }