Skip to content

Commit b76adb8

Browse files
committed
Improve worksheet handling in ExcelService
Updated the file header in `ExcelService.cs` to include an MIT license statement. Enhanced the logic for handling missing worksheets by selecting the first worksheet as a fallback if the specified worksheet is not found. Added error handling for cases where the workbook contains no worksheets, improving code robustness.
1 parent 522fd29 commit b76adb8

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

src/Infrastructure/Services/ExcelService.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Data;
@@ -97,8 +97,11 @@ public async Task<IResult<IEnumerable<TEntity>>> ImportAsync<TEntity>(
9797
using var workbook = new XLWorkbook(new MemoryStream(data));
9898
if (!workbook.Worksheets.TryGetWorksheet(sheetName, out var ws))
9999
{
100-
var msg = $"Sheet with name {sheetName} does not exist!";
101-
return await Result<IEnumerable<TEntity>>.FailureAsync(msg);
100+
ws = workbook.Worksheets.Count > 0 ? workbook.Worksheets.First() : null;
101+
if (ws is null)
102+
{
103+
return await Result<IEnumerable<TEntity>>.FailureAsync("Workbook contains no worksheets.");
104+
}
102105
}
103106

104107
// Check if the worksheet contains any cells.

0 commit comments

Comments
 (0)