diff --git a/src/CellSharp/Internal/XlsxStream.cs b/src/CellSharp/Internal/XlsxStream.cs index 53ebd55..ac39b78 100644 --- a/src/CellSharp/Internal/XlsxStream.cs +++ b/src/CellSharp/Internal/XlsxStream.cs @@ -25,17 +25,8 @@ internal static SpreadsheetDocument Open(Stream stream, ExcelReadOptions? option internal static SpreadsheetDocument Open(string path, ExcelReadOptions? options = null) { var readOptions = options ?? ExcelReadOptions.Default; - var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); - try - { - ValidatePackageLength(stream.Length, readOptions.MaxPackageBytes); - return SpreadsheetDocument.Open(stream, false, OpenSettings(readOptions)); - } - catch - { - stream.Dispose(); - throw; - } + ValidatePackageLength(new FileInfo(path).Length, readOptions.MaxPackageBytes); + return SpreadsheetDocument.Open(path, false, OpenSettings(readOptions)); } internal static void CompleteWrite(Stream stream) => stream.Position = stream.Length; diff --git a/tests/CellSharp.Tests/ExcelReadTests.cs b/tests/CellSharp.Tests/ExcelReadTests.cs index c2a28a8..a7b91da 100644 --- a/tests/CellSharp.Tests/ExcelReadTests.cs +++ b/tests/CellSharp.Tests/ExcelReadTests.cs @@ -44,6 +44,26 @@ public void ReadRoundTripsWrittenRows() } } + [Fact] + public void ReadReleasesTheSourceFile() + { + var path = TemporaryPath(); + + try + { + Excel.Write(path, [new SimpleCustomer { Id = 1, Name = "Ada", Active = true }]); + + var result = Excel.Read(path); + + Assert.True(result.IsValid); + using var exclusiveAccess = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None); + } + finally + { + Delete(path); + } + } + [Fact] public void ReadMapsColumnsInAnyOrder() {