Skip to main content

XlCellAlignment.HorizontalAlignment Property

Gets or sets the horizontal alignment of the cell content.

Namespace: DevExpress.Export.Xl

Assembly: DevExpress.Printing.v24.1.Core.dll

NuGet Package: DevExpress.Printing.Core

Declaration

public XlHorizontalAlignment HorizontalAlignment { get; set; }

Property Value

Type Description
XlHorizontalAlignment

An XlHorizontalAlignment enumeration member specifying how cell data should be horizontally aligned.

Available values:

Name Description
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 to the left edge of the cell.

Center

The cell content is centered horizontally across the cell.

Right

The cell content is horizontally aligned to the right edge of the cell.

Fill

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

Justify

The text is wrapped to fully fit into 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 into the cell width and each line of text is horizontally distributed across the full width of the cell. This alignment type applies an indent to both the left and right sides of the cell.

Remarks

Use the HorizontalAlignment property to specify the position of the cell content between the left and right borders of a cell. The XlCellAlignment.VerticalAlignment property allows you to align data between the cell’s top and bottom borders. You can also use the static XlCellAlignment.FromHV method to define both horizontal and vertical alignment of cell data at the same time.

To set other alignment characteristics for an individual cell (e.g., indent, text rotation, values indicating whether the text should be wrapped and shrunk in a cell), use the corresponding properties of the XlCellAlignment object.

To apply the alignment settings to a cell, pass the specified XlCellAlignment object to the IXlCell.ApplyFormatting method as a parameter, or assign it to the IXlCell.Formatting property.

For more information on how to align data contained in a cell, refer to the How to: Align Cell Content topic.

Example

Note

A complete sample project is available at https://github.com/DevExpress-Examples/excel-export-api-examples

// Create a worksheet.
using(IXlSheet sheet = document.CreateSheet()) {

    // Create three successive columns and set their widths.
    for(int i = 0; i < 3; i++) {
        using(IXlColumn column = sheet.CreateColumn()) {
            column.WidthInPixels = 130;
        }
    }

    // Create the first row in the worksheet.
    using(IXlRow row = sheet.CreateRow()) {
        // Set the row height.
        row.HeightInPixels = 40;
        // Create the first cell in the row.
        using(IXlCell cell = row.CreateCell()) {
            // Set the cell value.
            cell.Value = "Left and Top";
            // Specify the horizontal and vertical alignment of the cell content.
            cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Left, XlVerticalAlignment.Top));
        }
        // Create the second cell in the row.
        using(IXlCell cell = row.CreateCell()) {
            // Set the cell value.
            cell.Value = "Center and Top";
            // Specify the horizontal and vertical alignment of the cell content.
            cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Center, XlVerticalAlignment.Top));
        }
        // Create the third cell in the row.
        using(IXlCell cell = row.CreateCell()) {
            // Set the cell value.
            cell.Value = "Right and Top";
            // Specify the horizontal and vertical alignment of the cell content.
            cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Right, XlVerticalAlignment.Top));
        }
    }

    // Create the second row in the worksheet.
    using(IXlRow row = sheet.CreateRow()) {
        // Set the row height.
        row.HeightInPixels = 40;
        // Create the first cell in the row.
        using(IXlCell cell = row.CreateCell()) {
            // Set the cell value.
            cell.Value = "Left and Center";
            // Specify the horizontal and vertical alignment of the cell content.
            cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Left, XlVerticalAlignment.Center));
        }
        // Create the second cell in the row.
        using(IXlCell cell = row.CreateCell()) {
            // Set the cell value.
            cell.Value = "Center and Center";
            // Specify the horizontal and vertical alignment of the cell content.
            cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Center, XlVerticalAlignment.Center));
        }
        // Create the third cell in the row.
        using(IXlCell cell = row.CreateCell()) {
            // Set the cell value.
            cell.Value = "Right and Center";
            // Specify the horizontal and vertical alignment of the cell content.
            cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Right, XlVerticalAlignment.Center));
        }
    }

    // Create the third row in the worksheet.
    using(IXlRow row = sheet.CreateRow()) {
        // Set the row height.
        row.HeightInPixels = 40;
        // Create the first cell in the row.
        using(IXlCell cell = row.CreateCell()) {
            // Set the cell value.
            cell.Value = "Left and Bottom";
            // Specify the horizontal and vertical alignment of the cell content.
            cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Left, XlVerticalAlignment.Bottom));
        }
        // Create the second cell in the row.
        using(IXlCell cell = row.CreateCell()) {
            // Set the cell value.
            cell.Value = "Center and Bottom";
            // Specify the horizontal and vertical alignment of the cell content.
            cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Center, XlVerticalAlignment.Bottom));
        }
        // Create the third cell in the row.
        using(IXlCell cell = row.CreateCell()) {
            // Set the cell value.
            cell.Value = "Right and Bottom";
            // Specify the horizontal and vertical alignment of the cell content.
            cell.ApplyFormatting(XlCellAlignment.FromHV(XlHorizontalAlignment.Right, XlVerticalAlignment.Bottom));
        }
    }

    sheet.SkipRows(1);

    // Create the fifth row in the worksheet.
    using(IXlRow row = sheet.CreateRow()) {
        // Create the first cell in the row.
        using(IXlCell cell = row.CreateCell()) {
            // Set the cell value.
            cell.Value = "The WrapText property is applied to wrap the text within a cell";
            // Wrap the text within the cell.
            cell.Formatting = new XlCellAlignment() { WrapText = true };
        }
        // Create the second cell in the row.
        using(IXlCell cell = row.CreateCell()) {
            // Set the cell value.
            cell.Value = "Indented text";
            // Set the indentation of the cell content.
            cell.Formatting = new XlCellAlignment() { Indent = 2 };
        }
        // Create the third cell in the row.
        using(IXlCell cell = row.CreateCell()) {
            // Set the cell value.
            cell.Value = "Rotated text";
            // Rotate the text within the cell.
            cell.Formatting = new XlCellAlignment() { TextRotation = 90 };
        }
    }
}

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the HorizontalAlignment 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.

See Also