예제 #1
0
        private void SetColumnsNumber <T>()
        {
            Type            type        = typeof(T);
            ConstructorInfo constructor = type.GetConstructor(new Type[0]);
            object          instance    = constructor.Invoke(new object[] { });

            PropertyInfo[] properties = type.GetProperties();
            this.ModelColumnsNumber = new List <int>();
            foreach (PropertyInfo property in properties)
            {
                ExcelColumn excelColumn = property.GetCustomAttribute <ExcelColumn>();
                if (excelColumn == null)
                {
                    continue;
                }
                int columnNumber = excelColumn.Column;
                if (!String.IsNullOrWhiteSpace(excelColumn.Letter))
                {
                    columnNumber = ExcelExtension.ColumnLetterToColumnIndex(excelColumn.Letter);
                }
                this.ModelColumnsNumber.Add(columnNumber);
            }
        }
예제 #2
0
        public bool TryGetObject <T>(DataRow dataRow, out T dataObj, int rowNumber)
        {
            Type            type        = typeof(T);
            ConstructorInfo constructor = type.GetConstructor(new Type[0]);
            object          instance    = constructor.Invoke(new object[] { });

            PropertyInfo[] properties = type.GetProperties();
            dataObj = default(T);
            dynamic dataObjRaw = new ExpandoObject();
            IDictionary <string, object> dynamicObjDictionary = dataObjRaw as IDictionary <string, object>;
            RowResult rowResult = new RowResult
            {
                Index  = rowNumber,
                Number = rowNumber + 1
            };

            foreach (PropertyInfo property in properties)
            {
                ExcelColumn excelColumn = property.GetCustomAttribute <ExcelColumn>();
                if (excelColumn == null)
                {
                    continue;
                }
                int columnNumber = excelColumn.Column;
                if (!String.IsNullOrWhiteSpace(excelColumn.Letter))
                {
                    columnNumber = ExcelExtension.ColumnLetterToColumnIndex(excelColumn.Letter);
                }
                if (columnNumber >= dataRow.ItemArray.Length)
                {
                    return(false);
                }
                object columnData = dataRow[columnNumber];
                dynamicObjDictionary.Add(property.Name, Convert.ToString(columnData));
                Header header    = this.Headers.FirstOrDefault(x => x.Index == columnNumber);
                string excelName = header.Name;
                if (String.IsNullOrWhiteSpace(excelName))
                {
                    excelName = String.Format("\"{0}\"", header.Letter);
                }
                //Validar
                IEnumerable <ValidationAttribute> validationAttributes = property.GetCustomAttributes <ValidationAttribute>();
                IList <ValidationResult>          result = null;

                ColumnResult columnResult = new ColumnResult
                {
                    Header = header
                };
                if (columnData != null)
                {
                    columnResult.Value = Convert.ToString(columnData);
                }
                if (!ValidatorUtil.ValidateValue(columnData, excelName, validationAttributes, out result))
                {
                    columnResult.ErrorMessages = result.Select(x => x.ErrorMessage).ToList();
                    rowResult.ColumnResults.Add(columnResult);
                    continue;
                }
                //if has a adapte process
                if (excelColumn.Adapter != null)
                {
                    string       dataStr = Convert.ToString(columnData);
                    ITypeAdapter adapter = (ITypeAdapter)Activator.CreateInstance(excelColumn.Adapter);
                    columnData = adapter.AdaptFormat(dataStr);
                }
                string typeErrorMessage = null;
                if (!this.TryGetAttribute(instance, property, columnData, header, rowResult, out typeErrorMessage))
                {
                    columnResult.ErrorMessages.Add(typeErrorMessage);
                }
                rowResult.ColumnResults.Add(columnResult);
            }
            rowResult.RowsValues = dataObjRaw;
            Results.RowResults.Add(rowResult);
            dataObj = (T)instance;
            return(!rowResult.HasError);
        }