Back to Devexpress

How to: Align Cell Content

wpf-16385-controls-and-libraries-spreadsheet-examples-formatting-how-to-align-cell-content.md

latest2.4 KB
Original Source

How to: Align Cell Content

  • Jun 07, 2019
  • 2 minutes to read

This example demonstrates how to specify the alignment of cell content by modifying the Alignment object accessed via the Formatting.Alignment property of the Cell object.

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 = 2;

Cell cellA4 = worksheet.Cells["A4"];
cellA4.Value = "The Alignment.WrapText property is applied to wrap the text within a cell";
cellA4.Alignment.WrapText = true;

Cell cellA5 = worksheet.Cells["A5"];
cellA5.Value = "Rotation by 45 degrees";
cellA5.Alignment.RotationAngle = 45;
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 = 2

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

Dim cellA5 As Cell = worksheet.Cells("A5") 
cellA5.Value = "Rotation by 45 degrees"
cellA5.Alignment.RotationAngle = 45

The image below shows how text can be aligned within worksheet cells.