Piotr Czajkowski 1 жил өмнө
parent
commit
fd79b73a68

+ 1 - 3
ExcelORM/ExcelORM/ExcelReader.cs

@@ -34,11 +34,9 @@ public class ExcelReader
 
                 switch (property.PropertyType)
                 {
-                    case Type _ when property.PropertyType == typeof(string):
+                    case not null when property.PropertyType == typeof(string):
                         property.SetValue(current, cell.Value.ToString());
                         break;
-                    default:
-                        break;
                 }
             }
 

+ 9 - 11
ExcelORM/ExcelORM/ExcelWriter.cs

@@ -9,13 +9,10 @@ public class ExcelWriter
 
     public ExcelWriter(string? path = null)
     {
-        if (File.Exists(path))
-            xlWorkbook = new XLWorkbook(path);
-        else
-            xlWorkbook = new XLWorkbook();
+        xlWorkbook = File.Exists(path) ? new XLWorkbook(path) : new XLWorkbook();
     }
 
-    private uint GenerateHeader<T>(T value, IXLWorksheet worksheet, uint rowIndex = 1) where T : class, new()
+    private static uint GenerateHeader<T>(T value, IXLWorksheet worksheet, uint rowIndex = 1) where T : class, new()
     {
         var cellIndex = 1;
         var properties = value.GetType().GetProperties();
@@ -38,15 +35,16 @@ public class ExcelWriter
         Write(values, xlWorksheet, append, rowIndex);
     }
 
-    public void Write<T>(IEnumerable<T> values, IXLWorksheet worksheet, bool append = false, uint rowIndex = 1) where T : class, new()
+    private void Write<T>(IEnumerable<T> values, IXLWorksheet worksheet, bool append = false, uint rowIndex = 1) where T : class, new()
     {
         if (!values.Any()) return;
 
-        if (append)
-            rowIndex = (uint)worksheet.LastRowUsed().RowNumber() + 1;
-
-        if (!append && WriteHeader)
-            rowIndex = GenerateHeader(values.First(), worksheet);
+        rowIndex = append switch
+        {
+            true => (uint)worksheet.LastRowUsed().RowNumber() + 1,
+            false when WriteHeader => GenerateHeader(values.First(), worksheet),
+            _ => rowIndex
+        };
 
         foreach (var value in values)
         {

+ 3 - 0
ExcelORM/ExcelORMTests/ExcelORMTests.csproj

@@ -33,6 +33,9 @@
       <None Update="testFiles\first.xlsx">
         <CopyToOutputDirectory>Always</CopyToOutputDirectory>
       </None>
+      <None Update="testFiles\filtered.xlsx">
+        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+      </None>
     </ItemGroup>
 
     <ItemGroup>

+ 18 - 2
ExcelORM/ExcelORMTests/ReaderWriterTests.cs

@@ -6,8 +6,9 @@ public class ReaderWriterTests
 {
     private const string regularFile = "testFiles/first.xlsx";
     private const string hiddenFile = "testFiles/hidden.xlsx";
-    
-    class Test
+    private const string filteredFile = "testFiles/filtered.xlsx";
+
+    private class Test
     {
         [Column("First name" )]
         public string? Name { get; set; }
@@ -42,4 +43,19 @@ public class ReaderWriterTests
         Assert.NotEmpty(resultsHidden);
         Assert.NotEqual(results.Count(), resultsHidden.Count());
     }
+    
+    [Fact]
+    public void ReadFiltered()
+    {
+        var reader = new ExcelReader(filteredFile);
+        var results = reader.Read<Test>();
+        Assert.NotNull(results);
+        Assert.NotEmpty(results);
+
+        var readerFiltered = new ExcelReader(filteredFile) { ObeyFilter = true };
+        var resultsFiltered = readerFiltered.Read<Test>();
+        Assert.NotNull(resultsFiltered);
+        Assert.NotEmpty(resultsFiltered);
+        Assert.NotEqual(results.Count(), resultsFiltered.Count());
+    }
 }

BIN
ExcelORM/ExcelORMTests/testFiles/filtered.xlsx