Excel中的条件格式功能是个十分强大且方便的功能,通过对使用条件格式功能可以在很大程度上改进表格的设计和可读性,用户可以指定单个或者多个单元格区域应用一种或者多种格式,如此一来,也大大提高了表格的可操作性。下面将介绍在C#编程中如何来设置并应用Excel条件格式。
示例要点概述:
测试文档如下:
【示例 1 】应用条件格式
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");
}
}
}
调试运行程序,生成文档,如下:
【示例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[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");
}
}
}
测试结果:
【示例3】删除条件格式
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");
}
}
}
删除效果
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。