Bläddra i källkod

Added FallbackToPropertyName

Piotr Czajkowski 1 vecka sedan
förälder
incheckning
ea601d70b5
2 ändrade filer med 6 tillägg och 3 borttagningar
  1. 2 1
      ExcelORM/ExcelORM/ExcelReader.cs
  2. 4 2
      ExcelORM/ExcelORM/ExcelWriter.cs

+ 2 - 1
ExcelORM/ExcelORM/ExcelReader.cs

@@ -9,6 +9,7 @@ public sealed class ExcelReader : IDisposable
     private readonly IXLWorkbook xlWorkbook;
     public bool SkipHidden { get; set; }
     public bool ObeyFilter { get; set; }
+    public bool FallbackToPropertyName { get; set; }
 
     public ExcelReader(IXLWorkbook? workbook)
     {
@@ -71,7 +72,7 @@ public sealed class ExcelReader : IDisposable
         
         if (firstRow == null || firstRow.IsEmpty()) yield break;
 
-        var mapping = Mapping.MapProperties<T>(firstRow.CellsUsed());
+        var mapping = Mapping.MapProperties<T>(firstRow.CellsUsed(), FallbackToPropertyName);
         if (mapping == null) yield break;
 
         var rowsToProcess = (ObeyFilter && worksheet.AutoFilter.IsEnabled) switch

+ 4 - 2
ExcelORM/ExcelORM/ExcelWriter.cs

@@ -9,6 +9,8 @@ namespace ExcelORM;
 public sealed class ExcelWriter : IDisposable
 {
     private readonly IXLWorkbook xlWorkbook;
+    public bool FallbackToPropertyName { get; set; }
+    
     public ExcelWriter(string? path = null)
     {
         xlWorkbook = File.Exists(path) ? new XLWorkbook(path) : new XLWorkbook();
@@ -77,7 +79,7 @@ public sealed class ExcelWriter : IDisposable
         }
     }
 
-    private static void Write<T>(IEnumerable<T> values, IXLWorksheet worksheet, bool append, uint? headerRowIndex = null, uint? startFrom = null) where T : class
+    private void Write<T>(IEnumerable<T> values, IXLWorksheet worksheet, bool append, uint? headerRowIndex = null, uint? startFrom = null) where T : class
     {
         var properties = typeof(T).GetProperties();
         List<Mapping>? mapping = [];
@@ -99,7 +101,7 @@ public sealed class ExcelWriter : IDisposable
         if (append)
         {
             var headerCells = headerRowIndex != null ? worksheet.Row((int)headerRowIndex).CellsUsed() : worksheet.FirstRowUsed()?.CellsUsed();
-            mapping = Mapping.MapProperties<T>(headerCells);
+            mapping = Mapping.MapProperties<T>(headerCells, FallbackToPropertyName);
             if (mapping == null || mapping.Count == 0) return;
         }