您好,登錄后才能下訂單哦!
上一篇文章中介紹了關(guān)于設(shè)置Excel條件格式,包括基于單元格值、自定義公式
等應(yīng)用條件格式、應(yīng)用數(shù)據(jù)條條件類(lèi)型格式、刪除條件格式等內(nèi)容。在本篇文章中將繼續(xù)介紹C# 設(shè)置條件格式的方法。
【示例 1】應(yīng)用條件格式用于高亮重復(fù)、唯一數(shù)值
C#
using Spire.Xls;
using System.Drawing;
namespace HightDuplicateData_XLS
{
class Program
{
static void Main(string[] args)
{
//實(shí)例化Workbook類(lèi),加載測(cè)試文檔
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx");
//獲取第一個(gè)工作表
Worksheet sheet = workbook.Worksheets[0];
//應(yīng)用條件格式1到指定數(shù)據(jù)范圍,高亮重復(fù)的數(shù)值
ConditionalFormatWrapper format1 = sheet.Range["A3:A13"].ConditionalFormats.AddCondition();
format1.FormatType = ConditionalFormatType.DuplicateValues;
format1.BackColor = Color.Cyan;
//應(yīng)用條件格式2到指定數(shù)據(jù)范圍,高亮數(shù)據(jù)中的唯一值
ConditionalFormatWrapper format2 = sheet.Range["A3:A13"].ConditionalFormats.AddCondition();
format2.FormatType = ConditionalFormatType.UniqueValues;
format2.BackColor = Color.Yellow;
//保存文檔并打開(kāi)
workbook.SaveToFile("result.xlsx", ExcelVersion.Version2013);
System.Diagnostics.Process.Start("result.xlsx");
}
}
}
完成代碼后,調(diào)試運(yùn)行程序,生成文檔,如下圖:
【示例2】應(yīng)用條件格式用于高亮峰值(最高、最低)
C#
using Spire.Xls;
using System.Drawing;
namespace HighlightTopData_XLS
{
class Program
{
static void Main(string[] args)
{
//實(shí)例化workbook類(lèi)
Workbook workbook = new Workbook();
//加載測(cè)試文檔
workbook.LoadFromFile("test.xlsx");
//獲取第一個(gè)工作表
Worksheet sheet = workbook.Worksheets[0];
//應(yīng)用條件格式1到指定范圍,并高亮最高的兩個(gè)數(shù)值
ConditionalFormatWrapper format1 = sheet.Range["B17:B24"].ConditionalFormats.AddCondition();
format1.FormatType = ConditionalFormatType.TopBottom;
format1.TopBottom.Type = TopBottomType.Top;
format1.TopBottom.Rank = 2;
format1.BackColor = Color.Green;
//應(yīng)用條件格式2到指定范圍,并高亮最低的兩個(gè)數(shù)值
ConditionalFormatWrapper format2 = sheet.Range["B17:B24"].ConditionalFormats.AddCondition();
format2.FormatType = ConditionalFormatType.TopBottom;
format2.TopBottom.Type = TopBottomType.Bottom;
format2.TopBottom.Rank = 2;
format2.BackColor = Color.RosyBrown;
//保存并打開(kāi)文檔
workbook.SaveToFile("output.xlsx", ExcelVersion.Version2013);
System.Diagnostics.Process.Start("output.xlsx");
}
}
}
測(cè)試結(jié)果:
【示例3】應(yīng)用條件格式用于高亮低于、高于平均值的數(shù)值
C#
using System.Drawing;
using Spire.Xls;
using Spire.Xls.Core;
using Spire.Xls.Core.Spreadsheet.Collections;
namespace Average_Condition
{
class Program
{
static void Main(string[] args)
{
//實(shí)例化workbook類(lèi)
Workbook workbook = new Workbook();
//加載文檔
workbook.LoadFromFile("test.xlsx");
//獲取第一個(gè)工作表
Worksheet sheet = workbook.Worksheets[0];
//添加條件格式1并應(yīng)用到指定數(shù)據(jù)范圍
XlsConditionalFormats format1 = sheet.ConditionalFormats.Add();
format1.AddRange(sheet.Range["B17:B24"]);
//高亮低于平均數(shù)值的單元格
IConditionalFormat cf1 = format1.AddAverageCondition(AverageType.Below);
cf1.BackColor = Color.SkyBlue;
//添加條件格式2并應(yīng)用到指定數(shù)據(jù)范圍
XlsConditionalFormats format2 = sheet.ConditionalFormats.Add();
format2.AddRange(sheet.Range["B17:B24"]);
//高亮高于平均數(shù)值的單元格
IConditionalFormat cf2 = format1.AddAverageCondition(AverageType.Above);
cf2.BackColor = Color.Orange;
//保存并打開(kāi)文檔
workbook.SaveToFile("result.xlsx", ExcelVersion.Version2013);
System.Diagnostics.Process.Start("result.xlsx");
}
}
}
測(cè)試結(jié)果:
以上內(nèi)容是本次關(guān)于設(shè)置Excel表格條件格式的補(bǔ)充介紹,如需轉(zhuǎn)載,請(qǐng)注明出處。
(本文完)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。