From 2e8ebb3ac5d15977d55cddb4913e1a10a6f19664 Mon Sep 17 00:00:00 2001 From: Michele Bastione Date: Wed, 25 Nov 2026 16:58:56 +0100 Subject: [PATCH 1/2] Fix issue preventing correct date serialization Due to an oversight, when exporting a document using a non invariant `CultureInfo` in the configuration, the format of a property specified in the `MiniExcelFormatAttribute` was not honored. --- .../Writer/OpenXmlWriter.XmlGeneration.cs | 2 +- .../Issues/MiniExcelGithubIssuesTests.cs | 21 +++++++++++++++++++ .../MiniExcel.OpenXml.Tests/Issues/Models.cs | 6 ++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/MiniExcel.OpenXml/Writer/OpenXmlWriter.XmlGeneration.cs b/src/MiniExcel.OpenXml/Writer/OpenXmlWriter.XmlGeneration.cs index 5ba35110..dfb47896 100644 --- a/src/MiniExcel.OpenXml/Writer/OpenXmlWriter.XmlGeneration.cs +++ b/src/MiniExcel.OpenXml/Writer/OpenXmlWriter.XmlGeneration.cs @@ -324,7 +324,7 @@ private string GetFileValue(int rowIndex, int cellIndex, object value) private (string, string, string) GetDateTimeValue(DateTime value, MiniExcelColumnMapping? columnMapping) { string? cellValue; - if (!ReferenceEquals(_configuration.Culture, CultureInfo.InvariantCulture)) + if (string.IsNullOrEmpty(columnMapping?.ExcelFormat) && !ReferenceEquals(_configuration.Culture, CultureInfo.InvariantCulture)) { cellValue = value.ToString(_configuration.Culture); return (RegularCellStyleIndex, ExcelDataTypes.CalculatedString, cellValue); diff --git a/tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs b/tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs index 90db09ff..a4fb933f 100644 --- a/tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs +++ b/tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs @@ -3400,4 +3400,25 @@ public void FillMergedCellsAppliesTheStyleOfTheFirstCellOfTheMergedRange() Assert.Equal(3, rows.Count); Assert.All(rows, row => Assert.Equal(new DateTime(2026, 11, 1), (object)row.A)); } + + [Fact] + public async Task TestIssue1073_CultureWithFormatSerializesCorrectly() + { + var date = new DateTime(2026, 11, 25); + + var rows = new[] { new Issue1073Dto { Date = date } }; + var config = new OpenXmlConfiguration + { + Culture = CultureInfo.GetCultureInfo("ja-JP") + }; + + using var ms = new MemoryStream(); + await _excelExporter.ExportAsync(ms, rows, configuration: config); + + ms.Seek(0, SeekOrigin.Begin); + using var wb = new XLWorkbook(ms); + var cell = wb.Worksheet(1).Cell("A2"); + + Assert.Equal(date, cell.Value.GetDateTime()); + } } diff --git a/tests/MiniExcel.OpenXml.Tests/Issues/Models.cs b/tests/MiniExcel.OpenXml.Tests/Issues/Models.cs index 743d8f33..132b8241 100644 --- a/tests/MiniExcel.OpenXml.Tests/Issues/Models.cs +++ b/tests/MiniExcel.OpenXml.Tests/Issues/Models.cs @@ -259,6 +259,12 @@ internal class Issue951Dto public object this[string? test] => new(); } +public class Issue1073Dto +{ + [MiniExcelFormat("yyyy/MM/dd")] + public DateTime Date { get; set; } +} + internal class TestIssueI4YCLQ_2Dto { [MiniExcelColumnIndex("A")] public string? 站点编码 { get; set; } From 704be74251c8c9325c4c35156f6e9cab92235196 Mon Sep 17 00:00:00 2001 From: Michele Bastione Date: Wed, 25 Nov 2026 17:44:04 +0100 Subject: [PATCH 2/2] Fix minor date serialization error `DateTime`s falling in the interval between 28/02/1900 and 01/03/1900 were not properly adjusted to represent the correct date instead of the non-existent 29/02/1900. --- src/MiniExcel.OpenXml/Writer/OpenXmlWriter.XmlGeneration.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MiniExcel.OpenXml/Writer/OpenXmlWriter.XmlGeneration.cs b/src/MiniExcel.OpenXml/Writer/OpenXmlWriter.XmlGeneration.cs index dfb47896..41b16863 100644 --- a/src/MiniExcel.OpenXml/Writer/OpenXmlWriter.XmlGeneration.cs +++ b/src/MiniExcel.OpenXml/Writer/OpenXmlWriter.XmlGeneration.cs @@ -342,10 +342,10 @@ private static double CorrectDateTimeValue(DateTime value) // Excel says 1900 was a leap year :( Replicate an incorrect behavior thanks // to Lotus 1-2-3 decision from 1983... // https://github.com/ClosedXML/ClosedXML/blob/develop/ClosedXML/Extensions/DateTimeExtensions.cs#L45 - const int nonExistent1900Feb29SerialDate = 60; + const double serialDate01March1900 = 61d; var oaDate = value.ToOADate(); - if (oaDate <= nonExistent1900Feb29SerialDate) + if (oaDate < serialDate01March1900) { oaDate--; }