Beispiel #1
0
        public virtual CellsParseReult ParseCells(ExcelRowParseOnTypeRequest request, int[] excludeCells)
        {
            var result = new CellsParseReult();

            var  invalidCells = new List <int>();
            var  values       = new Dictionary <string, object>();
            var  row          = request.Row;
            var  sheet        = row.Sheet;
            Type parseOnType  = null;
            var  name         = request.Options.SheetName ?? sheet.Name;
            var  map          = request.Options.Map ?? row.Sheet.Header.Select(x => x.Value.ToString()).ToArray();
            var  strategy     = request.NamingStrategy;

            parseOnType = sheet.Index == 0 ? request.RootType : request.RootType.GetEnumerablePropertyType(name, strategy);

            for (var i = 0; i < row.Cells.Length; i++)
            {
                if (excludeCells.Contains(i))
                {
                    continue;
                }

                var    value        = row.Cells[i].Value;
                var    propertyName = map[i].Transform(strategy);
                object castValue;

                var propertyPath = PropertyPath.TryParse(parseOnType, propertyName);

                var convertSucceeded =
                    Converter.TryCreate(value, propertyPath.PropertyType, out castValue);

                if (convertSucceeded)
                {
                    if (castValue != null && castValue.GetType() == typeof(string) &&
                        (castValue as string) == string.Empty)
                    {
                        castValue = null;
                    }
                    values[propertyName] = castValue;
                }
                else
                {
                    invalidCells.Add(i);
                }
            }

            result.InvalidCells = invalidCells.ToArray();

            if (invalidCells.Count == 0)
            {
                result.RowMapped = JsonConvert.SerializeObject(parseOnType.CreateFromDictionary(values),
                                                               JsonUtil.GetSettings(request.NamingStrategy));
            }

            return(result);
        }
 public void Populate(CellsParseReult result)
 {
     RowAsData    = result.RowMapped;
     InvalidCells = result.InvalidCells;
 }