在工作表中应用条件格式

使用 Aspose.Cells 应用基于 Cell 值的条件格式

  1. 下载并安装 Aspose.Cells. 1.下载Aspose.Cells for .NET。
  2. 在您的开发计算机上安装它。 所有 Aspose 组件在安装后都在评估模式下工作。评估模式没有时间限制,它只在生成的文档中注入水印。
  3. 创建项目. 启动 Visual Studio.NET 并创建一个新的控制台应用程序。此示例创建一个 C# 控制台应用程序,但您也可以使用 VB.NET。
  4. 添加引用. 将对 Aspose.Cells 的引用添加到您的项目中,例如添加对 ….\Program Files\Aspose\Aspose.Cells\Bin\Net1.0\Aspose.Cells.dll 的引用
  5. *根据单元格值应用条件格式。 下面是用于完成任务的代码。我在单元格上应用条件格式。
// 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();
Worksheet sheet = workbook.Worksheets[0];
// Adds an empty conditional formatting
int index = sheet.ConditionalFormattings.Add();
FormatConditionCollection fcs = sheet.ConditionalFormattings[index];
// Sets the conditional format range.
CellArea ca = new CellArea();
ca.StartRow = 0;
ca.EndRow = 0;
ca.StartColumn = 0;
ca.EndColumn = 0;
fcs.AddArea(ca);
// Adds condition.
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100");
// Sets the background color.
FormatCondition fc = fcs[conditionIndex];
fc.Style.BackgroundColor = 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 上应用条件格式。
// 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();
Worksheet sheet = workbook.Worksheets[0];
// Adds an empty conditional formatting
int index = sheet.ConditionalFormattings.Add();
FormatConditionCollection fcs = sheet.ConditionalFormattings[index];
// Sets the conditional format range.
CellArea ca = new CellArea();
ca = new CellArea();
ca.StartRow = 2;
ca.EndRow = 2;
ca.StartColumn = 1;
ca.EndColumn = 1;
fcs.AddArea(ca);
// Adds condition.
int conditionIndex = fcs.AddCondition(FormatConditionType.Expression);
// Sets the background color.
FormatCondition fc = fcs[conditionIndex];
fc.Formula1 = "=IF(SUM(B1:B2)>100,TRUE,FALSE)";
fc.Style.BackgroundColor = Color.Red;
sheet.Cells["B3"].Formula = "=SUM(B1:B2)";
sheet.Cells["C4"].PutValue("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 之和的公式。