Răsfoiți Sursa

Added WriteWithAppendExistingHeaderFirst

Piotr Czajkowski 1 lună în urmă
părinte
comite
ca7d40d323

+ 2 - 2
ExcelORM/ExcelORM/ExcelWriter.cs

@@ -105,7 +105,7 @@ public class ExcelWriter : IDisposable
         }
     }
 
-    public void Write<T>(IEnumerable<T> values, string? worksheetName = null, bool append = false, uint? headerRowIndex = null) where T : class
+    public void Write<T>(IEnumerable<T> values, string? worksheetName = null, bool append = false, uint? headerRowIndex = null, uint? appendFrom = null) where T : class
     {
         var xlWorksheet = xlWorkbook.Worksheets.FirstOrDefault(x => x.Name.Equals(worksheetName, StringComparison.InvariantCultureIgnoreCase));
         
@@ -113,7 +113,7 @@ public class ExcelWriter : IDisposable
             xlWorkbook.AddWorksheet(worksheetName)
             : xlWorkbook.Worksheets.Count == 0 ? xlWorkbook.AddWorksheet() : xlWorkbook.Worksheets.First();
 
-        Write(values, xlWorksheet, append, headerRowIndex);
+        Write(values, xlWorksheet, append, headerRowIndex, appendFrom);
     }
 
     public void SaveAs(string path, IExcelConverter? converter = null)

+ 3 - 0
ExcelORM/ExcelORMTests/ExcelORMTests.csproj

@@ -56,6 +56,9 @@
       <None Update="testFiles\badDate.xlsx">
         <CopyToOutputDirectory>Always</CopyToOutputDirectory>
       </None>
+      <None Update="testFiles\forAppendWithRubbish.xlsx">
+        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+      </None>
     </ItemGroup>
 
     <ItemGroup>

+ 24 - 0
ExcelORM/ExcelORMTests/WriterTests.cs

@@ -90,6 +90,30 @@ public class WriterTests
         File.Delete(testFile);
     }
 
+    private const string ForAppendWithRubbish = "testFiles/forAppendWithRubbish.xlsx";
+    
+    [Fact]
+    public void WriteWithAppendExistingAndRubbish()
+    {
+        var testFile = Path.GetRandomFileName();
+        testFile = Path.ChangeExtension(testFile, "xlsx");
+        File.Copy(ForAppendWithRubbish, testFile);
+
+        uint headerRowIndex = 3;
+        using var writer = new ExcelWriter(testFile);
+        writer.Write(arrayOfThree, append: true, headerRowIndex: headerRowIndex, appendFrom: 7);
+        writer.SaveAs(testFile);
+
+        using var reader = new ExcelReader(testFile);
+        var readArray = reader.Read<Test>(startFrom: headerRowIndex).ToArray();
+        Assert.Equal(6, readArray.Length);
+
+        for (int i = 0; i < arrayOfThree.Length; i++)
+            Assert.Equal(arrayOfThree[i], readArray[i+3]);
+
+        File.Delete(testFile);
+    }
+    
     private const string ForAppendHeaderFirst = "testFiles/forAppendHeaderFirst.xlsx";
 
     [Fact]

BIN
ExcelORM/ExcelORMTests/testFiles/forAppendWithRubbish.xlsx