本文将介绍通过C#编程在Excel工作表中应用条件格式的方法。示例中将从以下几种情况来设置条件格式:

  1. 应用条件格式
  • AddAverageCondition(AverageType averageType)  应用于低于或高于平均值的数据
  • AddTopBottomCondition(TopBottomTypetopBottomType, int rank)  应用于最高或最低值的数据
  • ConditionalFormatType.UniqueValues 应用于唯一值数据
  • ConditionalFormatType.DuplicateValues 应用于重复出现的数据
  • AddTimePeriodCondition(TimePeriodTypetimePeriodType) 应用于满足条件格式日期的数据
  • ConditionalFormatType.CellValue  基于单元格值应用条件格式
  • ConditionalFormatType.Formula 基于自定义公式应用条件格式
  • ConditionalFormatType.DataBar  应用数据条条件类型格式
2.删除条件格式
  • 删除指定数据范围中的条件格式
  • 删除全部条件格式
工具:Spire.XLS for .NET

C#代码示例(供参考)

【示例1

  1. using Spire.Xls;
  2. using Spire.Xls.Core;
  3. using Spire.Xls.Core.Spreadsheet.Collections;
  4. using Spire.Xls.Core.Spreadsheet.ConditionalFormatting;
  5. using System.Drawing;

  6. namespace Test_XLS
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             //创建Workbook类的对象,并加载测试文档
  13.             Workbook wb = new Workbook();
  14.             wb.LoadFromFile("test.xlsx");

  15.             //获取指定工作表
  16.             Worksheet sheet= wb.Worksheets[0];

  17.             //添加条件格式1并指定数据范围
  18.             XlsConditionalFormats format1 = sheet.ConditionalFormats.Add();
  19.             format1.AddRange(sheet.Range["A2:A12"]);
  20.             //高亮低于平均数值的单元格
  21.             IConditionalFormat cf1 = format1.AddAverageCondition(AverageType.Below);
  22.             cf1.BackColor = Color.Lavender;
  23.             //高亮高于平均数值的单元格
  24.             IConditionalFormat cf2 = format1.AddAverageCondition(AverageType.Above);
  25.             cf2.BackColor = Color.LightBlue;

  26.             //添加条件格式2并指定数据范围
  27.             XlsConditionalFormats format2 = sheet.ConditionalFormats.Add();
  28.             format2.AddRange(sheet.Range["B2:B12"]);
  29.             //高亮最高值
  30.             IConditionalFormat cf3 = format2.AddTopBottomCondition(TopBottomType.Top, 1);
  31.             cf3.BackColor = Color.Green;
  32.             //高亮最低值单元格
  33.             IConditionalFormat cf4 = format2.AddTopBottomCondition(TopBottomType.Bottom, 1);
  34.             cf4.BackColor = Color.RosyBrown;

  35.             //添加条件格式3并指定数据范围
  36.             XlsConditionalFormats format3 = sheet.ConditionalFormats.Add();
  37.             format3.AddRange(sheet.Range["C2:C12"]);
  38.             //高亮唯一值的单元格
  39.             IConditionalFormat cf5 = format3.AddDuplicateValuesCondition();
  40.             cf5.FormatType = ConditionalFormatType.UniqueValues;
  41.             cf5.BackColor = Color.Cyan;

  42.             //添加条件格式4并指定数据范围
  43.             XlsConditionalFormats format4 = sheet.ConditionalFormats.Add();
  44.             format4.AddRange(sheet.Range["D2:D12"]);
  45.             //高亮重复数值的单元格
  46.             IConditionalFormat cf6 = format4.AddDuplicateValuesCondition();
  47.             cf6.FormatType = ConditionalFormatType.DuplicateValues;
  48.             cf6.BackColor = Color.Beige;

  49.             //添加条件格式5并指定数据范围
  50.             XlsConditionalFormats format5 = sheet.ConditionalFormats.Add();
  51.             format5.AddRange(sheet.Range["E2:E12"]);
  52.             //高亮本周日期的单元格
  53.             IConditionalFormat cf7 = format5.AddTimePeriodCondition(TimePeriodType.ThisWeek);
  54.             cf7.BackColor = Color.Orange;

  55.             //保存文档
  56.             wb.SaveToFile("result.xlsx", ExcelVersion.Version2013);
  57.             System.Diagnostics.Process.Start("result.xlsx");
  58.         }
  59.     }
  60. }

条件格式应用效果:

C# 应用Excel条件格式(8种)-LMLPHP

