Beispiel #1
0
 public void GetHeaders(IExcelDataReader reader)
 {
     foreach (int headerIndex in this.ModelColumnsNumber)
     {
         int excelIndex = headerIndex + 1;
         this.Headers.Add(new Header
         {
             Column = excelIndex,
             Letter = ExcelExtension.ColumnIndexToColumnLetter(excelIndex),
             Name   = Convert.ToString(reader.GetValue(headerIndex)),
             Index  = headerIndex
         });
     }
 }
Beispiel #2
0
        private bool TryGetAttribute(object instance, PropertyInfo property, object data, int columnNumber, int rowNumber)
        {
            Type     propertyType = property.PropertyType;
            ErrorMap errorMap     = new ErrorMap
            {
                Line         = rowNumber,
                ExcelLine    = rowNumber + 1,
                Column       = columnNumber,
                ExcelColumn  = columnNumber + 1,
                ColumnLetter = ExcelExtension.ColumnIndexToColumnLetter(columnNumber)
            };

            if (propertyType == typeof(DateTime))
            {
                DateTime objectData = new DateTime();
                if (DateUtil.TryParserObject(data, out objectData))
                {
                    property.SetValue(instance, objectData);
                    return(true);
                }
                errorMap.Description = "Error en el formato de la fecha";
                Results.ErrorMap.Add(errorMap);
                return(false);
            }
            if (propertyType == typeof(DateTime?))
            {
                string dataStr = Convert.ToString(data);
                if (String.IsNullOrWhiteSpace(dataStr))
                {
                    property.SetValue(instance, null);
                    return(true);
                }
                DateTime objectData = new DateTime();
                if (DateUtil.TryParserObject(data, out objectData))
                {
                    property.SetValue(instance, objectData);
                    return(true);
                }
                errorMap.Description = "Error en el formato de la fecha";
                Results.ErrorMap.Add(errorMap);
                return(false);
            }
            try
            {
                if (propertyType == typeof(string))
                {
                    property.SetValue(instance, Convert.ToString(data));
                }
                else
                {
                    object value = null;
                    if (FormatUtils.TryGetValue(data, property.PropertyType, out value))
                    {
                        property.SetValue(instance, value);
                    }
                }
            }
            catch (InvalidCastException e)
            {
                errorMap.Description = "Error en el tipo de dato no coincide con el especificado";
                Results.ErrorMap.Add(errorMap);
                return(false);
            }
            catch (FormatException e)
            {
                errorMap.Description = "Error en el tipo de dato no coincide con el especificado";
                Results.ErrorMap.Add(errorMap);
                return(false);
            }
            return(true);
        }