Excel中的條件格式功能是個十分強大且友善的功能,通過對使用條件格式功能可以在很大程度上改進表格的設計和可讀性,使用者可以指定單個或者多個單元格區域應用一種或者多種格式,如此一來,也在大大提高了表格的可操作性。下面将介紹在C#程式設計中如何來設定并應用Excel條件格式。
示例要點概述:
1. 基于單元格值應用條件格式
2. 基于自定義公式應用條件格式
3. 應用資料條條件類型格式
4. 删除條件格式
4.1 删除指定資料範圍中的條件格式
4.2 删除全部條件格式
使用工具
- Free Spire.XLS for .NET 8.3(免費版)
- Visual Studio
示例代碼(供參考)
測試文檔如下:
【示例 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");
//擷取第2個工作表
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");
}
}
}
删除效果
1. 删除指定資料範圍的條件格式
2. 删除全部條件格式
本次關于“C# 應用條件格式到Excel”的示例方法介紹到此。
如需轉載,請注明出處。