对齐设置
配置对齐设置
Microsoft Excel中的对齐设置
任何使用Microsoft Excel格式化单元格的人都会熟悉Microsoft Excel中的对齐设置。
从上面的图中可以看出,有不同种类的对齐选项:
- 文本对齐(水平和垂直)
- 缩进
- 方向
- 文本控制
- 文本方向
Aspose.Cells完全支持所有这些对齐设置,并在下面详细讨论。
Aspose.Cells中的对齐设置
Aspose.Cells提供一个表示Excel文件的类Workbook。Workbook类包含一个Worksheets集合,允许访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供Cells集合。Cells集合中的每个项表示Cell类对象。
Aspose.Cells为GetStyle和SetStyle提供了Cell类的方法,用于获取和设置单元格的格式。Style类提供了有用的属性来配置对齐设置。
使用TextAlignmentType枚举选择任何文本对齐类型。TextAlignmentType枚举中预定义的文本对齐类型包括:
文本对齐类型 | 描述 |
---|---|
Bottom | 表示底部文本对齐 |
Center | 表示居中文本对齐 |
CenterAcross | 表示跨列居中文本对齐 |
Distributed | 表示分散文本对齐 |
Fill | 表示填充文本对齐 |
General | 表示常规文本对齐 |
Justify | 表示两端对齐文本对齐 |
Left | 表示左对齐文本对齐 |
Right | 表示右对齐文本对齐 |
Top | 表示顶部文本对齐 |
JustifiedLow | 用于阿拉伯文本具有调整的卡什达长度。 |
ThaiDistributed | 分布泰文,因为每个字符被视为一个单词。 |
水平对齐
使用Style对象的HorizontalAlignment属性使文本水平对齐。
// 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); |
垂直对齐
与水平对齐类似,使用 Style 对象的 VerticalAlignment 属性来垂直对齐文本。
// 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); |
缩进
可以使用 Style 对象的 IndentLevel 属性来设置单元格中文本的缩进级别。
// 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); |
方向
使用 Style 对象的 RotationAngle 属性来设置单元格中文本的方向(旋转)。
// 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); |
文本控制
以下部分讨论了如何通过设置文本换行、缩小以适应和其他格式设置选项来控制文本。
自动换行文本
在单元格中设置文本换行可使其更易读:单元格的高度会根据文本内容自动调整,而不会被截断或溢出到相邻单元格。可以使用 Style 对象的 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"); |
缩小以适应
在单元格中设置文本字段换行的选项是缩小文本大小以适应单元格尺寸,可以通过将 Style 对象的 IsTextWrapped 属性设置为 true 来实现。
// 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); |
合并单元格
与 Microsoft Excel 一样,Aspose.Cells 支持将多个单元格合并为一个。Aspose.Cells 提供两种方法来完成此任务。一种方法是调用 Cells 集合的 Merge 方法。Merge 方法接受以下参数来合并单元格:
- 第一行:开始合并的第一行。
- 第一列:开始合并的第一列。
- 行数:要合并的行数。
- 列数:要合并的列数。
// 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 集合的 CreateRange 方法来创建要合并的单元格范围。CreateRange 方法接受与上述 Merge 方法相同的参数集,并返回一个 Range 对象。Range 对象还提供一个 Merge 方法,该方法合并 Range 对象中指定的范围。
文本方向
可以设置单元格中文本的阅读顺序。阅读顺序是以字符、单词等显示的视觉顺序。例如,英语是从左到右的语言,而阿拉伯语是从右到左的语言。
阅读顺序是由 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.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"); |