Format cells
Introduction
Format Cells using GetStyle and SetStyle Methods
Apply different kinds of formatting styles on cells to set background or foreground colors, borders, fonts, horizontal and vertical alignments, indentation level, text direction, rotation angle and much more.
Using the GetStyle and SetStyle Methods
If developers need to apply different formatting styles to different cells then it’s better to get the Style of the cell using Cell.GetStyle method, specify the style attributes and then apply the formatting using Cell.SetStyle method. An example is given below to demonstrate this approach to apply various formatting on a cell.
Using Style Object to Format Different Cells
If developers need to apply the Same formatting style to different cells then they can use Style object. Please follow the steps below to use the Style object:
- Add a Style object by calling the CreateStyle method of the Workbook class
- Access the newly added Style object
- Set the desired properties/attributes of the Style object to apply desired formatting settings
- Assign the configured Style object to your desired cells
This approach can greatly improve the efficiency of your applications and save memory too.
Using Microsoft Excel 2007 Predefined Styles
If you need to apply different formatting styles for Microsoft Excel 2007, apply styles using the Aspose.Cells API. An example is given below to demonstrate this approach to apply a predefined style on a cell.
Formatting Selected Characters in a Cell
Dealing with Font Settings explains how to format text in cells, but it only explains how to format all of the cell content. What if you want to format only selected characters?
Aspose.Cells supports this feature too. This topic explains how to we use this feature effectively.
Formatting Selected Characters
Aspose.Cells provides a class, Workbook that represents a Microsoft Excel file. The Workbook class contains the 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.
The Cell class provides the Characters method that takes the following parameters to select a range of characters inside a cell:
- Start Index, the index of the character that the selection starts from.
- Number of Characters, the number of characters to select.
The Characters method returns an instance of the FontSetting class that allows developers to format the characters in the same way as they would a cell as shown below in the code example. In the output file, in the A1 cell, the word ‘Visit’ will be formatted with the default font but ‘Aspose!’ is bold and blue.
Formatting Rows and Columns
Sometimes, developers need to apply the same formatting on rows or columns. Applying formatting on cells one by one often takes longer and is not a good solution. To address this issue, Aspose.Cells provides a simple, fast way discussed in detail in this article.
Formatting Rows & Columns
Aspose.Cells provides a class, the Workbook that represents a Microsoft Excel file. The Workbook class contains a Worksheets collection that allows access to each worksheet in the Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides a Cells collection. The Cells collection provides a Rows collection.
Formatting a Row
Each item in the Rows collection represents a Row object. The Row object offers the ApplyStyle method used to set the row’s formatting. To apply the same formatting to a row, use the Style object. The steps below show how to use it.
- Add a Style object to the Workbook class by calling its CreateStyle method.
- Set the Style object’s properties to apply formatting settings.
- Make the relevant attributes ON for the StyleFlag object.
- Assign the configured Style object to the Row object.
Formatting a Column
The Cells collection also provides a Columns collection. Each item in the Columns collection represents a Column object. Similar to a Row object, the Column object also offers the ApplyStyle method for formatting a column.