对齐设置

配置对齐设置

Microsoft Excel 中的对齐设置

用过Microsoft Excel格式化单元格的人都会熟悉Microsoft Excel中的对齐设置。

从上图可以看出,有不同种类的对齐选项:

  • 文本对齐方式(水平和垂直)
  • 缩进。
  • 方向。
  • 文字控制。
  • 文字方向。

Aspose.Cells 完全支持所有这些对齐设置,并在下面进行更详细的讨论。

Aspose.Cells 中的对齐设置

Aspose.Cells提供了一个类,工作簿 , 表示一个 Excel 文件。这工作簿类包含一个工作表允许访问 Excel 文件中每个工作表的集合。工作表由工作表班级。这工作表类提供了Cells收藏。中的每一项Cells集合代表一个对象Cell班级。

Aspose.Cells提供获取样式设置样式的方法Cell用于获取和设置单元格格式的类。这风格类提供用于配置对齐设置的有用属性。

使用文本对齐类型枚举。中的预定义文本对齐类型文本对齐类型枚举是:

文本对齐类型 描述
底部 表示底部文本对齐
中心 表示居中文本对齐
跨中心 表示跨文本居中对齐
分散式 表示分布式文本对齐
充满 表示填充文本对齐方式
一般的 表示一般文本对齐方式
证明合法 表示对齐文本对齐
剩下 表示文本左对齐
正确的 表示文本右对齐
最佳 表示顶部文本对齐
合理的低 将文本与调整后的阿拉伯文本 kashida 长度对齐。
泰国分布式 特别分发泰语文本,因为每个字符都被视为一个单词。

水平对齐

使用风格对象的水平对齐水平对齐文本的属性。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
Style style = cell.GetStyle();
style.HorizontalAlignment = TextAlignmentType.Center;
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

垂直对齐

类似于水平对齐,使用风格对象的垂直对齐垂直对齐文本的属性。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Clearing all the worksheets
workbook.Worksheets.Clear();
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
Style style = cell.GetStyle();
// Setting the vertical alignment of the text in a cell
style.VerticalAlignment = TextAlignmentType.Center;
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

缩进

可以设置单元格中文本的缩进级别风格对象的缩进级别财产。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
Style style = cell.GetStyle();
// Setting the indentation level of the text (inside the cell) to 2
style.IndentLevel = 2;
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

方向

设置单元格中文本的方向(旋转)风格对象的旋转角度财产。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
Style style = cell.GetStyle();
// Setting the rotation of the text (inside the cell) to 25
style.RotationAngle = 25;
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

文字控制

以下部分讨论如何通过设置文本换行、收缩以适合和其他格式设置选项来控制文本。

环绕文字

在单元格中环绕文本使其更易于阅读:单元格的高度会调整以适合所有文本,而不是将其切断或溢出到相邻的单元格中。使用风格对象的IsTextWrapped财产。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create Workbook Object
Workbook wb = new Workbook();
// Open first Worksheet in the workbook
Worksheet ws = wb.Worksheets[0];
// Get Worksheet Cells Collection
Aspose.Cells.Cells cell = ws.Cells;
// Increase the width of First Column Width
cell.SetColumnWidth(0, 35);
// Increase the height of first row
cell.SetRowHeight(0, 36);
// Add Text to the Firts Cell
cell[0, 0].PutValue("I am using the latest version of Aspose.Cells to test this functionality");
// Make Cell's Text wrap
Style style = cell[0, 0].GetStyle();
style.IsTextWrapped = true;
cell[0, 0].SetStyle(style);
// Save Excel File
wb.Save(dataDir+ "WrappingText.out.xlsx");
收缩以适应

在字段中换行文本的一个选项是缩小文本大小以适合单元格的尺寸。这是通过设置风格对象的IsTextWrapped财产给真的.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A1" cell
Style style = cell.GetStyle();
// Shrinking the text to fit according to the dimensions of the cell
style.ShrinkToFit = true;
cell.SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);
合并 Cells

与Microsoft Excel一样,Aspose.Cells支持将多个单元格合并为一个单元格。 Aspose.Cells 提供了两种方法来完成这项任务。一种方法是调用Cells收藏的合并方法。这合并方法采用以下参数来合并单元格:

  • 第一行:从哪里开始合并的第一行。
  • 第一列:从哪里开始合并的第一列。
  • 行数:要合并的行数。
  • 列数:要合并的列数。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Create a Workbook.
Workbook wbk = new Workbook();
// Create a Worksheet and get the first sheet.
Worksheet worksheet = wbk.Worksheets[0];
// Create a Cells object ot fetch all the cells.
Cells cells = worksheet.Cells;
// Merge some Cells (C6:E7) into a single C6 Cell.
cells.Merge(5, 2, 2, 3);
// Input data into C6 Cell.
worksheet.Cells[5, 2].PutValue("This is my value");
// Create a Style object to fetch the Style of C6 Cell.
Style style = worksheet.Cells[5, 2].GetStyle();
// Create a Font object
Font font = style.Font;
// Set the name.
font.Name = "Times New Roman";
// Set the font size.
font.Size = 18;
// Set the font color
font.Color = System.Drawing.Color.Blue;
// Bold the text
font.IsBold = true;
// Make it italic
font.IsItalic = true;
// Set the backgrond color of C6 Cell to Red
style.ForegroundColor = System.Drawing.Color.Red;
style.Pattern = BackgroundType.Solid;
// Apply the Style to C6 Cell.
cells[5, 2].SetStyle(style);
// Save the Workbook.
wbk.Save(dataDir + "mergingcells.out.xls");

另一种方法是先调用Cells收藏的创建范围方法来创建要合并的单元格范围。这创建范围方法采用与合并上面讨论的方法并返回一个范围目的。这范围对象还提供了一个合并合并指定范围的方法范围目的。

文字方向

可以设置单元格中文本的阅读顺序。阅读顺序是字符、单词等显示的视觉顺序。例如,英语是从左到右的语言,而阿拉伯语是从右到左的语言。

阅读顺序设置为风格对象的文本方向财产。 Aspose.Cells 提供预定义的文本方向类型文本方向类型枚举。

文本方向类型 描述
语境 与第一个输入字符的语言一致的阅读顺序
左到右 从左到右的阅读顺序
右到左 从右到左的阅读顺序
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("I am using the latest version of Aspose.Cells to test this functionality.");
// Gets style in the "A1" cell
Style style = cell.GetStyle();
// Shrinking the text to fit according to the dimensions of the cell
style.TextDirection = (TextDirectionType.LeftToRight);
cell.SetStyle(style);
// Saving the Excel file
workbook.Save("book1.xlsx");

推进主题