在工作表中应用条件格式
本文旨在详细介绍如何向工作表的一系列单元格添加条件格式。
条件格式是Microsoft Excel中的一个高级功能,允许您对一系列单元格应用格式,并且根据单元格的值或公式的值进行格式更改。例如,单元格的背景可能显示为红色以突出显示负值,或者正值的文字颜色可能为绿色。当单元格的值满足格式条件时,将应用格式。如果单元格的值不满足格式条件,则使用单元格的默认格式。
使用Microsoft Office Automation可以应用条件格式,但这有其缺点。涉及几个原因和问题:例如,安全性,稳定性,可扩展性和速度。寻找另一个解决方案的主要原因是,Microsoft本身强烈建议不要在软件解决方案中使用Office Automation。
本文展示了如何使用Aspose.Cells API在单元格上添加条件格式的几行简单的代码来创建一个控制台应用程序。
使用Aspose.Cells根据单元格值应用条件格式
- 下载并安装Aspose.Cells。
- 下载 Aspose.Cells for Python via .NET。
- 在您的开发计算机上安装它。 所有Aspose组件在安装后都处于评估模式。评估模式没有时间限制,只会在生成的文档中插入水印。
- 创建一个项目。 启动 Visual Studio.NET 并创建一个新控制台应用程序。此示例创建一个 Python 控制台应用程序,但您也可以使用 VB.NET。
- 添加引用。 在您的项目中添加对 Aspose.Cells 的引用。
- *根据单元格值应用条件格式。 下面是用于完成任务的代码。它应用了一个单元格的条件格式。
from aspose.cells import CellArea, FormatConditionType, OperatorType, 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() | |
sheet = workbook.worksheets[0] | |
# Adds an empty conditional formatting | |
index = sheet.conditional_formattings.add() | |
fcs = sheet.conditional_formattings[index] | |
# Sets the conditional format range. | |
ca = CellArea() | |
ca.start_row = 0 | |
ca.end_row = 0 | |
ca.start_column = 0 | |
ca.end_column = 0 | |
fcs.add_area(ca) | |
# Adds condition. | |
conditionIndex = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100") | |
# Sets the background color. | |
fc = fcs[conditionIndex] | |
fc.style.background_color = Color.red | |
# Saving the Excel file | |
workbook.save(dataDir + "output.out.xls", SaveFormat.AUTO) |
执行上述代码后,在输出文件(output.xls)的第一个工作表中的单元格“A1”上应用了条件格式。应用于A1的条件格式取决于单元格的值。如果A1的单元格值在50到100之间,则由于应用了条件格式,背景颜色为红色。
使用Aspose.Cells根据公式应用条件格式
1.根据公式应用条件格式(代码片段) 以下是完成任务的代码。它在B3上应用条件格式。
from aspose.cells import CellArea, FormatConditionType, 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() | |
sheet = workbook.worksheets[0] | |
# Adds an empty conditional formatting | |
index = sheet.conditional_formattings.add() | |
fcs = sheet.conditional_formattings[index] | |
# Sets the conditional format range. | |
ca = CellArea() | |
ca = CellArea() | |
ca.start_row = 2 | |
ca.end_row = 2 | |
ca.start_column = 1 | |
ca.end_column = 1 | |
fcs.add_area(ca) | |
# Adds condition. | |
conditionIndex = fcs.add_condition(FormatConditionType.EXPRESSION) | |
# Sets the background color. | |
fc = fcs[conditionIndex] | |
fc.formula1 = "=IF(SUM(B1:B2)>100,TRUE,FALSE)" | |
fc.style.background_color = Color.red | |
sheet.cells.get("B3").formula = "=SUM(B1:B2)" | |
sheet.cells.get("C4").put_value("If Sum of B1:B2 is greater than 100, B3 will have RED background") | |
# Saving the Excel file | |
workbook.save(dataDir + "output.out.xls", SaveFormat.AUTO) |
执行以上代码后,在输出文件的第一个工作表(output.xls)中的单元格“B3”应用条件格式。应用的条件格式取决于计算“B3”值的公式,该公式将B1和B2相加。