Excel中的條件格式功能是個(gè)十分強(qiáng)大且方便的功能,通過對(duì)使用條件格式功能可以在很大程度上改進(jìn)表格的設(shè)計(jì)和可讀性,用戶可以指定單個(gè)或者多個(gè)單元格區(qū)域應(yīng)用一種或者多種格式,如此一來,也大大提高了表格的可操作性。下面將介紹在C#編程中如何來設(shè)置并應(yīng)用Excel條件格式。
示例要點(diǎn)概述:
測(cè)試文檔如下:
【示例 1 】應(yīng)用條件格式
using Spire.Xls;
using System.Drawing;
namespace ConditionalFormatting_XLS
{
class Program
{
static void Main(string[] args)
{
//實(shí)例化workbook對(duì)象并加載文檔
Workbook wb = new Workbook();
wb.LoadFromFile("sample.xlsx");
//獲取第一個(gè)工作表
Worksheet sheet = wb.Worksheets[0];
//獲取數(shù)據(jù)范圍
CellRange range = sheet.Range["A2:H27"];
//在所選范圍添加條件格式1
ConditionalFormatWrapper format1 = range.ConditionalFormats.AddCondition();
//條件格式類型1基于單元格值
format1.FormatType = ConditionalFormatType.CellValue;
//將數(shù)值在60到90之間的單元格進(jìn)行字體加粗,并設(shè)置字體顏色為橙色
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");
}
}
}
調(diào)試運(yùn)行程序,生成文檔,如下:
【示例2】應(yīng)用數(shù)據(jù)條類型的條件格式
using Spire.Xls;
using System.Drawing;
namespace ConditionalFormatting_XLS
{
class Program
{
static void Main(string[] args)
{
//實(shí)例化workbook對(duì)象并加載文檔
Workbook wb = new Workbook();
wb.LoadFromFile("sample.xlsx");
//獲取第一個(gè)工作表
Worksheet sheet = wb.Worksheets[1];
//獲取數(shù)據(jù)范圍
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");
}
}
}
測(cè)試結(jié)果:
【示例3】刪除條件格式
using Spire.Xls;
namespace RemoveConditionalFormat_XLS
{
class Program
{
static void Main(string[] args)
{
//實(shí)例化Workbook類對(duì)象,加載測(cè)試文檔
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx");
//獲取第一個(gè)工作表
Worksheet sheet = workbook.Worksheets[0];
//刪除指定區(qū)域的條件格式
//sheet.Range["A5:H5"].ConditionalFormats.Remove();
//刪除表格中的所有條件格式
sheet.AllocatedRange.ConditionalFormats.Remove();
//保存并打開文檔
workbook.SaveToFile("result1.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("result1.xlsx");
}
}
}
刪除效果
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。