MiniExcel icon indicating copy to clipboard operation
MiniExcel copied to clipboard

[BUG] - Cells with built-in number format 17 (mmm-yy) are read as a raw double instead of DateTime

Open pedrorossato opened this issue 1 month ago • 2 comments

Description

ExcelOpenXmlStyles declares the built-in number format 17 (mmm-yy) with a non-nullable TimeSpan:

{ 17, new NumberFormatString("mmm-yy",typeof(TimeSpan)) },

ConvertValueByStyleFormat only converts when the format type is typeof(DateTime?) or typeof(TimeSpan?). typeof(TimeSpan) matches neither, so a cell formatted with id 17 is returned as the raw OADate serial (double). The other built-in date formats (14, 15, 16 and 22) are declared as typeof(DateTime?) and are read as DateTime. Since mmm-yy is a date format, it should be declared the same way.

Excel writes id 17 when the user picks the built-in month/year format, which is common for month/year columns (in the pt-BR locale it is displayed as mmm/aa).

Minimal repro (.NET 10 file-based app, dotnet run repro.cs):

#:package [email protected]

using System.IO.Compression;
using MiniExcelLibs;

var path = Path.Combine(Path.GetTempPath(), "format17.xlsx");
MiniExcel.SaveAs(path, new[] { new { Value = "placeholder" } }, overwriteFile: true);

using (var zip = ZipFile.Open(path, ZipArchiveMode.Update))
{
    Replace(zip, "xl/styles.xml", """
        <?xml version="1.0" encoding="utf-8"?>
        <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
          <cellXfs count="2">
            <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>
            <xf numFmtId="17" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>
          </cellXfs>
        </styleSheet>
        """);
    Replace(zip, "xl/worksheets/sheet1.xml", """
        <?xml version="1.0" encoding="utf-8"?>
        <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
          <sheetData><row r="1"><c r="A1" s="1"><v>46327</v></c></row></sheetData>
        </worksheet>
        """);
}

var value = ((IDictionary<string, object>)MiniExcel.Query(path).Single())["A"];
Console.WriteLine($"{value} ({value.GetType().Name})"); // prints: 46327 (Double)

static void Replace(ZipArchive zip, string name, string xml)
{
    zip.GetEntry(name)!.Delete();
    using var writer = new StreamWriter(zip.CreateEntry(name).Open());
    writer.Write(xml);
}

Expected Behavior

A1 is returned as DateTime 2026-11-01, like the built-in formats 14, 15, 16 and 22.

Actual Behavior

A1 is returned as double 46327.

Workarounds

Convert the serial manually with DateTime.FromOADate when the column is known to hold dates.

Environment

  • MiniExcel Version: 1.44.1 (the same entry is in 1.46.1, v1.x-maintenance and master)
  • .NET Version: net10.0
  • OS: Windows

Files and/or Screenshots

The repro above generates the file.

Additional Context

The fix is a one-line change (typeof(TimeSpan) → typeof(DateTime?)). I have a PR ready against v1.x-maintenance with a unit test covering the built-in date formats. master has the same entry in src/MiniExcel.OpenXml/Styles/OpenXmlStyles.cs, and I can open a PR there as well if you prefer.

pedrorossato avatar Oct 29 '26 16:10 pedrorossato

Thanks for merging both PRs, @michelebastione! Is there an estimate for when a new release including this fix will be published? We're on the 1.x line, so a 1.x release would be especially helpful.

pedrorossato avatar Nov 05 '26 13:11 pedrorossato

I will prepare a release for the end of the week.

michelebastione avatar Nov 05 '26 17:11 michelebastione