XlCellAlignment.FromHV(XlHorizontalAlignment, XlVerticalAlignment) Method
Creates the XlCellAlignment object using the specified horizontal and vertical alignment settings.
Namespace: DevExpress.Export.Xl
Assembly: DevExpress.Printing.v24.1.Core.dll
NuGet Package: DevExpress.Printing.Core
Declaration
public static XlCellAlignment FromHV(
XlHorizontalAlignment horizontalAlignment,
XlVerticalAlignment verticalAlignment
)
Parameters
Name | Type | Description |
---|---|---|
horizontalAlignment | XlHorizontalAlignment | An XlHorizontalAlignment enumeration member specifying how cell data should be horizontally aligned. |
verticalAlignment | XlVerticalAlignment | An XlVerticalAlignment enumeration member specifying how cell data should be vertically aligned. |
Returns
Type | Description |
---|---|
XlCellAlignment | An XlCellAlignment class instance. |
Remarks
Use the FromHV method to create an instance of the XlCellAlignment class that specifies both horizontal and vertical alignment for a cell. To align data in a cell either horizontally or vertically, use the XlCellAlignment.HorizontalAlignment and XlCellAlignment.VerticalAlignment properties, respectively. Using the FromHV method automatically sets these properties to the values specified by the horizontalAlignment and verticalAlignment parameters.
You can also use other properties of the XlCellAlignment object to specify additional alignment characteristics for a cell (e.g., indent, text rotation, values indicating whether the text should be wrapped or shrunk in a cell).
To apply the specified alignment settings to a cell, pass the 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 };
}
}
}
Related GitHub Examples
The following code snippets (auto-collected from DevExpress Examples) contain references to the FromHV(XlHorizontalAlignment, XlVerticalAlignment) method.
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.