若要对齐单元格中包含的数据,请使用 Alignment 对象的属性。
若要访问此对象以修改单个单元格内容的对齐方式,请使用 Cell 对象的 Formatting.Alignment 属性,该属性继承自 Formatting 接口。
若要更改单元格区域的对齐属性,请调用此区域的 CellRange.BeginUpdateFormatting 方法,使用返回的 Formatting 对象的 Alignment 属性访问和修改 Alignment 对象,并调用 CellRange.EndUpdateFormatting 方法完成修改。
Alignment 对象提供以下属性来更改单元格对齐方式设置:
若要在单个步骤中与多个单元格共享对齐设置,请使用根据需要指定的 Formatting.Alignment 属性创建或修改样式,并将此样式分配给所需单元格的 CellRange.Style。
此示例演示如何通过修改通过 Cell 对象的 Formatting.Alignment 属性访问的 Alignment 对象来指定单元格内容的对齐方式。
Vb.Net |
|
C# |
DevExpress.Spreadsheet.Cell cellA1 = worksheet.Cells["A1"]; cellA1.Value = "Right and top"; cellA1.Alignment.Horizontal = DevExpress.Spreadsheet.SpreadsheetHorizontalAlignment.Right; cellA1.Alignment.Vertical = DevExpress.Spreadsheet.SpreadsheetVerticalAlignment.Top; DevExpress.Spreadsheet.Cell cellA2 = worksheet.Cells["A2"]; cellA2.Value = "Center"; cellA2.Alignment.Horizontal = DevExpress.Spreadsheet.SpreadsheetHorizontalAlignment.Center; cellA2.Alignment.Vertical = DevExpress.Spreadsheet.SpreadsheetVerticalAlignment.Center; DevExpress.Spreadsheet.Cell cellA3 = worksheet.Cells["A3"]; cellA3.Value = "Left and bottom, indent"; cellA3.Alignment.Indent = 1; DevExpress.Spreadsheet.Cell cellB1 = worksheet.Cells["B1"]; cellB1.Value = "The Alignment.ShrinkToFit property is applied"; cellB1.Alignment.ShrinkToFit = true; DevExpress.Spreadsheet.Cell cellB2 = worksheet.Cells["B2"]; cellB2.Value = "Rotated Cell Contents"; cellB2.Alignment.Horizontal = DevExpress.Spreadsheet.SpreadsheetHorizontalAlignment.Center; cellB2.Alignment.Vertical = DevExpress.Spreadsheet.SpreadsheetVerticalAlignment.Center; cellB2.Alignment.RotationAngle = 15; DevExpress.Spreadsheet.Cell cellB3 = worksheet.Cells["B3"]; cellB3.Value = "The Alignment.WrapText property is applied to wrap the text within a cell"; cellB3.Alignment.WrapText = true; |
下图显示了如何在工作表单元格中对齐文本(工作簿在 Microsoft® Excel® 中打开)。