Alignment Settings
Configuring Alignment Settings
Alignment settings in Microsoft Excel
Anyone who has used Microsoft Excel to format cells will be familiar with the alignment settings in Microsoft Excel.
As you can see from the above figure, there are different kinds of alignment options:
- Text alignment(horizontal & vertical)
- Indentation.
- Orientation.
- Text control.
- Text direction.
All of these alignment settings are fully supported by Aspose.Cells and are discussed in more detail below.
Alignment settings in Aspose.Cells
Aspose.Cells provides GetStyle and SetStyle methods for the Cell class that are used to get and set a cell’s formatting. The Style class provides useful properties for configuring alignment settings.
Select any text alignment type using the TextAlignmentType enumeration. The pre-defined text alignment types in the TextAlignmentType enumeration are:
Text Alignment Types | Description |
---|---|
Bottom | Represents bottom text alignment |
Center | Represents center text alignment |
CenterAcross | Represents center across text alignment |
Distributed | Represents distributed text alignment |
Fill | Represents fill text alignment |
General | Represents general text alignment |
Justify | Represents justify text alignment |
Left | Represents left text alignment |
Right | Represents right text alignment |
Top | Represents top text alignment |
JustifiedLow | Aligns the text with an adjusted kashida length for Arabic text. |
ThaiDistributed | Distributes Thai text especially, because each character is treated as a word. |
Horizontal , Vertical Alignment and Indentation
Use the HorizontalAlignment property to align the text horizontally and VerticalAlignment property to align the text vertically. It is possible to set the indentation level of the text in a cell with the IndentLevel property and tt only effects when Horizontal alignment is left or right.
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Visit Aspose!"); | |
// Setting the horizontal alignment of the text in the "A1" cell | |
Style style = cell.getStyle(); | |
//Set text left horizontal alignment | |
style.setHorizontalAlignment(TextAlignmentType.RIGHT); | |
//Set indent | |
style.setIndentLevel(4); | |
//Set text top vertical alignment | |
style.setVerticalAlignment(TextAlignmentType.TOP); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("book1.xlsx"); |
Orientation
Set the orientation (rotation) of the text in a cell with the RotationAngle property.
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Visit Aspose!"); | |
// Setting the horizontal alignment of the text in the "A1" cell | |
Style style = cell.getStyle(); | |
// Setting the rotation of the text (inside the cell) to 25 | |
style.setRotationAngle(25); | |
cell.setStyle(style); | |
//Accessing the "A2" cell from the worksheet | |
cell = worksheet.getCells().get("A2"); | |
// Adding some value to the "A1" cell | |
cell.putValue("Visit Aspose!"); | |
// Setting the horizontal alignment of the text in the "A2" cell | |
style = cell.getStyle(); | |
// Setting the orientation of the text from top to bottom | |
style.setRotationAngle(255); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("book1.xlsx"); |
Text Control
The following section discusses how to control text by setting text wrapping, shrink to fit and other formatting options.
Wrapping Text
Wrapping text in a cell makes it easier to read: the height of the cell adjusts to fit all the text, instead of cutting it off or spilling over into adjacent cells. Set text wrapping on or off with the IsTextWrapped property.
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("I am using the latest version of Aspose.Cells to test this functionality."); | |
// Gets style | |
Style style = cell.getStyle(); | |
// Wrap Cell's Text wrap | |
style.setTextWrapped( true); | |
//Set style. | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("book1.xlsx"); |
Shrinking to Fit
An option to wrapping text in a field is to shrink the text size to fit a cell’s dimensions. This is done by setting the ShrinkToFit property. to true.
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("I am using the latest version of Aspose.Cells to test this functionality."); | |
// Gets style in the "A1" cell | |
Style style = cell.getStyle(); | |
// Shrinking the text to fit according to the dimensions of the cell | |
style.setShrinkToFit(true); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("book1.xlsx"); | |
Merging Cells
Like Microsoft Excel, Aspose.Cells supports merging several cells into one. Aspose.Cells provides two approaches to this task. One way is to call the Merge method. The method takes the following parameters to merge the cells:
- First row: the first row from where to start merging.
- First column: the first column from where to start merging.
- Number of rows: the number of rows to merge.
- Number of columns: the number of columns to merge.
// Create a Cells object ot fetch all the cells. | |
Cells cells = worksheet.getCells(); | |
// Merge some Cells (C6:E7) into a single C6 Cell. | |
cells.merge(5, 2, 2, 3); | |
// Input data into C6 Cell. | |
worksheet.getCells().get(5, 2).putValue("This is my value"); | |
// Create a Style object to fetch the Style of C6 Cell. | |
Style style = worksheet.getCells().get(5, 2).getStyle(); | |
// Create a Font object | |
Font font = style.getFont(); | |
// Set the name. | |
font.setName("Times New Roman"); | |
// Set the font size. | |
font.setSize(18); | |
// Set the font color | |
font.setColor(Color.getBlue()); | |
// Bold the text | |
font.setBold(true); | |
// Make it italic | |
font.setItalic(true); | |
// Set the backgrond color of C6 Cell to Red | |
style.setForegroundColor(Color.getRed()); | |
style.setPattern(BackgroundType.SOLID); | |
// Apply the Style to C6 Cell. | |
cells.get(5, 2).setStyle(style); | |
Text Direction
It is possible to set the reading order of text in cells. The reading order is the visual order in which characters, words, etc. are displayed. For example, English is a left to right language while Arabic is a right to left language.
The reading order is set with the TextDirection property. Aspose.Cells provides pre-defined text direction types in the TextDirectionType enumeration.
Text Direction Types | Description |
---|---|
Context | The reading order consistent with the language of the first entered character |
LeftToRight | Left to right reading order |
RightToLeft | Right to left reading order |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Accessing the "A1" cell from the worksheet | |
Cell cell = worksheet.getCells().get("A1"); | |
// Adding some value to the "A1" cell | |
cell.putValue("I am using the latest version of Aspose.Cells to test this functionality."); | |
// Gets style in the "A1" cell | |
Style style = cell.getStyle(); | |
// Shrinking the text to fit according to the dimensions of the cell | |
style.setTextDirection(TextDirectionType.LEFT_TO_RIGHT); | |
cell.setStyle(style); | |
// Saving the Excel file | |
workbook.save("book1.xlsx"); |