rabanti-github / PicoXLSX

PicoXLSX is a small .NET / C# library to create XLSX files (Microsoft Excel 2007 or newer) in an easy and native way
MIT License
52 stars 13 forks source link

Style font setting doesn't appear to work. #27

Open ronnyek opened 3 months ago

ronnyek commented 3 months ago

I've taken from example, code where its setting font like so:

Style s = new Style();
s.Append(Style.BasicStyles.Font("Arial Black", 11));

And then call .AddNextCell("text", s);

No matter what I do, it always renders the text as "Calibri". Interestingly it does seem to take the font size from that, and render text smaller (I'm actually specifying "Arial Narrow" and 8 for the font) but doesn't change the font itself.

this is with version 3.2.1

rabanti-github commented 3 months ago

Hi, Thanks for reporting the issue. It looks, like it can be solved by the following code:

Style s = new Style();
s.CurrentFont.Scheme = Style.Font.SchemeValue.none;
s.Append(Style.BasicStyles.Font("Arial Black", 11));

I'm not entirely sure whether it is a bug or just a feature that should be better documented. I will read into the documentation about the scheme value of the style font part. With the information from above, you can set the font correctly, in the meantime. I will reach back to you.

rabanti-github commented 3 months ago

Hi, So, I was looking into the definition of the font scheme... But it was a little bit vague:

18.18.33 ST_FontScheme (Font scheme Styles)

Defines the font scheme, if any, to which this font belongs. This simple type's contents are a restriction of the W3C XML Schema string datatype. This simple type is restricted to the values listed in the following table: Enumeration value Description
major (Major Font) This font is the major font for this theme.
minor (Minor Font) This font is the minor font for this theme.
none (None) This font is not a theme font.

It seems to be related to the theme of the worksheet. But to be honest, normally I completely neglect this, because I don't know anyone that actively uses theming. PicoXLSX and NanoXLSX are not even writing a theme definition in the current version (what is valid). When I look into the default theme, generated by Excel, then is 'Calibri' listed under 'minor' fonts and 'Calibri Light' is listed under 'major' fonts. In the font section of the style definition, 'Calibri' is the default font, with the scheme 'minor' (what seems to be redundant).

The outcome of this is, if other fonts than the defined minor and major fonts (Calibri and Calibri Light) are set as font faces, then the scheme should always be 'None'. I will adapt the default value in the next Version of PicoXLSX/NanoXLSX when creating a custom font to 'None', if it is not Calibri or Calibri Light. In this major version, I will just do a hardcoded check. In an upcoming major version, the theme will be provided and then a logical check between the style and the theme can be made.

rabanti-github commented 2 months ago

The issue should be fixed automatically with the new release v3.3.0 (NuGet package available in some minutes). In this release, the scheme is automatically set, when a font name is defined.

The scheme value can be overwritten indeed after defining the font name.