Explorar o código

Added WriteWithFormula

Piotr Czajkowski hai 6 meses
pai
achega
52c358386b

+ 16 - 0
ExcelORM/ExcelORM/ExcelWriter.cs

@@ -1,5 +1,6 @@
 using ClosedXML.Excel;
 using ExcelORM.Attributes;
+using ExcelORM.Models;
 
 namespace ExcelORM;
 
@@ -28,6 +29,8 @@ 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, new()
     {
         if (!values.Any()) return;
@@ -47,9 +50,22 @@ public class ExcelWriter
                 if (property.Skip()) continue;
 
                 cellIndex++;
+
                 var valueToSet = property.GetValue(value);
                 if (valueToSet == null) continue;
 
+                if (property.PropertyType == formulaType)
+                {
+                    var formulaA1Property = formulaType.GetProperty(nameof(Formula.FormulaA1));
+                    if (formulaA1Property != null)
+                    {
+                        if (formulaA1Property.GetValue(valueToSet) is string formulaA1)
+                            worksheet.Cell(rowIndex, cellIndex).FormulaA1 = formulaA1;
+                    }
+
+                    continue;
+                }
+
                 worksheet.Cell(rowIndex, cellIndex).Value = XLCellValue.FromObject(valueToSet);
             }
 

+ 3 - 3
ExcelORM/ExcelORMTests/ExcelORMTests.csproj

@@ -10,9 +10,9 @@
     </PropertyGroup>
 
     <ItemGroup>
-        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
-        <PackageReference Include="xunit" Version="2.8.0" />
-        <PackageReference Include="xunit.runner.visualstudio" Version="2.8.0">
+        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
+        <PackageReference Include="xunit" Version="2.8.1" />
+        <PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
             <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
             <PrivateAssets>all</PrivateAssets>
         </PackageReference>

+ 32 - 1
ExcelORM/ExcelORMTests/WriterTests.cs

@@ -1,4 +1,5 @@
 using ExcelORM;
+using ExcelORM.Models;
 
 namespace ExcelORMTests;
 
@@ -130,7 +131,7 @@ public class WriterTests
         File.Delete(testFile);
     }
 
-    private readonly TestSkipMiddle[] arrayWithSkipMiddle =
+    private static readonly TestSkipMiddle[] arrayWithSkipMiddle =
     {
         new() {Text = "Lorem", Date = DateTime.Now.AddHours(1), Int = 1},
         new() {Text = "Ipsum", Date = DateTime.Now.AddHours(2), Int = 2},
@@ -160,4 +161,34 @@ public class WriterTests
 
         File.Delete(testFile);
     }
+
+    private static readonly TestWithFormula[] arrayWithFormulas =
+    {
+        new() { Name = "Bilbo", Surname = "Baggins", Job = "Eater", FullName = new Formula{ FormulaA1 = "B2&C2" } },
+        new() { Name = "John", Job = "Policeman", FullName = new Formula{ FormulaA1 = "B3&C3" } },
+        new() { Name = "Bruce", Surname = "Lee", Job = "Fighter", FullName = new Formula{ FormulaA1 = "B4&C4" } },
+    };
+
+    [Fact]
+    public void WriteWithFormula()
+    {
+        var testFile = Path.GetRandomFileName();
+        testFile = Path.ChangeExtension(testFile, "xlsx");
+
+        var writer = new ExcelWriter(testFile);
+        writer.Write(arrayWithFormulas);
+        writer.SaveAs(testFile);
+
+        var reader = new ExcelReader(testFile);
+        var readArray = reader.Read<TestWithFormula>().ToArray();
+        Assert.Equal(arrayWithFormulas.Length, readArray.Length);
+
+        foreach (var item in readArray)
+        {
+            Assert.NotNull(item.FullName);
+            Assert.Equal($"{item.Name}{item.Surname}", item.FullName.Value);
+        }
+        
+        File.Delete(testFile);
+    }
 }