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; } }