Reputation: 2759
We have Excel 2013-2016 Add-in in C#. We use following method to get worksheet font:
var defaultFont = Microsoft.Office.Interop.Excel.Worksheet.Cells.Font
Everything worked until user changed font of some particular cell. For example user changed cell A1 and set font "Arial". By default we have font "Calibri".
Thereafter when we try to get default font by Worksheet.Cells.Font
we get nothing - just empty object. I assume that because of ambiguity: A1 has font "Arial", other cells - default font "Calibri". My goal to get default font, i.e. "Calibri".
So far I re-write my code and now I check Styles too:
var workbookFont = Microsoft.Office.Interop.Excel.Worksheet.Parent.Styles["Normal"].Font;
This workaround returns exactly what I need - my "Calibri" font. But then I found , that's because this font is default for all new worksheets/workbooks (it can be setup in Options of Excel). It doesn't work if user selected all cells in worksheet and changed default font to "Times New Roman", then I would get "Calibri" - because this is default font will be used once new worksheet/workbook created. Again I faced issue when I can't get real default font for current worksheet. My next thought was to get font of some particular cell at the edge of worksheet that is not much usable for user like:
var defaultFont = workSheet.Cells[1048576][16384].Font
It looks pretty weird, but it works. My assumption that user doesn't use the last cell on a worksheet. (The 1048576 and 16384 are max size of worksheet ). I don't know implications of these method, so I'm wondering does it exist some "legal" way to get default font of worksheet , without such crutch ?
Upvotes: 3
Views: 2358
Reputation: 9461
You were on the right path when using:
var workbookFont = Microsoft.Office.Interop.Excel.Worksheet.Parent.Styles["Normal"].Font;
The Normal
style is the default font for a sheet, but the sheet might not actually have any instances of the default font (or the Normal
style), or, all cells decorated with the Normal
style may have had the font overridden on each cell format.
If a user applies their own fonts to various ranges, then the sheet will potentially have numerous fonts, and none of them are guaranteed to be the same as the font in the Normal
style. The Font.Name
might differ across a sheet, in which case Font.Name
returns null
, even though the Font.Size
might be consistent (or if it isn't, then it too returns null
).
When a user applies a custom font to a range that already has the Normal
style, then the Style
remains associated with the range, and the font overrides any font defined in the style.
Furthermore, it is common practice to select all the cells on the sheet when changing the font of the UsedRange
(so that the row heights adjust proportionally), even if the UsedRange
is only a small portion of all Cells
.
So, you have to choose a font that is representative of the fonts that are used in the sheet, or apply fonts to new ranges as if they were Normal
. That choice should be informed by what you know about the sheet, and what you intend to do with the font:
If you're inserting a column or row, then I'd suggest that you mimic Excel behavior and use the adjacent formats.
If you're adding a new range that is not adjacent to the UsedRange
, then you will probably want to default to the Normal
style.
If you're looking for a representative font of the UsedRange
, you might want to avoid the first few rows and columns, as these tend to be headers, and the last rows can sometimes be totals. You'll need to loop over the cells in the range to find an appropriate cell format.
If you're reproducing the sheet content in some other format, then you'll need to inspect the font of each cell and every cell in the UsedRange
.
The bottom-right-most cell is not necessarily any more or less similar to Normal
or the fonts used in the UsedRange
, so I'd advise against using it.
Upvotes: 2