Skip to content

Enhanced formatting capabilities#955

Merged
michelebastione merged 5 commits into
mini-software:masterfrom
michelebastione:proper-number-formats
May 10, 2026
Merged

Enhanced formatting capabilities#955
michelebastione merged 5 commits into
mini-software:masterfrom
michelebastione:proper-number-formats

Conversation

@michelebastione
Copy link
Copy Markdown
Collaborator

Added proper numerical formatting for numbers, dates and times

Prior to this update MiniExcelFormatAttribute converted all values to their string representation according to the specified format, instead of exporting them as numerical values linked to a style element as per the OpenXml specification intended. This prevented the results to be used as actual numerical values e.g. in formulas while still retaining their representation.

This PR addresses this problem and fixes it by optimizing the way SheetStyleBuildContext keeps track of format mappings to correctly make DefaultSheetStyleBuilder dynamically add new styles for each distinct one.
A new hardcoded style has also been added for automatically displaying TimeSpan and TimeOnly as formatted times even when a specific format is not specified by the user.

The OpenXmlNumberFormatHelper utility has also been revised so that it accepts multiple character currencies as valid numeric literals.

Closes #506
Fixes #520
Resolves #627

Added deconstruct method polyfill for type `KeyValuePair<TKey, TValue>`
Made the MiniExcelFormatAttribute correctly add cellXfs entries in the styles.xml file by separating the creation and initialization of SheetStyleBuildContext, keeping track of the column mappings and their relative styles through the new SheetStyleFormatsCache class. The redundant utilities SheetStyleBuilderHelper and SheetStyleBuildResult have been removed, and tests have been updated to reflect the changes.
Changed OpenXmlWriter to treat TimeSpan values as the number of seconds from the start of the day, when less than a single day, or as the number of seconds after 1899/12/31 otherwise. Style builders have been edited to include a new cellXfs element as the default style for unformatted time values that still need to be treated as numeric in Excel.
Adjusted `OpenXmlNumberFormatHelper` to accept multiple character currencies as valid formatting literals
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the OpenXml writing logic to improve style and number format management through the introduction of SheetStyleBuildContext and SheetStyleFormatsCache. It also extends support for additional date and time types, such as DateTimeOffset and TimeSpan, and enhances asynchronous resource disposal. A critical bug was identified in the SheetStyleFormatsCache where the style index counter is incremented for every column mapping regardless of whether the format is unique, which would lead to incorrect style assignments and broken references in the generated Excel files.

Comment thread src/MiniExcel.OpenXml/Styles/Builder/SheetStyleFormatsCache.cs Outdated
@michelebastione michelebastione merged commit ac6ae3c into mini-software:master May 10, 2026
3 checks passed
@michelebastione michelebastione deleted the proper-number-formats branch May 10, 2026 23:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant