Sfoglia il codice sorgente

Better and futureproof

Piotr Czajkowski 5 mesi fa
parent
commit
fb68437a5f

+ 3 - 11
ExcelORM/ExcelORM/ExcelWriter.cs

@@ -30,8 +30,6 @@ public class ExcelWriter
         return ++rowIndex;
     }
 
-    private static Type formulaType = typeof(Formula);
-
     private static void Write<T>(IEnumerable<T> values, IXLWorksheet worksheet, bool append) where T : class
     {
         if (!values.Any()) return;
@@ -54,16 +52,10 @@ public class ExcelWriter
 
                 var valueToSet = property.GetValue(value);
                 if (valueToSet == null) continue;
-
-                if (property.PropertyType == formulaType)
+                
+                if (valueToSet is ISpecialProperty specialProperty)
                 {
-                    var formulaA1Property = formulaType.GetProperty(nameof(Formula.FormulaA1));
-                    if (formulaA1Property != null)
-                    {
-                        if (formulaA1Property.GetValue(valueToSet) is string formulaA1)
-                            worksheet.Cell(rowIndex, cellIndex).FormulaA1 = formulaA1;
-                    }
-
+                    specialProperty.SetCellValue(worksheet.Cell(rowIndex, cellIndex));
                     continue;
                 }
 

+ 10 - 0
ExcelORM/ExcelORM/Interfaces/ISpecialProperty.cs

@@ -0,0 +1,10 @@
+using ClosedXML.Excel;
+
+namespace ExcelORM.Interfaces
+{
+    internal interface ISpecialProperty
+    {
+        public object? Value { get; set; }
+        public void SetCellValue(IXLCell cell);
+    }
+}

+ 6 - 2
ExcelORM/ExcelORM/Models/Formula.cs

@@ -1,8 +1,12 @@
-namespace ExcelORM.Models
+using ClosedXML.Excel;
+using ExcelORM.Interfaces;
+
+namespace ExcelORM.Models
 {
-    public class Formula
+    public class Formula : ISpecialProperty
     {
         public object? Value { get; set; }
         public string? FormulaA1 { get; set; }
+        public void SetCellValue(IXLCell cell) => cell.FormulaA1 = FormulaA1;
     }
 }