Back to Devexpress

Alignment.Horizontal Property

officefileapi-devexpress-dot-spreadsheet-dot-alignment.md

latest13.0 KB
Original Source

Alignment.Horizontal Property

Gets or sets the horizontal alignment of the cell content.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
SpreadsheetHorizontalAlignment Horizontal { get; set; }
vb
Property Horizontal As SpreadsheetHorizontalAlignment

Property Value

TypeDescription
SpreadsheetHorizontalAlignment

A SpreadsheetHorizontalAlignment enumeration member specifying how cell data should be horizontally aligned.

|

Available values:

NameDescription
General

The cell content is horizontally aligned according to the data type (text is aligned to the left; numbers, date and time values are aligned to the right; boolean and error values are centered).

| | Left |

The cell content is horizontally aligned on the left.

| | Center |

The cell content is centered horizontally.

| | Right |

The cell content is horizontally aligned on the right.

| | Fill |

Repeats the cell content across the whole width of the cell.

| | Justify |

The text is wrapped to fit the cell width and each line of text (except the last line) is horizontally justified to the right and left of the cell.

| | CenterContinuous |

The cell content is centered horizontally across multiple cells.

| | Distributed |

The text is wrapped to fit the cell width and each line of text is horizontally distributed across the entire cell. This alignment type allows you to apply an indent to both the left and right sides of the cell.

|

Remarks

Use the Horizontal and Alignment.Vertical properties to specify horizontal and vertical alignment of cell data.

To set other alignment characteristics for a cell or range of cells (for example, indent, text rotation, values indicating whether the text should be wrapped and shrunk in a cell), use the corresponding properties of the Alignment object. Access this object via the Formatting.Alignment property.

For examples on how to specify formatting for an individual cell and cell range, or modify a style, refer to the How to: Format a Cell or Range of Cells or How to: Create or Modify a Style document, respectively.

Example

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

The following code snippets (auto-collected from DevExpress Examples) contain references to the Horizontal property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-spreadsheetcontrol-api-part1/CS/SpreadsheetControl/SpreadsheetActions/FormulaActions.cs#L30

csharp
worksheet.Range["A1:B1"].FillColor = Color.LightGray;
worksheet.Range["A1:B2"].Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
worksheet.Cells["A2"].Value = "'= (1+5)*6";

wpf-spreadsheetcontrol-api-part-1/CS/SpreadsheetControl_WPF_API/SpreadsheetActions/FormulaActions.cs#L30

csharp
worksheet.Range["A1:B1"].FillColor = Color.LightGray;
worksheet.Range["A1:B2"].Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
worksheet.Cells["A2"].Value = "'= (1+5)*6";

spreadsheet-document-api-examples-part1/CS/SpreadsheetExamples/SpreadsheetActions/CellActions.cs#L37

csharp
worksheet.Columns["B"].WidthInCharacters = 20;
worksheet.Range["A1:B8"].Alignment.Horizontal = SpreadsheetHorizontalAlignment.Left;

winforms-spreadsheetcontrol-api-part-2/CS/SpreadsheetControl_API_Part02/SpreadsheetActions/CustomFunctionActions.cs#L36

csharp
worksheet.Range["A1:H1"].ColumnWidthInCharacters = 12;
worksheet.Range["A1:H1"].Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;

asp-net-mvc-spreadsheet-modify-document/CS/DXWebApplication1/Controllers/HomeController.cs#L37

csharp
rangeFormatting.Alignment.Vertical = SpreadsheetVerticalAlignment.Center;
rangeFormatting.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
priceRange.EndUpdateFormatting(rangeFormatting);

wpf-spreadsheetcontrol-api-part-1/VB/SpreadsheetControl_WPF_API/SpreadsheetActions/FormulaActions.vb#L29

vb
worksheet.Range("A1:B1").FillColor = Color.LightGray
worksheet.Range("A1:B2").Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center
worksheet.Cells("A2").Value = "'= (1+5)*6"

spreadsheet-document-api-examples-part1/VB/SpreadsheetExamples/SpreadsheetActions/CellActions.vb#L40

vb
worksheet.Columns("B").WidthInCharacters = 20
            worksheet.Range("A1:B8").Alignment.Horizontal = SpreadsheetHorizontalAlignment.Left
#Region "#CellValue"

winforms-spreadsheetcontrol-api-part1/VB/SpreadsheetControl/SpreadsheetActions/FormulaActions.vb#L27

vb
worksheet.Range("A1:B1").FillColor = Color.LightGray
worksheet.Range("A1:B2").Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center
worksheet.Cells("A2").Value = "'= (1+5)*6"

spreadsheet-document-api-examples-part2/VB/SpreadsheetDocServerAPIPart2/CodeExamples/TableActions.vb#L47

vb
' Specify horizontal alignment for the header and total rows.
table.HeaderRowRange.Alignment.Horizontal = DevExpress.Spreadsheet.SpreadsheetHorizontalAlignment.Center
table.TotalRowRange.Alignment.Horizontal = DevExpress.Spreadsheet.SpreadsheetHorizontalAlignment.Center

spreadsheet-document-api-import-data-from-data-sources/VB/DataImportExample/Form1.vb#L136

vb
' Specify horizontal alignment for header and total rows of the table.
table.HeaderRowRange.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center
table.TotalRowRange.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center

See Also

Vertical

Indent

WrapText

ShrinkToFit

RotationAngle

Spreadsheet Document API Examples

Alignment Interface

Alignment Members

DevExpress.Spreadsheet Namespace