private IEnumerable <RowInfo <T> > Take <T>(ISheet sheet, int maxErrorRows, Func <T> objectInitializer = null) { if (sheet == null || sheet.PhysicalNumberOfRows < 1) { yield break; } var firstRowIndex = sheet.FirstRowNum; var firstRow = sheet.GetRow(firstRowIndex); // Scan object attributes. MapHelper.LoadAttributes <T>(Attributes); // Read the first row to get column information. var columns = GetColumns <T>(firstRow); MapHelper.LoadDataFormats(sheet.GetRow(firstRowIndex + 1), columns); 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 data = GetRowData(columns, row, objectInitializer); if (data.ErrorColumnIndex >= 0) { errorCount++; } if (TrackObjects) { Objects[sheet.SheetName][row.RowNum] = data.Value; } yield return(data); } }
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 = GetFirstRowIndex(sheet); 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; var firstDataRowIndex = HasHeader ? firstRowIndex + 1 : firstRowIndex; foreach (IRow row in sheet) { if (maxErrorRows > 0 && errorCount >= maxErrorRows) break; if (row.RowNum < firstDataRowIndex) 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; } }
private IRow PopulateFirstRow <T>(ISheet sheet, List <ColumnInfo <T> > columns = null) { 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. var type = typeof(T); MapHelper.LoadAttributes <T>(Attributes); 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); }