本文将介绍通过C#编程在Excel工作表中应用条件格式的方法。示例中将从以下几种情况来设置条件格式:
- 应用条件格式
- AddAverageCondition(AverageType averageType) 应用于低于或高于平均值的数据
- AddTopBottomCondition(TopBottomTypetopBottomType, int rank) 应用于最高或最低值的数据
- ConditionalFormatType.UniqueValues 应用于唯一值数据
- ConditionalFormatType.DuplicateValues 应用于重复出现的数据
- AddTimePeriodCondition(TimePeriodTypetimePeriodType) 应用于满足条件格式日期的数据
- ConditionalFormatType.CellValue 基于单元格值应用条件格式
- ConditionalFormatType.Formula 基于自定义公式应用条件格式
- ConditionalFormatType.DataBar 应用数据条条件类型格式
- 删除指定数据范围中的条件格式
- 删除全部条件格式
C#代码示例(供参考)
【示例1】
- using Spire.Xls;
- using Spire.Xls.Core;
- using Spire.Xls.Core.Spreadsheet.Collections;
- using Spire.Xls.Core.Spreadsheet.ConditionalFormatting;
- using System.Drawing;
- namespace Test_XLS
- {
- class Program
- {
- static void Main(string[] args)
- {
- //创建Workbook类的对象,并加载测试文档
- Workbook wb = new Workbook();
- wb.LoadFromFile("test.xlsx");
- //获取指定工作表
- Worksheet sheet= wb.Worksheets[0];
- //添加条件格式1并指定数据范围
- XlsConditionalFormats format1 = sheet.ConditionalFormats.Add();
- format1.AddRange(sheet.Range["A2:A12"]);
- //高亮低于平均数值的单元格
- IConditionalFormat cf1 = format1.AddAverageCondition(AverageType.Below);
- cf1.BackColor = Color.Lavender;
- //高亮高于平均数值的单元格
- IConditionalFormat cf2 = format1.AddAverageCondition(AverageType.Above);
- cf2.BackColor = Color.LightBlue;
- //添加条件格式2并指定数据范围
- XlsConditionalFormats format2 = sheet.ConditionalFormats.Add();
- format2.AddRange(sheet.Range["B2:B12"]);
- //高亮最高值
- IConditionalFormat cf3 = format2.AddTopBottomCondition(TopBottomType.Top, 1);
- cf3.BackColor = Color.Green;
- //高亮最低值单元格
- IConditionalFormat cf4 = format2.AddTopBottomCondition(TopBottomType.Bottom, 1);
- cf4.BackColor = Color.RosyBrown;
- //添加条件格式3并指定数据范围
- XlsConditionalFormats format3 = sheet.ConditionalFormats.Add();
- format3.AddRange(sheet.Range["C2:C12"]);
- //高亮唯一值的单元格
- IConditionalFormat cf5 = format3.AddDuplicateValuesCondition();
- cf5.FormatType = ConditionalFormatType.UniqueValues;
- cf5.BackColor = Color.Cyan;
- //添加条件格式4并指定数据范围
- XlsConditionalFormats format4 = sheet.ConditionalFormats.Add();
- format4.AddRange(sheet.Range["D2:D12"]);
- //高亮重复数值的单元格
- IConditionalFormat cf6 = format4.AddDuplicateValuesCondition();
- cf6.FormatType = ConditionalFormatType.DuplicateValues;
- cf6.BackColor = Color.Beige;
- //添加条件格式5并指定数据范围
- XlsConditionalFormats format5 = sheet.ConditionalFormats.Add();
- format5.AddRange(sheet.Range["E2:E12"]);
- //高亮本周日期的单元格
- IConditionalFormat cf7 = format5.AddTimePeriodCondition(TimePeriodType.ThisWeek);
- cf7.BackColor = Color.Orange;
- //保存文档
- wb.SaveToFile("result.xlsx", ExcelVersion.Version2013);
- System.Diagnostics.Process.Start("result.xlsx");
- }
- }
- }
条件格式应用效果:
【示例2】基于单元格值、公式应用条件格式
- using Spire.Xls;
- using System.Drawing;
- namespace ConditionalFormatting_XLS
- {
- class Program
- {
- static void Main(string[] args)
- {
- //实例化workbook对象并加载文档
- Workbook wb = new Workbook();
- wb.LoadFromFile("sample.xlsx");
- //获取第一个工作表
- Worksheet sheet = wb.Worksheets[0];
- //获取数据范围
- CellRange range = sheet.Range["A2:H27"];
- //在所选范围添加条件格式1
- ConditionalFormatWrapper format1 = range.ConditionalFormats.AddCondition();
- //条件格式类型1基于单元格值
- format1.FormatType = ConditionalFormatType.CellValue;
- //将数值在60到90之间的单元格进行字体加粗,并设置字体颜色为橙色
- format1.FirstFormula = "60";
- format1.SecondFormula = "90";
- format1.Operator = ComparisonOperatorType.Between;
- format1.FontColor = Color.Orange;
- //format1.BackColor = Color.Orange;
- //添加条件格式2
- ConditionalFormatWrapper format2 = range.ConditionalFormats.AddCondition();
- format2.FormatType = ConditionalFormatType.CellValue;
- format2.FirstFormula = "60";
- format2.Operator = ComparisonOperatorType.Less;
- format2.FontColor = Color.Red;
- //format2.BackColor = Color.Red;
- format2.IsBold = true;
- //添加边框格式(边框颜色、边框类型)到条件格式2
- format2.LeftBorderColor = Color.Red;
- format2.RightBorderColor = Color.DarkBlue;
- format2.TopBorderColor = Color.DeepSkyBlue;
- format2.BottomBorderColor = Color.DeepSkyBlue;
- format2.LeftBorderStyle = LineStyleType.Medium;
- format2.RightBorderStyle = LineStyleType.Thick;
- format2.TopBorderStyle = LineStyleType.Double;
- format2.BottomBorderStyle = LineStyleType.Double;
- //条件格式3的类型为自定义公式
- ConditionalFormatWrapper format3 = range.ConditionalFormats.AddCondition();
- format3.FormatType = ConditionalFormatType.Formula;
- //自定义公式将低于60的单元格所在的行填充背景色
- format3.FirstFormula = "=OR($C2<60,$D2<60,$E2<60,$F2<60,$G2<60,$H2<60)";
- format3.BackColor = Color.Gray;
- //保存并打开文档
- wb.SaveToFile("result.xlsx", ExcelVersion.Version2013);
- System.Diagnostics.Process.Start("result.xlsx");
- }
- }
- }
调试运行程序,生成文档,如下:
【示例3】应用数据条条件类型格式
- using Spire.Xls;
- using System.Drawing;
- namespace ConditionalFormatting_XLS
- {
- class Program
- {
- static void Main(string[] args)
- {
- //实例化workbook对象并加载文档
- Workbook wb = new Workbook();
- wb.LoadFromFile("sample.xlsx");
- //获取第一个工作表
- Worksheet sheet = wb.Worksheets[1];
- //获取数据范围
- CellRange range = sheet.Range["B2:D7"];
- //添加条件类型4为data bars
- ConditionalFormatWrapper format4 = sheet.AllocatedRange.ConditionalFormats.AddCondition();
- format4.FormatType = ConditionalFormatType.DataBar;
- format4.DataBar.BarColor = Color.ForestGreen;
- //保存并打开文档
- wb.SaveToFile("result1.xlsx", ExcelVersion.Version2013);
- System.Diagnostics.Process.Start("result1.xlsx");
- }
- }
- }
测试结果:
【示例4】删除条件格式
- using Spire.Xls;
- namespace RemoveConditionalFormat_XLS
- {
- class Program
- {
- static void Main(string[] args)
- {
- //实例化Workbook类对象,加载测试文档
- Workbook workbook = new Workbook();
- workbook.LoadFromFile("test.xlsx");
- //获取第一个工作表
- Worksheet sheet = workbook.Worksheets[0];
- //删除指定区域的条件格式
- //sheet.Range["A5:H5"].ConditionalFormats.Remove();
- //删除表格中的所有条件格式
- sheet.AllocatedRange.ConditionalFormats.Remove();
- //保存并打开文档
- workbook.SaveToFile("result1.xlsx", ExcelVersion.Version2010);
- System.Diagnostics.Process.Start("result1.xlsx");
- }
- }
- }
(本文完)