C# Logo

C# Font SpreadsheetGear API Sample

Excel-Compatible Samples for .NET

Description

Use the SpreadsheetGear.IFont interface to access various properties relating to font formatting, such as IFont.Name, IFont.Size, IFont.Bold, IFont.Italic, IFont.Underline, IFont.Superscript, IFont.SubscriptIFont.Color, IFont.ThemeColor and others.

This sample demonstrates setting the font of cell B2 to use Times New Roman, 14pt, bold and with the pre-defined color SpreadsheetGear.Colors.Blue.

Note that the SpreadsheetGear.IFont interface is available for objects besides cells—it's used anywhere that fonts can be formatted. For instance, various components of a chart will have a Font property such as IChartTitle.Font or ILegend.Font. Shapes that support Text Frames (such as a TextBox) use a Font property to modify its text. Workbook Styles also have an IStyle.Font property.

Sample C# Source Code

// Create a new workbook and some local variables.
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
SpreadsheetGear.IWorksheet worksheet = workbook.ActiveWorksheet;
SpreadsheetGear.IRange cells = worksheet.Cells;

// Get a reference to cell B2.
SpreadsheetGear.IRange cell = cells["B2"];

// Add text to the cell.
cell.Value = "SpreadsheetGear Engine for .NET";

// Get a reference to the range's font.
SpreadsheetGear.IFont font = cell.Font;

// Set various font options.
font.Name = "Times New Roman";
font.Size = 14;
font.Bold = true;
font.Color = SpreadsheetGear.Colors.Blue;

// AutoFit the column.
cell.EntireColumn.AutoFit();