How to: Configure Cell Font Settings
- 2 minutes to read
Use the SpreadsheetFont object’s properties to specify font attributes for worksheet cells and cell ranges.
The Cell.Font property allows you to modify the font for an individual cell.
To change font characteristics for a cell range, call the CellRange.BeginUpdateFormatting method for this range, use the Font property of the returned object to access and modify the SpreadsheetFont properties, and call the CellRange.EndUpdateFormatting method to finalize modifications.
The SpreadsheetFont object contains the following properties used to change cell font attributes:
- SpreadsheetFont.Name — sets the font name. The Spreadsheet control allows you to load and use fonts that are not installed on the current operating system. Refer to this topic for details: How to: Load and Use Custom Fonts in the Spreadsheet Control.
- SpreadsheetFont.Size — sets the font size.
- SpreadsheetFont.FontStyle, SpreadsheetFont.Bold, SpreadsheetFont.Italic — allows you to make the cell font bold or italic.
- SpreadsheetFont.Color — sets the font color.
- SpreadsheetFont.Script — allows you to format cell text as superscript or subscript.
- SpreadsheetFont.Strikethrough — allows you to display a horizontal line through cell text.
- SpreadsheetFont.UnderlineType — specifies the font underline type.
Create and modify cell styles to apply multiple format attributes to worksheet cells simultaneously.
The following example demonstrates how to specify cell font characteristics (font name, size, color, and style).
// 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.Single;
The image below shows the result.