对齐设置
配置对齐设置
Microsoft Excel 中的对齐设置
用过Microsoft Excel格式化单元格的人都会熟悉Microsoft Excel中的对齐设置。
从上图可以看出,有不同种类的对齐选项:
- 文本对齐方式(水平和垂直)
- 缩进。
- 方向。
- 文字控制。
- 文字方向。
Aspose.Cells 完全支持所有这些对齐设置,并在下面进行更详细的讨论。
Aspose.Cells 中的对齐设置
Aspose.Cells提供获取样式和设置样式的方法Cell用于获取和设置单元格格式的类。这风格类提供用于配置对齐设置的有用属性。
使用文本对齐类型枚举。中的预定义文本对齐类型文本对齐类型枚举是:
文本对齐类型 | 描述 |
---|---|
底部 | 表示底部文本对齐 |
中心 | 表示居中文本对齐 |
跨中心 | 表示跨文本居中对齐 |
分散式 | 表示分布式文本对齐 |
充满 | 表示填充文本对齐方式 |
一般的 | 表示一般文本对齐方式 |
证明合法 | 表示对齐文本对齐 |
剩下 | 表示文本左对齐 |
正确的 | 表示文本右对齐 |
最佳 | 表示顶部文本对齐 |
合理的低 | 将文本与调整后的阿拉伯文本 kashida 长度对齐。 |
泰国分布式 | 特别分发泰语文本,因为每个字符都被视为一个单词。 |
水平、垂直对齐和缩进
使用水平对齐水平对齐文本的属性和垂直对齐垂直对齐文本的属性。 可以设置单元格中文本的缩进级别缩进级别财产 而 tt 仅在水平对齐为左或右时有效。
// 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"); |
方向
设置单元格中文本的方向(旋转)旋转角度财产。
// 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"); |
收缩以适应
在字段中换行文本的一个选项是缩小文本大小以适合单元格的尺寸。这是通过设置缩小到适合财产。到真的.
// 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"); | |
合并 Cells
与Microsoft Excel一样,Aspose.Cells支持将多个单元格合并为一个单元格。 Aspose.Cells 提供了两种方法来完成这项任务。一种方法是调用[合并](https://reference.aspose.com/cells/java/com.aspose.cells/cells#merge(int,%20int,%20int,%20int)) 方法。该方法采用以下参数来合并单元格:
- 第一行:从哪里开始合并的第一行。
- 第一列:从哪里开始合并的第一列。
- 行数:要合并的行数。
- 列数:要合并的列数。
// 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); | |
文字方向
可以设置单元格中文本的阅读顺序。阅读顺序是字符、单词等显示的视觉顺序。例如,英语是从左到右的语言,而阿拉伯语是从右到左的语言。
阅读顺序设置为文本方向财产。 Aspose.Cells 提供预定义的文本方向类型文本方向类型枚举。
文本方向类型 | 描述 |
---|---|
语境 | 与第一个输入字符的语言一致的阅读顺序 |
左到右 | 从左到右的阅读顺序 |
右到左 | 从右到左的阅读顺序 |
// 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"); |