public static List <IFilter> CreateFilters <TTemplate>(ExcelHeaderRow headerRow)
        {
            Type templateType = typeof(TTemplate);
            var  key          = templateType;

            if (Table[key] != null)
            {
                return((List <IFilter>)Table[key]);
            }

            List <IFilter>             filters        = new List <IFilter>();
            List <BaseFilterAttribute> attrs          = new List <BaseFilterAttribute>();
            TypeFilterInfo             typeFilterInfo = TypeFilterInfoFlyweight.CreateInstance(typeof(TTemplate), headerRow);

            typeFilterInfo.PropertyFilterInfos.ForEach(a => a.FilterAttrs.ForEach(f => attrs.Add(f)));

            attrs.Distinct(new FilterAttributeComparer()).ToList().ForEach
                (a =>
            {
                var filter = FilterFactory.CreateInstance(a.GetType());
                if (filter != null)
                {
                    filters.Add(filter);
                }
            });

            Table[key] = filters;
            return(filters);
        }
Beispiel #2
0
        /// <summary>
        /// 将Sheet转化为ExcelDataRow集合
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="startDataRowIndex"></param>
        /// <returns></returns>
        public static List <ExcelDataRow> Convert <TTemplate>(ISheet sheet, ExcelHeaderRow headerRow, int startDataRowIndex)
        {
            List <ExcelDataRow> dataRows = new List <ExcelDataRow>();

            for (int i = startDataRowIndex; i < sheet.PhysicalNumberOfRows; i++)
            {
                dataRows.Add(Convert <TTemplate>(sheet.GetRow(i), headerRow));
            }

            return(dataRows);
        }
Beispiel #3
0
        /// <summary>
        /// 将IRow转换为ExcelDataRow
        /// </summary>
        /// <typeparam name="TTemplate"></typeparam>
        /// <param name="row"></param>
        /// <param name="headerRow"></param>
        /// <returns></returns>
        public static ExcelDataRow Convert <TTemplate>(IRow row, ExcelHeaderRow headerRow)
        {
            Type type = typeof(TTemplate);

            ExcelDataRow dataRow = new ExcelDataRow()
            {
                DataCols = new List <ExcelDataCol>(),
                ErrorMsg = string.Empty,
                IsValid  = true,
                RowIndex = row.RowNum
            };

            ICell        cell;
            ExcelDataCol dataCol;
            string       colName;
            string       propertyName;
            string       key;

            for (int i = 0; i < headerRow.Cells.Count; i++)
            {
                colName = headerRow?.Cells?.SingleOrDefault(h => h.ColIndex == i)?.ColName;

                if (colName == null)
                {
                    continue;
                }

                key = $"{type.FullName}_{i}";

                if (Table[key] == null)
                {
                    propertyName = type.GetProperties().ToList().FirstOrDefault(p => p.IsDefined(typeof(ColNameAttribute), false) &&
                                                                                p.GetCustomAttribute <ColNameAttribute>()?.ColName == colName
                                                                                )?.Name;
                    Table[key] = propertyName;
                }

                dataCol = new ExcelDataCol()
                {
                    ColIndex     = i,
                    ColName      = colName,
                    PropertyName = Table[key]?.ToString(),
                    RowIndex     = row.RowNum,
                    ColValue     = row.GetCell(i) == null ? string.Empty : row.GetCell(i).GetStringValue()
                };

                dataRow.DataCols.Add(dataCol);
            }

            return(dataRow);
        }
Beispiel #4
0
        public static TypeFilterInfo CreateInstance(Type importType, ExcelHeaderRow excelHeaderRow)
        {
            if (importType == null)
            {
                throw new ArgumentNullException("importDTOType");
            }

            if (excelHeaderRow == null)
            {
                throw new ArgumentNullException("excelHeaderRow");
            }

            var key = importType;

            if (Table[key] != null)
            {
                return((TypeFilterInfo)Table[key]);
            }

            TypeFilterInfo typeFilterInfo = new TypeFilterInfo()
            {
                PropertyFilterInfos = new List <PropertyFilterInfo>()
                {
                }
            };

            IEnumerable <PropertyInfo> props = importType.GetProperties().ToList().Where(p => p.IsDefined(typeof(ColNameAttribute)));

            props.ToList().ForEach(p =>
            {
                string colName = p.GetCustomAttribute <ColNameAttribute>().ColName;
                ExcelCol col   = excelHeaderRow.Cells.SingleOrDefault(c => c.ColName == colName);
                if (col != null)
                {
                    typeFilterInfo.PropertyFilterInfos.Add(
                        new PropertyFilterInfo
                    {
                        ColIndex    = col.ColIndex,
                        FilterAttrs = p.GetCustomAttributes <BaseFilterAttribute>()?.ToList()
                    });
                }
            });

            Table[key] = typeFilterInfo;

            return(typeFilterInfo);
        }
        /// <summary>
        /// 设置表头字典,列索引:列名
        /// </summary>
        /// <returns></returns>
        private static void SetDictHeader()
        {
            HeaderRow = new ExcelHeaderRow();
            IRow  row = Sheet.GetRow(0);
            ICell cell;

            for (int i = 0; i < row.PhysicalNumberOfCells; i++)
            {
                cell = row.GetCell(i);
                HeaderRow.Cells.Add(
                    new ExcelCol()
                {
                    ColIndex = i,
                    ColName  = cell.GetStringValue()
                });
            }
        }