对齐设置

配置对齐设置

Microsoft Excel中的对齐设置

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

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

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

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

Aspose.Cells中的对齐设置

Aspose.Cells为GetStyleSetStyle提供了Cell类的方法,用于获取和设置单元格的格式。Style类提供了有用的属性来配置对齐设置。

使用TextAlignmentType枚举选择任何文本对齐类型。TextAlignmentType枚举中预定义的文本对齐类型包括:

文本对齐类型 描述
Bottom 表示底部文本对齐
Center 表示居中文本对齐
CenterAcross 表示跨列居中文本对齐
Distributed 表示分散文本对齐
Fill 表示填充文本对齐
General 表示常规文本对齐
Justify 表示两端对齐文本对齐
Left 表示左对齐文本对齐
Right 表示右对齐文本对齐
Top 表示顶部文本对齐
JustifiedLow 用于阿拉伯文本具有调整的卡什达长度。
ThaiDistributed 分布泰文,因为每个字符被视为一个单词。

水平,垂直对齐和缩进

使用HorizontalAlignment属性水平对齐文本,使用VerticalAlignment属性垂直对齐文本。 使用IndentLevel属性可以设置单元格中文本的缩进级别。 仅在水平对齐为左对齐或右对齐时有效。

// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("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();
//Set text left horizontal alignment
style.setHorizontalAlignment(TextAlignmentType.RIGHT);
//Set indent
style.setIndentLevel(4);
//Set text top vertical alignment
style.setVerticalAlignment(TextAlignmentType.TOP);
cell.setStyle(style);
// Saving the Excel file
workbook.save("book1.xlsx");

方向

使用RotationAngle属性设置单元格中文本的方向(旋转)。

// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("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.setRotationAngle(25);
cell.setStyle(style);
//Accessing the "A2" cell from the worksheet
cell = worksheet.getCells().get("A2");
// Adding some value to the "A1" cell
cell.putValue("Visit Aspose!");
// Setting the horizontal alignment of the text in the "A2" cell
style = cell.getStyle();
// Setting the orientation of the text from top to bottom
style.setRotationAngle(255);
cell.setStyle(style);
// Saving the Excel file
workbook.save("book1.xlsx");

文本控制

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

自动换行文本

在单元格中进行文本换行可以更容易阅读:单元格的高度调整以适应所有文本,而不是截断或溢出到相邻单元格。使用IsTextWrapped属性设置文本换行开启或关闭。

// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("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
Style style = cell.getStyle();
// Wrap Cell's Text wrap
style.setTextWrapped( true);
//Set style.
cell.setStyle(style);
// Saving the Excel file
workbook.save("book1.xlsx");

缩小以适应

在字段中包装文本的一个选项是将文本尺寸缩小以适应单元格的尺寸。通过设置ShrinkToFit属性来实现这一点。设为true

// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("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.setShrinkToFit(true);
cell.setStyle(style);
// Saving the Excel file
workbook.save("book1.xlsx");

合并单元格

像Microsoft Excel一样,Aspose.Cells支持将多个单元格合并为一个。Aspose.Cells提供了两种方法来执行此任务。一种方式是调用Merge方法。该方法接受以下参数来合并单元格:

  • 第一行:开始合并的第一行。
  • 第一列:开始合并的第一列。
  • 行数:要合并的行数。
  • 列数:要合并的列数。
// Create a Cells object ot fetch all the cells.
Cells cells = worksheet.getCells();
// Merge some Cells (C6:E7) into a single C6 Cell.
cells.merge(5, 2, 2, 3);
// Input data into C6 Cell.
worksheet.getCells().get(5, 2).putValue("This is my value");
// Create a Style object to fetch the Style of C6 Cell.
Style style = worksheet.getCells().get(5, 2).getStyle();
// Create a Font object
Font font = style.getFont();
// Set the name.
font.setName("Times New Roman");
// Set the font size.
font.setSize(18);
// Set the font color
font.setColor(Color.getBlue());
// Bold the text
font.setBold(true);
// Make it italic
font.setItalic(true);
// Set the backgrond color of C6 Cell to Red
style.setForegroundColor(Color.getRed());
style.setPattern(BackgroundType.SOLID);
// Apply the Style to C6 Cell.
cells.get(5, 2).setStyle(style);

文本方向

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

阅读顺序是通过TextDirection属性进行设置。Aspose.Cells在TextDirectionType枚举中提供预定义的文本方向类型。

文本方向类型 描述
Context 与第一个输入字符的语言一致的阅读顺序
LeftToRight 从左到右的阅读顺序
RightToLeft 从右到左的阅读顺序
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.getCells().get("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.setTextDirection(TextDirectionType.LEFT_TO_RIGHT);
cell.setStyle(style);
// Saving the Excel file
workbook.save("book1.xlsx");

高级主题