Font Settings

Configuring Font Settings

Aspose.Cells provides a class, Workbook that represents a Microsoft Excel file. The Workbook class contains a Worksheets collection that allows access to each worksheet in an Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides a Cells collection. Each item in the Cells collection represents an object of the Cell class.

Aspose.Cells provides the Cell class' GetStyle and SetStyle methods which are used to get and set a cell’s formatting style. The Style class provides properties for configuring font settings.

Setting Font Name

Developers can apply any font to text inside a cell by using the Style.Font object’s Name property.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting the font name to "Times New Roman"
style.Font.Name = "Times New Roman";
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

Setting Font Style to Bold

Developers can make text bold by setting the Style.Font object’s IsBold property to true.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting the font weight to bold
style.Font.IsBold = true;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save("out.xlsx");

Setting Font Size

Set the font size with the Style.Font object’s Size property.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting the font size to 14
style.Font.Size = 14;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save("out.xlsx");

Setting Font Color

Use the Style.Font object’s Color property to set the font color. Select any color from the Color enumeration (part of the .NET framework) and assign it to the Color property.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting the font color to blue
style.Font.Color = Color.Blue;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save("out.xlsx");

Setting Font Underline Type

Use the Style.Font object’s Underline property to underline text. Aspose.Cells offers various pre-defined font underline types in the FontUnderlineType enumeration.

Font Underline Types Description
Accounting A single accounting underline
Double Double underline
DoubleAccounting Double accounting underline
None No underline
Single A single underline
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting the font to be underlined
style.Font.Underline = FontUnderlineType.Single;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "out.xlsx");

Setting Strikeout Effect

Apply strikeout by setting the Style.Font object’s IsStrikeout property to true.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting the strike out effect on the font
style.Font.IsStrikeout = true;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "out.xlsx");

Setting Subscript Effect

Apply subscript by setting the Style.Font object’s IsSubScript property to true.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting subscript effect
style.Font.IsSubscript = true;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "out.xlsx");

Setting Superscript Effect on Font

Developers can apply the superscript effect on the font by setting the IsSuperscript property of the Style.Font object to true.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Obtaining the style of the cell
Style style = cell.GetStyle();
// Setting superscript effect
style.Font.IsSuperscript = true;
// Applying the style to the cell
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "out.xlsx");

Advance topics