Skip to content

Allow null font name, Excel uses default font#1778

Open
opencode-agent[bot] wants to merge 2 commits into
masterfrom
opencode/issue1417-20260401065412
Open

Allow null font name, Excel uses default font#1778
opencode-agent[bot] wants to merge 2 commits into
masterfrom
opencode/issue1417-20260401065412

Conversation

@opencode-agent
Copy link
Copy Markdown
Contributor

@opencode-agent opencode-agent Bot commented Apr 1, 2026

PR created. The branch opencode/issue1417-20260401065412 has been pushed with commit:

56d01bb - Allow XSSF font name to be null, letting Excel use its default font

Changes Made (3 files, +12 -7 lines):

  1. XSSFFont.cs - Removed default font name from constructor; getter/setter now handle null
  2. StylesTable.cs - Removed explicit font name in CreateDefaultFont()
  3. XSSFTextRun.cs - FontFamily getter returns null instead of DEFAULT_FONT_NAME

When FontName is not set, the XML won't include a <name> element, allowing Excel to use its own default font (Aptos, Calibri, or user's preference).

Closes #1417

New%20session%20-%202026-04-01T06%3A54%3A11.511Z
opencode session  |  github run

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates XSSF font handling to allow omitting the font name in the generated XML so Excel can apply its own current default font (Aptos/Calibri/user-configured).

Changes:

  • Stop auto-populating XSSFFont.FontName with DEFAULT_FONT_NAME; allow it to be unset (null) so <name> is omitted.
  • Remove explicit default font name assignment in StylesTable.CreateDefaultFont().
  • Make XSSFTextRun.FontFamily return null when the run has no font family set.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
ooxml/XSSF/UserModel/XSSFTextRun.cs Returns null for FontFamily when unset to reflect missing XML and Excel-default behavior.
ooxml/XSSF/UserModel/XSSFFont.cs Makes FontName nullable and removes default-name assignment from the constructor/setter logic.
ooxml/XSSF/Model/StylesTable.cs Stops setting a default font name when creating the default font in the styles table.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

xssfFont.FontHeightInPoints = (XSSFFont.DEFAULT_FONT_SIZE);
xssfFont.Color = (XSSFFont.DEFAULT_FONT_COLOR);//SetTheme
xssfFont.FontName = (XSSFFont.DEFAULT_FONT_NAME);
xssfFont.Color = (XSSFFont.DEFAULT_FONT_COLOR);
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

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

With FontName now allowed to be null (default font name omitted), StylesTable.FindFont() currently uses font.FontName.Equals(name) in both overloads, which will throw a NullReferenceException when either the stored font has no name (e.g., the default font created here) or when the caller passes name == null. Please switch these comparisons to a null-safe string comparison (e.g., string.Equals(font.FontName, name, StringComparison.Ordinal)).

Suggested change
xssfFont.Color = (XSSFFont.DEFAULT_FONT_COLOR);
xssfFont.Color = (XSSFFont.DEFAULT_FONT_COLOR);
xssfFont.FontName = XSSFFont.DEFAULT_FONT_NAME;

Copilot uses AI. Check for mistakes.
Comment on lines 216 to 220
{
return font.typeface;
}
return XSSFFont.DEFAULT_FONT_NAME;
return null;
}
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

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

Changing FontFamily to return null when unset will break existing expectations in tests (e.g., TestXSSFDrawing currently asserts runs[1].FontFamily == XSSFFont.DEFAULT_FONT_NAME). Please update the affected tests to expect null (or explicitly set a font family in the test setup) to match the new behavior of omitting the font name element.

Copilot uses AI. Check for mistakes.
@tonyqus
Copy link
Copy Markdown
Member

tonyqus commented Apr 9, 2026

/oc fix the issue according to copilot review comments

- Use string.Equals with Ordinal comparison in StylesTable.FindFont()
  for null-safe font name comparison
- Update TestXSSFDrawing to expect null FontFamily when font name
  is not set in the XML
@opencode-agent
Copy link
Copy Markdown
Contributor Author

opencode-agent Bot commented Apr 9, 2026

Fixed both issues:

  1. StylesTable.cs - Changed font.FontName.Equals(name) to string.Equals(font.FontName, name, StringComparison.Ordinal) in both FindFont overloads to handle null FontName safely.

  2. TestXSSFDrawing.cs - Updated assertion from ClassicAssert.AreEqual(XSSFFont.DEFAULT_FONT_NAME, runs[1].FontFamily) to ClassicAssert.IsNull(runs[1].FontFamily) to match the new behavior.

New%20session%20-%202026-04-09T08%3A08%3A01.519Z
opencode session  |  github run

@tonyqus
Copy link
Copy Markdown
Member

tonyqus commented Apr 9, 2026

This PR is a bit risky. It can break the Excel file

@tonyqus tonyqus modified the milestones: NPOI 2.8.1, NPOI 2.9.0 Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Excel has new default font Aptos, NPOI should not set a default FontName

2 participants