Skip to main content

XlCellAlignment Class

Contains alignment settings for a cell.

Namespace: DevExpress.Export.Xl

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

NuGet Package: DevExpress.Printing.Core

Declaration

public class XlCellAlignment :
    ISupportsCopyFrom<XlCellAlignment>

Remarks

The XlCellAlignment object provides a set of properties that you can use to align data contained within a cell (XlCellAlignment.HorizontalAlignment, XlCellAlignment.VerticalAlignment, XlCellAlignment.Indent, XlCellAlignment.TextRotation, XlCellAlignment.WrapText, etc.).

To define alignment settings for the cell content, create an instance of the XlCellAlignment class, and then set the required properties of the created object. You can also use the static XlCellAlignment.FromHV method to specify both the horizontal and vertical alignment for a cell simultaneously.

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

To share alignment settings with multiple cells in a row at once, use the IXlRow.BulkCells method.

To specify alignment characteristics for the entire row or column, use the IXlRow.ApplyFormatting and IXlColumn.ApplyFormatting methods, or IXlRow.Formatting and IXlColumn.Formatting properties, respectively.

For an example on how to specify alignment options for a cell, refer to the How to: Align Cell Content article.

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 };
        }
    }
}

Inheritance

Object
XlCellAlignment
See Also