Example #1
0
        private static void LoadRowData(IEnumerable <ColumnInfo> columns, IRow row, object target, IRowInfo rowInfo)
        {
            var    errorIndex   = -1;
            string errorMessage = null;

            void ColumnFailed(IColumnInfo column, string message)
            {
                if (errorIndex >= 0)
                {
                    return;                  // Ensures the first error will not be overwritten.
                }
                if (column.Attribute.IgnoreErrors == true)
                {
                    return;
                }
                errorIndex   = column.Attribute.Index;
                errorMessage = message;
            }

            foreach (var column in columns)
            {
                var index = column.Attribute.Index;
                if (index < 0)
                {
                    continue;
                }

                try
                {
                    var cell         = row.GetCell(index);
                    var propertyType = column.Attribute.PropertyUnderlyingType ?? column.Attribute.Property?.PropertyType;

                    if (!MapHelper.TryGetCellValue(cell, propertyType, out object valueObj))
                    {
                        ColumnFailed(column, "CellType is not supported yet!");
                        continue;
                    }

                    valueObj = column.RefreshAndGetValue(valueObj);

                    if (column.Attribute.TryTake != null)
                    {
                        if (!column.Attribute.TryTake(column, target))
                        {
                            ColumnFailed(column, "Returned failure by custom cell resolver!");
                        }
                    }
                    else if (propertyType != null)
                    {
                        // Change types between IConvertible objects, such as double, float, int and etc.
                        if (MapHelper.TryConvertType(valueObj, column, out object result))
                        {
                            column.Attribute.Property.SetValue(target, result, null);
                        }
                        else
                        {
                            ColumnFailed(column, "Cannot convert value to the property type!");
                        }
                        //var value = Convert.ChangeType(valueObj, column.Attribute.PropertyUnderlyingType ?? propertyType);
                    }
                }
                catch (Exception e)
                {
                    ColumnFailed(column, e.Message);
                }
            }

            rowInfo.ErrorColumnIndex = errorIndex;
            rowInfo.ErrorMessage     = errorMessage;
        }
Example #2
0
        private static void LoadRowData(IEnumerable <ColumnInfo> columns, IRow row, object target, IRowInfo rowInfo)
        {
            var errorIndex   = -1;
            var errorMessage = string.Empty;

            foreach (var column in columns)
            {
                var index = column.Attribute.Index;
                if (index < 0)
                {
                    continue;
                }

                try
                {
                    var    cell         = row.GetCell(index);
                    var    propertyType = column.Attribute.PropertyUnderlyingType ?? column.Attribute.Property?.PropertyType;
                    object valueObj;

                    if (!MapHelper.TryGetCellValue(cell, propertyType, out valueObj))
                    {
                        errorIndex   = index;
                        errorMessage = "CellType is not supported yet!";
                        break;
                    }

                    valueObj = column.RefreshAndGetValue(valueObj);

                    if (column.Attribute.TryTake != null)
                    {
                        if (!column.Attribute.TryTake(column, target))
                        {
                            errorIndex   = index;
                            errorMessage = "Returned failure by custom cell resolver!";
                            break;
                        }
                    }
                    else if (propertyType != null && valueObj != null)
                    {
                        // Change types between IConvertible objects, such as double, float, int and etc.
                        var value = MapHelper.ConvertType(valueObj, column);
                        //var value = Convert.ChangeType(valueObj, column.Attribute.PropertyUnderlyingType ?? propertyType);
                        column.Attribute.Property.SetValue(target, value);
                    }
                }
                catch (Exception e)
                {
                    errorIndex   = index;
                    errorMessage = e.Message;
                    break;
                }
            }

            rowInfo.ErrorColumnIndex = errorIndex;
            rowInfo.ErrorMessage     = errorMessage;
        }