예제 #1
0
        private TOutput ParseDataRow(TypeInfo typeInfo)
        {
            TOutput row        = new TOutput();
            int     colInRange = 0;

            for (int col = 0; col < ExcelDataReader.FieldCount; col++)
            {
                if (HasRange && col > Range.EndColumnIfSet)
                {
                    break;
                }
                if (HasRange && (col + 1) < Range.StartColumn)
                {
                    continue;
                }
                if (colInRange > typeInfo.PropertyLength)
                {
                    break;
                }
                if (!typeInfo.ExcelIndex2PropertyIndex.ContainsKey(colInRange))
                {
                    continue;
                }
                PropertyInfo propInfo = typeInfo.Properties[typeInfo.ExcelIndex2PropertyIndex[colInRange]];
                object       value    = ExcelDataReader.GetValue(col);
                propInfo.SetValue(row, TypeInfo.CastPropertyValue(propInfo, value.ToString()));
                colInRange++;
            }
            return(row);
        }
예제 #2
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);
            }
        }
예제 #3
0
 private void ParseHeader()
 {
     for (int col = 0, colNrInRange = -1; col < ExcelDataReader.FieldCount; col++)
     {
         if (HasRange && col > Range.EndColumnIfSet)
         {
             break;
         }
         if (HasRange && (col + 1) < Range.StartColumn)
         {
             continue;
         }
         colNrInRange++;
         string value = Convert.ToString(ExcelDataReader.GetValue(col));
         HeaderColumns.Add(value);
     }
     IsHeaderRead = true;
 }