Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions src/CellSharp/Internal/XlsxStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions tests/CellSharp.Tests/ExcelReadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SimpleCustomer>(path);

Assert.True(result.IsValid);
using var exclusiveAccess = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
finally
{
Delete(path);
}
}

[Fact]
public void ReadMapsColumnsInAnyOrder()
{
Expand Down