1

I am using an IEnumerable variable to create an Excel worksheet in C# windows form application. The problem is that I can set created worksheet font if the font name has 1 word, ex. 'IRNazanin', but for font name with space it doesn't work, ex. 'B Mitra'. My code is:

var wb = new XLWorkbook { RightToLeft = true };
var ws = wb.Worksheets.Add("Sick_Information");
ws.Columns().AdjustToContents();
ws.Style.Font.FontName = "B Mitra";

I have tried using font name:

  • without spase, ex. "BMitra"
  • with a hyphen, ex. "B-Mitra"
  • with underline, ex. "B_Mitra"
  • with extra single qout, ex. "'B Mitra'"

Still not working.
I am using ClosedXML.Excel library with ClosedXML version 0.94.2.0 and MS Office 2016. The excel will get the font name but won't apply it.

enter image description here

True B Mitra font is like this. (Changed manually)

enter image description here

The customer needs file with B Mitra font and Persian numbers. So I have no choice to change the font.

Ali Majed HA
  • 212
  • 1
  • 4
  • 16
  • Is it excel interop? please add the tag for library name. – Reza Aghaei Dec 25 '19 at 07:26
  • @RezaAghaei it is `ClosedXML.Excel` library. – Ali Majed HA Dec 25 '19 at 07:28
  • I tried [ClosedXML package](https://www.nuget.org/packages/ClosedXML) version 0.95.0-beta2 (latest) and 0.94.2 (latest stable) package and it works as expected. Tried `"B Nazanin"` which doesn't exists on my system and `"Times New Roman"` which exists on my system. – Reza Aghaei Dec 25 '19 at 07:42
  • My `ClosedXML.Excel` version is `0.91.0.0`. – Ali Majed HA Dec 25 '19 at 07:54
  • Try using the latest or latest stable version. – Reza Aghaei Dec 25 '19 at 07:57
  • I have upgraded my `ClosedXML` version to `0.94.2.0` but still not working. – Ali Majed HA Dec 25 '19 at 08:02
  • Fix the font in Excel, save the file, rename it to `.zip`, open it, find `styles.xml`, open that and find the way the font name is stored. Use that in ClosedXML. – Francois Botha Dec 26 '19 at 08:06
  • @FrancoisBotha I did what you said. In `styles.xml` file, font value is ``. I copy and paste it in my code. But still not working – Ali Majed HA Dec 26 '19 at 08:56
  • Please log an issue on the Github repo and I'll look into it when I'm back from holiday. – Francois Botha Dec 26 '19 at 09:01
  • Have you tried going to C:/Windows/Fonts and checking what the filename is for B Mitra? Perhaps if you try that filename without extension, it will work? Also, have you tried other fonts that you can verify exist within C:/Windows/Fonts with spaces in the font name? – Lennart Jan 17 '20 at 11:30
  • @Lennart yes I have tried that. It doesn't work for all fonts with space in their name like 'B Nazanin' which is a Persian font. – Ali Majed HA Jan 17 '20 at 12:27
  • https://superuser.com/questions/182039/how-to-change-some-of-the-numbers-in-word-to-be-arabic-numbers-within-word – Hans Passant Jan 18 '20 at 14:27

2 Answers2

3

"B Mitra" is a valid font name, but you also need to change FontCharSet:

cell.Style.Font.SetFontName("B Mitra");
cell.Style.Font.SetFontCharSet(XLFontCharSet.Arabic);

ClosedXML uses XLFontCharSet.Default by default. Output Excel file shows correct font name, but doesn't display cell content properly:

font

I used Open XML SDK 2.5 Productivity Tool for Microsoft Office to inspect Excel file. With XLFontCharSet.Arabic there is a line there:

FontCharSet fontCharSet6 = new FontCharSet(){ Val = 178 };

And content is displayed as expected.

ASh
  • 34,632
  • 9
  • 60
  • 82
0

If you are using ClosedXML library then try use setFontName method:

ws.Style.Font.SetFontName("B Mitra");
  • 1
    When i tried with like font names Bell MT, Berlin Sans FB and Gill Sans MT then it's worked. My excel version 2016 and CloseXML version 0.93.1 –  Dec 25 '19 at 07:44
  • I have Office version 2016 and `ClosedXML.Excel` version `0.91.0.0` – Ali Majed HA Dec 25 '19 at 07:56