Skip to content
Closed
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
1 change: 1 addition & 0 deletions src/MiniExcel.Core/OpenXml/OpenXmlConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class OpenXmlConfiguration : MiniExcelBaseConfiguration
public bool EnableWriteNullValueCell { get; set; } = true;
public bool WriteEmptyStringAsNull { get; set; } = false;
public bool TrimColumnNames { get; set; } = true;
public bool TrimSheetNames { get; set; } = false;
public bool IgnoreEmptyRows { get; set; } = false;
public bool EnableSharedStringCache { get; set; } = true;
public long SharedStringCacheSize { get; set; } = 5 * 1024 * 1024;
Expand Down
6 changes: 6 additions & 0 deletions src/MiniExcel.Core/OpenXml/OpenXmlWriter.DefaultOpenXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ internal partial class OpenXmlWriter : IMiniExcelWriter
{
private readonly Dictionary<string, ZipPackageInfo> _zipDictionary = [];
private Dictionary<string, string> _cellXfIdMap;
private const int MaxCharactersSheetName = 31;

private IEnumerable<Tuple<SheetDto, object?>> GetSheets()
{
Expand Down Expand Up @@ -44,6 +45,11 @@ internal partial class OpenXmlWriter : IMiniExcelWriter

private ExcellSheetInfo GetSheetInfos(string sheetName)
{
if (_configuration.TrimSheetNames)
{
sheetName = sheetName[..Math.Min(sheetName.Length, MaxCharactersSheetName)];
}

var info = new ExcellSheetInfo
{
ExcelSheetName = sheetName,
Expand Down
27 changes: 27 additions & 0 deletions tests/MiniExcel.Core.Tests/MiniExcelIssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3690,4 +3690,31 @@ public void TestIssue869(string fileName, DateOnlyConversionMode mode, bool thro
}
}
}

[Theory(DisplayName = "#876: Enabling trimming only trims too long values when enabled")]
[InlineData("ShortName", false, "ShortName")]
[InlineData("SomeVeryLongNameWithMoreThan32Characters", false, "SomeVeryLongNameWithMoreThan32Characters")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This test case appears to be based on an incorrect assumption. With TrimSheetNames = false, the library is expected to throw an ArgumentException for sheet names longer than 31 characters, which is the standard Excel limitation and is verified by other tests in this file (e.g., Test_Issue_693_SaveSheetWithLongName). This test case expects the export to succeed, which will likely cause the test to fail.

I recommend removing this InlineData and creating a separate [Fact] test to assert that an ArgumentException is thrown for long sheet names when trimming is disabled. This would ensure both the new feature and the existing validation are correctly tested.

[InlineData("ShortName", true, "ShortName")]
[InlineData("SomeVeryLongNameWithMoreThan32Characters", true, "SomeVeryLongNameWithMoreThan32C")] // Trimmed to 31 chars
public async Task TestIssue876(string sheetName, bool trimSheetName, string sheetNameToReadWith)
{
// Arrange
var exporter = MiniExcel.Exporters.GetOpenXmlExporter();
var someTable = new[]
{
new { Name = "Jack", Age = 25 },
};
var sheets = new Dictionary<string, object>
{
[sheetName] = someTable
};
var outputPath = PathHelper.GetTempFilePath();

// Act
await exporter.ExportAsync(outputPath, sheets, configuration: new OpenXmlConfiguration{ TrimSheetNames = trimSheetName});

// Assert
var resultsFromTrimmedSheet = MiniExcel.Importers.GetOpenXmlImporter().Query(outputPath, sheetName: sheetNameToReadWith);
Assert.Equal(2, resultsFromTrimmedSheet.Count()); // 2, header and one record
}
}
Loading