|
@@ -1,4 +1,5 @@
|
|
|
-using ClosedXML.Excel;
|
|
|
|
|
|
|
+using System.Reflection;
|
|
|
|
|
+using ClosedXML.Excel;
|
|
|
using ExcelORM.Attributes;
|
|
using ExcelORM.Attributes;
|
|
|
|
|
|
|
|
namespace ExcelORM
|
|
namespace ExcelORM
|
|
@@ -8,7 +9,29 @@ namespace ExcelORM
|
|
|
public string? PropertyName { get; set; }
|
|
public string? PropertyName { get; set; }
|
|
|
public int? Position { get; set; }
|
|
public int? Position { get; set; }
|
|
|
|
|
|
|
|
- public static List<Mapping>? MapProperties<T>(IXLCells? headerCells) where T : class
|
|
|
|
|
|
|
+ private static int? GetColumnPosition(IXLCells headerCells, PropertyInfo property, bool fallbackToPropertyName)
|
|
|
|
|
+ {
|
|
|
|
|
+ int? position = null;
|
|
|
|
|
+ var columnAttributeObject = property.GetCustomAttributes(typeof(ColumnAttribute), false).FirstOrDefault();
|
|
|
|
|
+ if (columnAttributeObject != null)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (columnAttributeObject is ColumnAttribute { Names.Length: > 0 } columnAttribute)
|
|
|
|
|
+ {
|
|
|
|
|
+ position = headerCells.FirstOrDefault(x => !x.Value.IsBlank && Array.Exists(columnAttribute.Names,
|
|
|
|
|
+ y => y.Equals(x.Value.ToString(), StringComparison.InvariantCultureIgnoreCase)))?.Address
|
|
|
|
|
+ .ColumnNumber;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (position != null || !fallbackToPropertyName) return position;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return headerCells.FirstOrDefault(x =>
|
|
|
|
|
+ !x.Value.IsBlank &&
|
|
|
|
|
+ property.Name.Equals(x.Value.ToString(), StringComparison.InvariantCultureIgnoreCase))?.Address
|
|
|
|
|
+ .ColumnNumber;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static List<Mapping>? MapProperties<T>(IXLCells? headerCells, bool fallbackToPropertyName = false) where T : class
|
|
|
{
|
|
{
|
|
|
if (headerCells == null || !headerCells.Any()) return null;
|
|
if (headerCells == null || !headerCells.Any()) return null;
|
|
|
|
|
|
|
@@ -18,15 +41,9 @@ namespace ExcelORM
|
|
|
{
|
|
{
|
|
|
if (property.Skip()) continue;
|
|
if (property.Skip()) continue;
|
|
|
|
|
|
|
|
- var position = property.GetCustomAttributes(typeof(ColumnAttribute), false).FirstOrDefault() switch
|
|
|
|
|
- {
|
|
|
|
|
- ColumnAttribute { Names.Length: > 0 } attribute => headerCells.FirstOrDefault(x => !x.Value.IsBlank && Array.Exists(attribute.Names,
|
|
|
|
|
- y => y.Equals(x.Value.ToString(), StringComparison.InvariantCultureIgnoreCase)))?.Address
|
|
|
|
|
- .ColumnNumber,
|
|
|
|
|
- _ => headerCells.FirstOrDefault(x => !x.Value.IsBlank && property.Name.Equals(x.Value.ToString(), StringComparison.InvariantCultureIgnoreCase))?.Address.ColumnNumber
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
|
|
+ var position = GetColumnPosition(headerCells, property, fallbackToPropertyName);
|
|
|
if (position == null) continue;
|
|
if (position == null) continue;
|
|
|
|
|
+
|
|
|
map.Add(new Mapping { PropertyName = property.Name, Position = position });
|
|
map.Add(new Mapping { PropertyName = property.Name, Position = position });
|
|
|
}
|
|
}
|
|
|
|
|
|