您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。