Пример #1
0
 private bool RowIsEmpty(IRow row, int cellCount)
 {
     try
     {
         for (int cellIndex = 0; cellIndex < cellCount; cellIndex++)
         {
             if (!string.IsNullOrWhiteSpace(CellValueHelper.GetCellValue(row.GetCell(cellIndex))))
             {
                 return(false);
             }
         }
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Пример #2
0
        private List <ImportColumnProperty> GetCellProperties(Type modelType, IRow headerRow)
        {
            var properties       = modelType.GetProperties();
            var columnProperties = new List <ImportColumnProperty>();

            foreach (var p in properties)
            {
                if (p.GetCustomAttribute(typeof(ColumnNameAttribute)) is ColumnNameAttribute attr)
                {
                    columnProperties.Add(new ImportColumnProperty(p, attr));
                }
            }

            for (int i = headerRow.FirstCellNum; i < headerRow.LastCellNum; i++)
            {
                var cell = headerRow.GetCell(i);
                if (cell == null)
                {
                    continue;
                }

                var headColumnName = CellValueHelper.GetCellValue(cell);
                if (string.IsNullOrWhiteSpace(headColumnName))
                {
                    continue;
                }

                headColumnName = headColumnName.Trim();
                var columnProperty = columnProperties.FirstOrDefault(x => x.Name == headColumnName.Trim());
                if (columnProperty != null)
                {
                    columnProperty.ColumnIndex = i;
                }
            }

            // 没有字段设置唯一认证,则给所有字段加上唯一认证
            if (!columnProperties.Any(m => m.IsUnique))
            {
                columnProperties.ForEach(m => m.IsUnique = true);
            }

            return(columnProperties);
        }
Пример #3
0
        private void FillPropertyValue(SheetRow rowModel, ImportColumnProperty columnProperty, ICell cell)
        {
            var cellValue = cell == null ? null : CellValueHelper.GetCellValue(cell);

            if (string.IsNullOrWhiteSpace(cellValue))
            {
                if (columnProperty.IsRequired || (!columnProperty.PropertyInfo.IsNullable() && columnProperty.PropertyInfo.PropertyType != typeof(string)))
                {
                    rowModel.SetError(columnProperty.PropertyInfo.Name, columnProperty.EmptyErrorMessage);
                }
            }
            else
            {
                if (columnProperty.HasRegex && !Regex.IsMatch(cellValue, columnProperty.RegexPattern, columnProperty.RegexOptions))
                {
                    rowModel.SetError(columnProperty.PropertyInfo.Name, columnProperty.RegexErrorMessage);
                    return;
                }

                var targetType = columnProperty.PropertyInfo.IsNullable() ? columnProperty.PropertyInfo.PropertyType.GetGenericArguments()[0] : columnProperty.PropertyInfo.PropertyType;
                columnProperty.PropertyInfo.SetValue(rowModel, this.ConvertValueType(cellValue, targetType));
            }
        }