Skip to content

Commit f6b6059

Browse files
Merge pull request #878
Fixed exception not being thrown when too long a sheet name was passed as dictionary key
2 parents 9785bae + 631a59c commit f6b6059

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

src/MiniExcel.Core/Helpers/ThrowHelper.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
internal static class ThrowHelper
44
{
5+
private static readonly byte[] ZipArchiveHeader = [0x50, 0x4B];
6+
private const int ExcelMaxSheetNameLength = 31;
7+
58
internal static void ThrowIfInvalidOpenXml(Stream stream)
69
{
710
var probe = new byte[8];
@@ -13,7 +16,16 @@ internal static void ThrowIfInvalidOpenXml(Stream stream)
1316
stream.Seek(0, SeekOrigin.Begin);
1417

1518
// OpenXml format can be any ZIP archive
16-
if (probe is not [0x50, 0x4B, ..])
19+
if (!probe.StartsWith(ZipArchiveHeader))
1720
throw new InvalidDataException("The file is not a valid OpenXml document.");
1821
}
22+
23+
internal static void ThrowIfInvalidSheetName(string? sheetName)
24+
{
25+
if (string.IsNullOrEmpty(sheetName))
26+
throw new ArgumentException("Sheet names cannot be empty or null");
27+
28+
if (sheetName.Length > ExcelMaxSheetNameLength)
29+
throw new ArgumentException("Sheet names must be less than 31 characters");
30+
}
1931
}

src/MiniExcel.Core/OpenXml/OpenXmlWriter.DefaultOpenXml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ internal partial class OpenXmlWriter : IMiniExcelWriter
1717
{
1818
foreach (var sheet in dictionary)
1919
{
20+
ThrowHelper.ThrowIfInvalidSheetName(sheet.Key);
21+
2022
sheetId++;
2123
var sheetInfos = GetSheetInfos(sheet.Key);
2224
yield return Tuple.Create(sheetInfos.ToDto(sheetId), sheet.Value);

src/MiniExcel.Core/OpenXml/OpenXmlWriter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ internal OpenXmlWriter(Stream stream, object? value, string? sheetName, IMiniExc
4545
[CreateSyncVersion]
4646
internal static Task<OpenXmlWriter> CreateAsync(Stream stream, object? value, string? sheetName, bool printHeader, IMiniExcelConfiguration? configuration, CancellationToken cancellationToken = default)
4747
{
48-
if (string.IsNullOrEmpty(sheetName))
49-
throw new ArgumentException("Sheet names cannot be empty or null", nameof(sheetName));
50-
if (sheetName?.Length > 31)
51-
throw new ArgumentException("Sheet names must be less than 31 characters", nameof(sheetName));
48+
ThrowHelper.ThrowIfInvalidSheetName(sheetName);
5249

5350
var writer = new OpenXmlWriter(stream, value, sheetName, configuration, printHeader);
5451
return Task.FromResult(writer);

tests/MiniExcel.Core.Tests/MiniExcelIssueTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3690,4 +3690,24 @@ public void TestIssue869(string fileName, DateOnlyConversionMode mode, bool thro
36903690
}
36913691
}
36923692
}
3693+
3694+
[Fact]
3695+
public void TestIssue876()
3696+
{
3697+
var someTable = new[]
3698+
{
3699+
new { Name = "Jack", Age = 25 },
3700+
};
3701+
3702+
var sheets = new Dictionary<string, object>
3703+
{
3704+
["SomeVeryLongNameWithMoreThan31Characters"] = someTable
3705+
};
3706+
3707+
Assert.Throws<ArgumentException>(() =>
3708+
{
3709+
using var outputPath = AutoDeletingPath.Create();
3710+
_excelExporter.Export(outputPath.ToString(), sheets);
3711+
});
3712+
}
36933713
}

0 commit comments

Comments
 (0)