在工作表中应用条件格式

使用Aspose.Cells根据单元格值应用条件格式

  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相加。