填充设置
颜色和背景图案
Microsoft Excel可以设置单元格的前景(轮廓)和背景(填充)颜色以及背景图案。
Aspose.Cells for Python via .NET 还能以灵活的方式支持这些功能。在本主题中,我们学习如何使用这些功能。
设置颜色和背景图案
Aspose.Cells for Python via .NET提供了Workbook类,代表一个Microsoft Excel文件。Workbook类包含一个worksheets集合,允许访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供一个Cells集合,集合中的每个项目代表一个Cell类的对象。
Cell 具有用于获取和设置单元格格式的 get_style 和 set_style 方法。Style 类提供属性,用于设置单元格的前景色和背景色。Aspose.Cells for Python via .NET 提供一个 BackgroundType 枚举,包含一组预定义的背景图案类型,详见如下内容。
背景图案 | 描述 |
---|---|
DIAGONAL_CROSSHATCH | 表示对角线交叉图案 |
DIAGONAL_STRIPE | 表示对角线条纹图案 |
GRAY6 | 表示 6.25% 灰色图案 |
GRAY12 | 表示 12.5% 灰色图案 |
GRAY25 | 表示 25% 灰色图案 |
GRAY50 | 表示 50% 灰色图案 |
GRAY75 | 表示 75% 灰色图案 |
HORIZONTAL_STRIPE | 表示水平条纹图案 |
NONE | 表示无背景 |
REVERSE_DIAGONAL_STRIPE | 表示反向对角线条纹图案 |
SOLID | 表示纯色图案 |
THICK_DIAGONAL_CROSSHATCH | 表示粗对角线交叉图案 |
THIN_DIAGONAL_CROSSHATCH | 表示细对角线交叉图案 |
THIN_DIAGONAL_STRIPE | 表示细对角线条纹图案 |
THIN_HORIZONTAL_CROSSHATCH | 表示细水平交叉图案 |
THIN_HORIZONTAL_STRIPE | 表示细水平条纹图案 |
THIN_REVERSE_DIAGONAL_STRIPE | 代表细线反斜线条纹样式 |
THIN_VERTICAL_STRIPE | 代表细竖条纹样式 |
VERTICAL_STRIPE | 代表竖条纹样式 |
在下面的示例中,A1单元格的前景色已设置,但A2配置为具有前景色和背景色的垂直条纹背景模式。
from aspose.cells import BackgroundType, SaveFormat, 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() | |
# Adding a new worksheet to the Workbook object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[i] | |
# Define a Style and get the A1 cell style | |
style = worksheet.cells.get("A1").get_style() | |
# Setting the foreground color to yellow | |
style.foreground_color = Color.yellow | |
# Setting the background pattern to vertical stripe | |
style.pattern = BackgroundType.VERTICAL_STRIPE | |
# Apply the style to A1 cell | |
worksheet.cells.get("A1").set_style(style) | |
# Get the A2 cell style | |
style = worksheet.cells.get("A2").get_style() | |
# Setting the foreground color to blue | |
style.foreground_color = Color.blue | |
# Setting the background color to yellow | |
style.background_color = Color.yellow | |
# Setting the background pattern to vertical stripe | |
style.pattern = BackgroundType.VERTICAL_STRIPE | |
# Apply the style to A2 cell | |
worksheet.cells.get("A2").set_style(style) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003) |
重要知识
- 若要设置单元格的前景色或背景色,请使用 Style 对象的 foreground_color 或 background_color 属性。只有当 Style 对象的 pattern 属性配置好时,这两个属性才会生效。
- foreground_color 属性设置单元格的阴影颜色。 属性 pattern 指定用于前景色或背景色的背景图案类型。Aspose.Cells for Python via .NET 提供了一个枚举 BackgroundType,其中包含一组预定义的背景图案类型。
- 如果从 BackgroundType 枚举中选择 BackgroundType.None 值,则不会应用前景色。 同样,如果选择 BackgroundType.None 或 BackgroundType.Solid 值,则不会应用背景色。
- 在检索单元格的阴影/填充颜色时,如果 Style.pattern 是 BackgroundType.None,Style.foreground_color 将返回 Color.Empty。
应用梯度填充效果
要将期望的渐变填充效果应用于单元格,相应地使用 Style 对象的 set_two_color_gradient 方法。
from aspose.cells import TextAlignmentType, Workbook | |
from aspose.cells.drawing import GradientStyleType | |
from aspose.pydrawing import Color | |
# 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(".") | |
# Instantiate a new Workbook | |
workbook = Workbook() | |
# Get the first worksheet (default) in the workbook | |
worksheet = workbook.worksheets[0] | |
# Input a value into B3 cell | |
worksheet.cells.get(2, 1).put_value("test") | |
# Get the Style of the cell | |
style = worksheet.cells.get("B3").get_style() | |
# Set Gradient pattern on | |
style.is_gradient = True | |
# Specify two color gradient fill effects | |
style.set_two_color_gradient(Color.from_argb(255, 255, 255), Color.from_argb(79, 129, 189), GradientStyleType.HORIZONTAL, 1) | |
# Set the color of the text in the cell | |
style.font.color = Color.red | |
# Specify horizontal and vertical alignment settings | |
style.horizontal_alignment = TextAlignmentType.CENTER | |
style.vertical_alignment = TextAlignmentType.CENTER | |
# Apply the style to the cell | |
worksheet.cells.get("B3").set_style(style) | |
# Set the third row height in pixels | |
worksheet.cells.set_row_height_pixel(2, 53) | |
# Merge the range of cells (B3:C3) | |
worksheet.cells.merge(2, 1, 1, 2) | |
# Save the Excel file | |
workbook.save(dataDir + "output.xlsx") |
颜色和调色板
调色板是在创建图像时可用的颜色数量。在演示文稿中使用标准调色板可以让用户创建一致的外观。每个Microsoft Excel(97-2003)文件都有一个包含可应用于单元格、字体、网格线、图形对象、填充和图表中的线条的56种颜色的调色板。
使用 Aspose.Cells for Python via .NET,不仅可以使用调色板中的现有颜色,还可以使用自定义颜色。在使用自定义颜色之前,首先将其添加到调色板中。
本主题讨论如何向调色板中添加自定义颜色。
向调色板中添加自定义颜色
Aspose.Cells for Python via .NET 支持微软 Excel 的 56 种调色板。若要使用调色板中未定义的自定义颜色,请将其添加到调色板中。
Aspose.Cells for Python via .NET 提供了一个类 Workbook,表示微软 Excel 文件。Workbook 类提供了一个 change_palette 方法,带有以下参数,用于添加自定义颜色以修改调色板:
- Custom Color,要添加的自定义颜色。
- Index,自定义颜色在调色板中的索引,将替换指定的颜色。应该在 0-55 之间。
下面的示例在应用于字体之前向调色板中添加了自定义颜色(兰花紫)。
from aspose.cells import SaveFormat, 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 an Workbook object | |
workbook = Workbook() | |
# Adding Orchid color to the palette at 55th index | |
workbook.change_palette(Color.orchid, 55) | |
# Adding a new worksheet to the Excel object | |
i = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[i] | |
# Accessing the "A1" cell from the worksheet | |
cell = worksheet.cells.get("A1") | |
# Adding some value to the "A1" cell | |
cell.put_value("Hello Aspose!") | |
# Defining new Style object | |
styleObject = workbook.create_style() | |
# Setting the Orchid (custom) color to the font | |
styleObject.font.color = Color.orchid | |
# Applying the style to the cell | |
cell.set_style(styleObject) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls", SaveFormat.AUTO) |