Exemplo n.º 1
0
        private void SetEntityByColumn(TCell cell, Column <TEntity, TCell> column, ImportResultItem <TEntity> resultItem,
                                       int index, TEntity entity)
        {
            if (string.IsNullOrWhiteSpace(cell?.ToString()))
            {
                //为空的时候。

                if (column.IsRequire)
                {
                    var errorMessage = BuildErrorMessageByTemplate(column.GetErrorMessageIfEmpty(), column);
                    resultItem.AddError(index, errorMessage);
                }
                else
                {
                    column.SetEmptyOrNull(entity);
                }
            }
            else
            {
                if (!column.TrySetValue(entity, cell, out var errorMessage))
                {
                    errorMessage = BuildErrorMessageByTemplate(errorMessage, column);
                    resultItem.AddError(index, errorMessage);
                }
            }
        }
Exemplo n.º 2
0
        private IList <ImportResultItem <TEntity> > ImportResultItems()
        {
            var result = new List <ImportResultItem <TEntity> >();

            // read titles.
            _fileReader.TryReadInString(TitleRowIndex, out var titleEnum);
            var titles   = titleEnum.ToList();
            var rowIndex = TitleRowIndex + 1;


            while (TryGetRows(rowIndex, out var cells))
            {
                var resultItem = new ImportResultItem <TEntity> {
                    Row = rowIndex
                };
                var entity = resultItem.Data = Create();
                result.Add(resultItem);


                var otherIndex = _columns.Count;

                for (var titleIndex = 0; titleIndex < titles.Count(); titleIndex++)
                {
                    if (titleIndex >= otherIndex) // 带有扩展列
                    {
                        if (OtherColumn == null)
                        {
                            break;
                        }

                        OtherColumn.Set(entity, cells[titleIndex], titles[titleIndex]);
                        continue;
                    }


                    var column = _columns[titleIndex];
                    //看一下当前行。
                    if (titleIndex < cells.Count)
                    {
                        var cell = cells[titleIndex];
                        SetEntityByColumn(cell, column, resultItem, titleIndex, entity);
                    }
                    else
                    {
                        column.SetEmptyOrNull(entity);
                    }
                }

                rowIndex++;
            }

            return(result);
        }