边框设置
向单元格添加边框
Microsoft Excel 允许用户通过添加边框来格式化单元格。边框的类型取决于添加的位置。例如,顶部边框是添加到单元格顶部位置的边框。用户还可以修改边框的线条样式和颜色。
使用Aspose.Cells for Python via .NET,开发者可以像在Microsoft Excel中一样添加边框并自定义其外观。
向单元格添加边框
Aspose.Cells for Python via .NET提供一个类 Workbook,代表一个Microsoft Excel文件。Workbook 类包含一个 worksheets 集合,允许访问Excel文件中的每个工作表。工作表由 Worksheet 类表示。Worksheet 类提供一个 Cells 集合。Cells 集合中的每个项目代表一个 Cell 类的对象。
Aspose.Cells for Python via .NET在 Cell 类中提供 get_style 方法。set_style 方法用于设置单元格的格式样式。Style 类提供添加边框到单元格的属性。
向单元格添加边框
开发人员可以通过使用 Style 对象的 borders 集合向单元格添加边框。边框类型作为索引传递给 borders 集合。所有边框类型都在 BorderType 枚举中预定义。
边框枚举
边框类型 | 描述 |
---|---|
BOTTOM_BORDER | 底部边框线 |
DIAGONAL_DOWN | 从左上到右下的对角线 |
DIAGONAL_UP | 从左下到右上的对角线 |
LEFT_BORDER | 左边边框线 |
RIGHT_BORDER | 右边边框线 |
TOP_BORDER | 顶部边框线 |
The borders collection stores all borders. Each border in the Borders collection is represented by a Border object which provides two properties, color and line_style to set a border’s line color and style respectively.
要设置边框线颜色,请使用.NET Framework的Color枚举(.NET Framework的一部分)选择颜色并将其分配给Border对象的Color属性。
通过从CellBorderType枚举中选择线条样式来设置边框线样式。
CellBorderType枚举
线条样式 | 描述 |
---|---|
DASH_DOT | 细虚线 |
DASH_DOT_DOT | 细点虚线 |
DASHED | 虚线 |
DOTTED | 点线 |
DOUBLE | 双线 |
HAIR | 细线 |
MEDIUM_DASH_DOT | 中虚线 |
MEDIUM_DASH_DOT_DOT | 中点虚线 |
MEDIUM_DASHED | 中虚线 |
NONE | 无线条 |
MEDIUM | 中线 |
SLANTED_DASH_DOT | 斜虚线 |
THICK | 加粗线 |
THIN | 细线 |
选择其中一种线条样式,然后将其分配给“Border”对象的line_style属性。 |
from aspose.cells import BorderType, CellBorderType, Workbook | |
from aspose.pydrawing import Color | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Obtaining the reference of the first (default) worksheet by passing its sheet index | |
worksheet = workbook.worksheets[0] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Visit Aspose!") | |
# Create a style object | |
style = cell.get_style() | |
# Setting the line style of the top border | |
style.borders.get(BorderType.TOP_BORDER).line_style = CellBorderType.THICK | |
# Setting the color of the top border | |
style.borders.get(BorderType.TOP_BORDER).color = Color.black | |
# Setting the line style of the bottom border | |
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.THICK | |
# Setting the color of the bottom border | |
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.black | |
# Setting the line style of the left border | |
style.borders.get(BorderType.LEFT_BORDER).line_style = CellBorderType.THICK | |
# Setting the color of the left border | |
style.borders.get(BorderType.LEFT_BORDER).color = Color.black | |
# Setting the line style of the right border | |
style.borders.get(BorderType.RIGHT_BORDER).line_style = CellBorderType.THICK | |
# Setting the color of the right border | |
style.borders.get(BorderType.RIGHT_BORDER).color = Color.black | |
# Apply the border styles to the cell | |
cell.set_style(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls") |
向单元格范围添加边框
还可以为一系列的单元格添加边框,而不仅仅是单个单元格。要实现这一点,首先通过调用“Cells”集合的create_range方法创建一系列单元格。它使用以下参数:
- 第一行,范围的第一行。
- 第一列,表示范围的第一列。
- 行数,范围中的行数。
- 列数,范围中的列数。
“create_range”方法返回一个Range对象,它包含指定范围的单元格。Range对象提供一个set_outline_border方法,使用以下参数为单元格范围添加边框:
- 边框类型,从BorderType枚举中选择的边框类型。
- 线条样式,从CellBorderType枚举中选择的边框线条样式。
- 颜色,线条颜色,从Color枚举中选择。
from aspose.cells import BorderType, CellBorderType, Workbook | |
from aspose.pydrawing import Color | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Obtaining the reference of the first (default) worksheet by passing its sheet index | |
worksheet = workbook.worksheets[0] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Hello World From Aspose") | |
# Creating a range of cells starting from "A1" cell to 3rd column in a row | |
range = worksheet.cells.create_range(0, 0, 1, 3) | |
# Adding a thick top border with blue line | |
range.set_outline_border(BorderType.TOP_BORDER, CellBorderType.THICK, Color.blue) | |
# Adding a thick bottom border with blue line | |
range.set_outline_border(BorderType.BOTTOM_BORDER, CellBorderType.THICK, Color.blue) | |
# Adding a thick left border with blue line | |
range.set_outline_border(BorderType.LEFT_BORDER, CellBorderType.THICK, Color.blue) | |
# Adding a thick right border with blue line | |
range.set_outline_border(BorderType.RIGHT_BORDER, CellBorderType.THICK, Color.blue) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls") |