/// <summary>
        /// 导入
        /// </summary>
        /// <typeparam name="T">模板类</typeparam>
        /// <param name="fileUrl">Excel文件绝对地址</param>
        /// <param name="delegateNotExistInDatabase">数据库校验委托,标记了数据库重复校验特性则必填</param>
        /// <returns></returns>
        public static List <ExcelDataRow> Validate <T>(string fileUrl, Func <DatabaseFilterContext, bool> delegateNotExistInDatabase = null)
            where T : class, new()
        {
            Init(fileUrl);

            List <ExcelDataRow> rows      = ExcelConverter.Convert <T>(Sheet, HeaderRow, 1);
            AndFilter           andFilter = new AndFilter()
            {
                filters = FiltersFlyWeight.CreateFilters <T>(HeaderRow)
            };

            FilterContext context = new FilterContext()
            {
                DelegateNotExistInDatabase = delegateNotExistInDatabase,
                TypeFilterInfo             = TypeFilterInfoFlyweight.CreateInstance(typeof(T), HeaderRow)
            };

            rows = andFilter.Filter(rows, context);

            return(rows);
        }
        public List <ExcelDataRow> Filter(List <ExcelDataRow> excelDataRows, FilterContext context)
        {
            List <KeyValuePair <int, string> > kvps = new List <KeyValuePair <int, string> >();

            excelDataRows.ToList().ForEach(r => r.DataCols.ForEach(c =>
            {
                KeyValuePair <int, string> kvp = new KeyValuePair <int, string>(c.ColIndex, c.ColValue);
                if (r.IsValid)
                {
                    var attr = c.GetFilterAttr <DuplicationAttribute>(context.TypeFilterInfo);
                    if (attr != null)
                    {
                        r.SetState(!kvps.Contains(kvp), c, attr.ErrorMsg);
                    }
                }

                kvps.Add(kvp);
            }));

            return(excelDataRows);
        }
Ejemplo n.º 3
0
        public List <ExcelDataRow> Filter(List <ExcelDataRow> excelDataRows, FilterContext context)
        {
            if (context.DelegateNotExistInDatabase == null)
            {
                throw new ArgumentNullException("please set delegate of database filter first");
            }

            excelDataRows.Where(r => r.IsValid).ToList().ForEach(r => r.DataCols.ForEach(c =>
            {
                var attr = c.GetFilterAttr <DatabaseExistAttribute>(context.TypeFilterInfo);
                if (attr != null)
                {
                    r.SetState(context.DelegateNotExistInDatabase(
                                   new DatabaseFilterContext()
                    {
                        TableName = attr.TableName,
                        FieldName = attr.FieldName,
                        Value     = c.ColValue
                    }), c, attr.ErrorMsg);
                }
            }));

            return(excelDataRows);
        }