【示例2】基于单元格值、公式应用条件格式

  1. using Spire.Xls;
  2. using System.Drawing;

  3. namespace ConditionalFormatting_XLS
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             //实例化workbook对象并加载文档
  10.             Workbook wb = new Workbook();
  11.             wb.LoadFromFile("sample.xlsx");

  12.             //获取第一个工作表
  13.             Worksheet sheet = wb.Worksheets[0];

  14.             //获取数据范围
  15.             CellRange range = sheet.Range["A2:H27"];

  16.             //在所选范围添加条件格式1
  17.             ConditionalFormatWrapper format1 = range.ConditionalFormats.AddCondition();

  18.             //条件格式类型1基于单元格值
  19.             format1.FormatType = ConditionalFormatType.CellValue;
  20.             //将数值在60到90之间的单元格进行字体加粗,并设置字体颜色为橙色
  21.             format1.FirstFormula = "60";
  22.             format1.SecondFormula = "90";
  23.             format1.Operator = ComparisonOperatorType.Between;
  24.             format1.FontColor = Color.Orange;
  25.             //format1.BackColor = Color.Orange;

  26.             //添加条件格式2
  27.             ConditionalFormatWrapper format2 = range.ConditionalFormats.AddCondition();
  28.             format2.FormatType = ConditionalFormatType.CellValue;
  29.             format2.FirstFormula = "60";
  30.             format2.Operator = ComparisonOperatorType.Less;
  31.             format2.FontColor = Color.Red;
  32.             //format2.BackColor = Color.Red;
  33.             format2.IsBold = true;
  34.             //添加边框格式(边框颜色、边框类型)到条件格式2
  35.             format2.LeftBorderColor = Color.Red;
  36.             format2.RightBorderColor = Color.DarkBlue;
  37.             format2.TopBorderColor = Color.DeepSkyBlue;
  38.             format2.BottomBorderColor = Color.DeepSkyBlue;
  39.             format2.LeftBorderStyle = LineStyleType.Medium;
  40.             format2.RightBorderStyle = LineStyleType.Thick;
  41.             format2.TopBorderStyle = LineStyleType.Double;
  42.             format2.BottomBorderStyle = LineStyleType.Double;
  43.             //条件格式3的类型为自定义公式
  44.             ConditionalFormatWrapper format3 = range.ConditionalFormats.AddCondition();
  45.             format3.FormatType = ConditionalFormatType.Formula;

  46.             //自定义公式将低于60的单元格所在的行填充背景色
  47.             format3.FirstFormula = "=OR($C2<60,$D2<60,$E2<60,$F2<60,$G2<60,$H2<60)";
  48.             format3.BackColor = Color.Gray;

  49.             //保存并打开文档
  50.             wb.SaveToFile("result.xlsx", ExcelVersion.Version2013);
  51.             System.Diagnostics.Process.Start("result.xlsx");
  52.         }
  53.     }
  54. }

调试运行程序,生成文档,如下:

C# 应用Excel条件格式(8种)-LMLPHP

【示例3】应用数据条条件类型格式

  1. using Spire.Xls;
  2. using System.Drawing;

  3. namespace ConditionalFormatting_XLS
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.               //实例化workbook对象并加载文档
  10.             Workbook wb = new Workbook();
  11.             wb.LoadFromFile("sample.xlsx");

  12.             //获取第一个工作表
  13.             Worksheet sheet = wb.Worksheets[1];

  14.             //获取数据范围
  15.             CellRange range = sheet.Range["B2:D7"];

  16.             //添加条件类型4为data bars
  17.             ConditionalFormatWrapper format4 = sheet.AllocatedRange.ConditionalFormats.AddCondition();
  18.             format4.FormatType = ConditionalFormatType.DataBar;
  19.             format4.DataBar.BarColor = Color.ForestGreen;

  20.             //保存并打开文档
  21.             wb.SaveToFile("result1.xlsx", ExcelVersion.Version2013);
  22.             System.Diagnostics.Process.Start("result1.xlsx");
  23.         }
  24.     }
  25. }

测试结果:

C# 应用Excel条件格式(8种)-LMLPHP

【示例4】删除条件格式

  1. using Spire.Xls;

  2. namespace RemoveConditionalFormat_XLS
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             //实例化Workbook类对象,加载测试文档
  9.             Workbook workbook = new Workbook();
  10.             workbook.LoadFromFile("test.xlsx");

  11.             //获取第一个工作表
  12.             Worksheet sheet = workbook.Worksheets[0];
  13.             //删除指定区域的条件格式
  14.             //sheet.Range["A5:H5"].ConditionalFormats.Remove();

  15.             //删除表格中的所有条件格式
  16.             sheet.AllocatedRange.ConditionalFormats.Remove();

  17.             //保存并打开文档
  18.             workbook.SaveToFile("result1.xlsx", ExcelVersion.Version2010);
  19.             System.Diagnostics.Process.Start("result1.xlsx");
  20.         }
  21.     }
  22. }
运行程序后,可查看生成的文档中条件格式的删除效果。

(本文完)


12-21 17:02