How to: Configure Cell Font Settings
- 2 minutes to read
To set different font attributes for cells, use the properties of the SpreadsheetFont object.
To access this object for modifying the font of an individual cell, use the Cell object’s Formatting.Font property that is inherited form the Formatting interface.
To change font characteristics for a range of cells, call the Range.BeginUpdateFormatting method for this range, use the Font property of the returned Formatting object to access and modify the SpreadsheetFont object and call the Range.EndUpdateFormatting method to finalize the modification.
The SpreadsheetFont object provides the following properties to change cell font attributes.
- SpreadsheetFont.Name - sets the cell font.
- SpreadsheetFont.Size - sets the cell font size.
- SpreadsheetFont.FontStyle, SpreadsheetFont.Bold, SpreadsheetFont.Italic - sets whether the cell font should be bold or italic.
- SpreadsheetFont.Color - sets the cell font color.
- SpreadsheetFont.Script - sets whether the cell text should be formatted as superscript or subscript.
- SpreadsheetFont.Strikethrough - sets whether or not the cell text should be displayed with a horizontal line through the text.
- SpreadsheetFont.UnderlineType - sets the type of underline applied to the font. The UnderlineType enumeration lists available underline types.
To share font settings with multiple cells in a single step, create or modify the style with the Formatting.Font property specified as required, and assign this style to Range.Style for the desired cells.
This example demonstrates how to format cell font characteristics (e.g., font name, size, color, style) by modifying the SpreadsheetFont object accessed via the Formatting.Font property of the Cell object.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/spreadsheet-document-server-api-e4339
// Access the Font object.
SpreadsheetFont cellFont = worksheet.Cells["A1"].Font;
// Set the font name.
cellFont.Name = "Times New Roman";
// Set the font size.
cellFont.Size = 14;
// Set the font color.
cellFont.Color = Color.Blue;
// Format text as bold.
cellFont.Bold = true;
// Set font to be underlined.
cellFont.UnderlineType = UnderlineType.Double;
The image below shows the specified cell font (the workbook is opened in Microsoft® Excel®).