示例#1
0
        private TOutput ParseDataRow()
        {
            TOutput row      = (TOutput)Activator.CreateInstance(typeof(TOutput));
            bool    emptyRow = true;

            for (int col = 0, colNrInRange = -1; col < ExcelDataReader.FieldCount; col++)
            {
                if (HasRange && col > Range.EndColumnIfSet)
                {
                    break;
                }
                if (HasRange && (col + 1) < Range.StartColumn)
                {
                    continue;
                }
                colNrInRange++;
                emptyRow &= ExcelDataReader.IsDBNull(col);
                object value = ExcelDataReader.GetValue(col);
                if (TypeInfo.IsDynamic)
                {
                    var r = row as IDictionary <string, Object>;
                    if (HasHeaderData)
                    {
                        r.Add(HeaderColumns[colNrInRange], value);
                    }
                    else
                    {
                        r.Add("Column" + (colNrInRange + 1), value);
                    }
                }
                else
                {
                    PropertyInfo propInfo = null;
                    if (HasHeaderData && TypeInfo.ExcelColumnName2PropertyIndex.ContainsKey(HeaderColumns[colNrInRange]))
                    {
                        propInfo = TypeInfo.Properties[TypeInfo.ExcelColumnName2PropertyIndex[HeaderColumns[colNrInRange]]];
                    }
                    else if (TypeInfo.ExcelIndex2PropertyIndex.ContainsKey(colNrInRange))
                    {
                        propInfo = TypeInfo.Properties[TypeInfo.ExcelIndex2PropertyIndex[colNrInRange]];
                    }
                    propInfo?.TrySetValue(row, TypeInfo.CastPropertyValue(propInfo, value?.ToString()));
                }
            }
            if (emptyRow)
            {
                return(default(TOutput));
            }
            else
            {
                return(row);
            }
        }