Back to Devexpress

How to: Align Cell Content in Spreadsheet Documents

officefileapi-12379-spreadsheet-document-api-examples-formatting-how-to-align-cell-content.md

latest5.1 KB
Original Source

How to: Align Cell Content in Spreadsheet Documents

  • Feb 26, 2024
  • 3 minutes to read

Use the Alignment object properties to align data contained in a cell. The properties are the following:

You can specify the alignment properties to an individual cell or to a range of cells.

Align Content in a Single Cell

Use the Cell.Alignment property to modify the alignment of individual cell content.

This example demonstrates how to specify the alignment of cell content so the cells appear as on the image below:

View Example

csharp
Cell cellA1 = worksheet.Cells["A1"];
cellA1.Value = "Right and top";
cellA1.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Right;
cellA1.Alignment.Vertical = SpreadsheetVerticalAlignment.Top;

Cell cellA2 = worksheet.Cells["A2"];
cellA2.Value = "Center";
cellA2.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
cellA2.Alignment.Vertical = SpreadsheetVerticalAlignment.Center;

Cell cellA3 = worksheet.Cells["A3"];
cellA3.Value = "Left and bottom, indent";
cellA3.Alignment.Indent = 1;

Cell cellB1 = worksheet.Cells["B1"];
cellB1.Value = "The Alignment.ShrinkToFit property is applied";
cellB1.Alignment.ShrinkToFit = true;

Cell cellB2 = worksheet.Cells["B2"];
cellB2.Value = "Rotated Cell Contents";
cellB2.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
cellB2.Alignment.Vertical = SpreadsheetVerticalAlignment.Center;
cellB2.Alignment.RotationAngle = 15;

Cell cellB3 = worksheet.Cells["B3"];
cellB3.Value = "The Alignment.WrapText property is applied to wrap the text within a cell";
cellB3.Alignment.WrapText = true;
vb
Dim cellA1 As Cell = worksheet.Cells("A1")
cellA1.Value = "Right and top"
cellA1.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Right
cellA1.Alignment.Vertical = SpreadsheetVerticalAlignment.Top

Dim cellA2 As Cell = worksheet.Cells("A2")
cellA2.Value = "Center"
cellA2.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center
cellA2.Alignment.Vertical = SpreadsheetVerticalAlignment.Center

Dim cellA3 As Cell = worksheet.Cells("A3")
cellA3.Value = "Left and bottom, indent"
cellA3.Alignment.Indent = 1

Dim cellB1 As Cell = worksheet.Cells("B1")
cellB1.Value = "The Alignment.ShrinkToFit property is applied"
cellB1.Alignment.ShrinkToFit = True

Dim cellB2 As Cell = worksheet.Cells("B2")
cellB2.Value = "Rotated Cell Contents"
cellB2.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center
cellB2.Alignment.Vertical = SpreadsheetVerticalAlignment.Center
cellB2.Alignment.RotationAngle = 15

Dim cellB3 As Cell = worksheet.Cells("B3")
cellB3.Value = "The Alignment.WrapText property is applied to wrap the text within a cell"
cellB3.Alignment.WrapText = True

Align Content in a Cell Range

The Formatting object properties allows you to change the alignment of the cell range.

Call the CellRange.BeginUpdateFormatting method to obtain the Formatting object. Use the Formatting.Alignment property to specify alignment options. Call the CellRange.EndUpdateFormatting method to finalize the modification.

To share alignment settings with multiple cells in a single step, create or modify the style with the Formatting.Alignment property specified as required, and assign this style to CellRange.Style for the desired cells.