使用Java程序化地在Excel工作表中應(yīng)用各種條件格式
引言
在數(shù)據(jù)分析和報(bào)表制作過(guò)程中,快速識(shí)別關(guān)鍵數(shù)據(jù)、異常值和趨勢(shì)是提高工作效率的關(guān)鍵。手動(dòng)逐個(gè)檢查數(shù)據(jù)不僅耗時(shí),還容易遺漏重要信息。通過(guò)條件格式,可以根據(jù)數(shù)據(jù)特征自動(dòng)應(yīng)用不同的視覺(jué)樣式,讓重要數(shù)據(jù)一目了然。
本文將介紹如何使用 Java 程序化地在 Excel 工作表中應(yīng)用各種條件格式,包括數(shù)值比較、數(shù)據(jù)條、色階、圖標(biāo)集、重復(fù)值檢測(cè)、平均值高亮等,實(shí)現(xiàn)數(shù)據(jù)的智能可視化分析。
本文使用的方法需要用到免費(fèi)的 Free Spire.XLS for Java,可通過(guò) Maven 或手動(dòng)導(dǎo)入 JAR 包的方式集成到項(xiàng)目中。
環(huán)境準(zhǔn)備
Maven 依賴(lài)配置
在項(xiàng)目的 pom.xml 文件中添加以下依賴(lài):
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>16.3.1</version>
</dependency>或者直接從官網(wǎng)下載 JAR 包并手動(dòng)導(dǎo)入項(xiàng)目。
1. 基于數(shù)值比較的條件格式
數(shù)值比較是最常用的條件格式類(lèi)型,可以根據(jù)單元格值與指定值的關(guān)系自動(dòng)應(yīng)用格式。
1.1 高亮大于指定值的單元格
import com.spire.xls.*;
import com.spire.xls.core.IConditionalFormat;
import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats;
import java.awt.*;
public class HighlightGreaterThan {
public static void main(String[] args) throws Exception {
// 創(chuàng)建工作簿
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);
// 添加示例數(shù)據(jù)
sheet.getCellRange("A1").setNumberValue(582);
sheet.getCellRange("A2").setNumberValue(234);
sheet.getCellRange("A3").setNumberValue(314);
sheet.getCellRange("A4").setNumberValue(50);
sheet.getCellRange("B1").setNumberValue(150);
sheet.getCellRange("B2").setNumberValue(894);
sheet.getCellRange("B3").setNumberValue(560);
sheet.getCellRange("B4").setNumberValue(900);
// 添加條件格式:大于800的值顯示紅色字體和灰色背景
XlsConditionalFormats xcfs = sheet.getConditionalFormats().add();
xcfs.addRange(sheet.getAllocatedRange());
IConditionalFormat format = xcfs.addCondition();
format.setFormatType(ConditionalFormatType.CellValue);
format.setFirstFormula("800");
format.setOperator(ComparisonOperatorType.Greater);
format.setFontColor(Color.RED);
format.setBackColor(Color.LIGHT_GRAY);
// 保存文件
workbook.saveToFile("output/HighlightGreaterThan.xlsx", ExcelVersion.Version2013);
workbook.dispose();
}
}說(shuō)明:
ConditionalFormatType.CellValue表示基于單元格值的條件格式ComparisonOperatorType.Greater表示"大于"比較運(yùn)算符setFirstFormula()設(shè)置比較的基準(zhǔn)值setFontColor()和setBackColor()分別設(shè)置字體顏色和背景顏色
此條件格式會(huì)自動(dòng)高亮所有大于800的單元格,便于快速識(shí)別高值數(shù)據(jù)。

1.2 高亮小于指定值的單元格
// 添加條件格式:小于300的值顯示綠色字體和藍(lán)色背景
XlsConditionalFormats xcfs2 = sheet.getConditionalFormats().add();
xcfs2.addRange(sheet.getAllocatedRange());
IConditionalFormat format2 = xcfs2.addCondition();
format2.setFormatType(ConditionalFormatType.CellValue);
format2.setFirstFormula("300");
format2.setOperator(ComparisonOperatorType.Less);
format2.setFontColor(Color.GREEN);
format2.setBackColor(Color.BLUE);使用場(chǎng)景: 適用于識(shí)別低值數(shù)據(jù)、異常值或需要特別關(guān)注的小數(shù)值。
1.3 高亮介于兩個(gè)值之間的單元格
// 添加條件格式:介于300到500之間的值顯示黃色背景
XlsConditionalFormats xcfs3 = sheet.getConditionalFormats().add();
xcfs3.addRange(sheet.getCellRange("A1:D4"));
IConditionalFormat format3 = xcfs3.addCondition();
format3.setFormatType(ConditionalFormatType.CellValue);
format3.setFirstFormula("300");
format3.setSecondFormula("500");
format3.setOperator(ComparisonOperatorType.Between);
format3.setBackColor(Color.YELLOW);說(shuō)明:
setSecondFormula()設(shè)置范圍的上限值ComparisonOperatorType.Between表示"介于...之間"的運(yùn)算符- 此格式會(huì)高亮所有在300到500之間的數(shù)值
1.4 高亮不在指定范圍內(nèi)的單元格
// 添加條件格式:不在100到200之間的值顯示條紋圖案
XlsConditionalFormats xcfs4 = sheet.getConditionalFormats().add();
xcfs4.addRange(sheet.getCellRange("A1:D4"));
IConditionalFormat format4 = xcfs4.addCondition();
format4.setFormatType(ConditionalFormatType.CellValue);
format4.setFirstFormula("100");
format4.setSecondFormula("200");
format4.setOperator(ComparisonOperatorType.NotBetween);
format4.setFillPattern(ExcelPatternType.ReverseDiagonalStripe);
format4.setColor(Color.LIGHT_GRAY);
format4.setBackColor(Color.BLACK);使用場(chǎng)景: 適用于識(shí)別異常值或超出正常范圍的數(shù)據(jù)。
2. 數(shù)據(jù)條條件格式
數(shù)據(jù)條通過(guò)在單元格中顯示漸變填充的條形圖,直觀展示數(shù)值大小。
import com.spire.xls.*;
import com.spire.xls.core.IConditionalFormat;
import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats;
import java.awt.*;
public class ApplyDataBars {
public static void main(String[] args) throws Exception {
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);
// 添加示例數(shù)據(jù)
sheet.getCellRange("A1").setNumberValue(582);
sheet.getCellRange("A2").setNumberValue(234);
sheet.getCellRange("A3").setNumberValue(314);
sheet.getCellRange("A4").setNumberValue(50);
sheet.getCellRange("B1").setNumberValue(150);
sheet.getCellRange("B2").setNumberValue(894);
sheet.getCellRange("B3").setNumberValue(560);
sheet.getCellRange("B4").setNumberValue(900);
// 設(shè)置行高和列寬
sheet.getAllocatedRange().setRowHeight(15);
sheet.getAllocatedRange().setColumnWidth(17);
// 添加數(shù)據(jù)條條件格式
XlsConditionalFormats xcfs = sheet.getConditionalFormats().add();
xcfs.addRange(sheet.getAllocatedRange());
IConditionalFormat format = xcfs.addCondition();
format.setFormatType(ConditionalFormatType.DataBar);
format.getDataBar().setBarColor(Color.BLUE);
workbook.saveToFile("output/ApplyDataBars.xlsx", ExcelVersion.Version2013);
workbook.dispose();
}
}說(shuō)明:
ConditionalFormatType.DataBar表示數(shù)據(jù)條類(lèi)型getDataBar().setBarColor()設(shè)置數(shù)據(jù)條的顏色- 數(shù)據(jù)條長(zhǎng)度與單元格值成正比,值越大條越長(zhǎng)
使用場(chǎng)景: 適用于快速比較數(shù)值大小,常用于銷(xiāo)售數(shù)據(jù)、績(jī)效指標(biāo)等場(chǎng)景。

3. 色階條件格式
色階使用雙色或三色漸變來(lái)表示數(shù)值分布,直觀展示數(shù)據(jù)的高低分布。
import com.spire.xls.*;
import com.spire.xls.core.IConditionalFormat;
import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats;
public class ApplyColorScales {
public static void main(String[] args) throws Exception {
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);
// 添加示例數(shù)據(jù)
sheet.getCellRange("A1").setNumberValue(582);
sheet.getCellRange("A2").setNumberValue(234);
sheet.getCellRange("A3").setNumberValue(314);
sheet.getCellRange("A4").setNumberValue(50);
sheet.getCellRange("B1").setNumberValue(150);
sheet.getCellRange("B2").setNumberValue(894);
sheet.getCellRange("B3").setNumberValue(560);
sheet.getCellRange("B4").setNumberValue(900);
// 添加色階條件格式
XlsConditionalFormats xcfs = sheet.getConditionalFormats().add();
xcfs.addRange(sheet.getAllocatedRange());
IConditionalFormat format = xcfs.addCondition();
format.setFormatType(ConditionalFormatType.ColorScale);
workbook.saveToFile("output/ApplyColorScales.xlsx", ExcelVersion.Version2013);
workbook.dispose();
}
}說(shuō)明:
ConditionalFormatType.ColorScale表示色階類(lèi)型- 默認(rèn)使用三色漸變(紅-黃-綠),低值為紅色,高值為綠色
- 色階會(huì)自動(dòng)根據(jù)數(shù)據(jù)分布計(jì)算顏色
使用場(chǎng)景: 適用于展示數(shù)據(jù)分布趨勢(shì),如溫度變化、銷(xiāo)售趨勢(shì)等。

4. 圖標(biāo)集條件格式
圖標(biāo)集使用圖標(biāo)(如交通燈、箭頭、星星等)來(lái)表示數(shù)據(jù)的不同等級(jí)。
import com.spire.xls.*;
import com.spire.xls.core.IConditionalFormat;
import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats;
public class ApplyIconSets {
public static void main(String[] args) throws Exception {
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);
// 添加示例數(shù)據(jù)
sheet.getCellRange("A1").setNumberValue(582);
sheet.getCellRange("A2").setNumberValue(234);
sheet.getCellRange("A3").setNumberValue(314);
sheet.getCellRange("A4").setNumberValue(50);
sheet.getCellRange("B1").setNumberValue(150);
sheet.getCellRange("B2").setNumberValue(894);
sheet.getCellRange("B3").setNumberValue(560);
sheet.getCellRange("B4").setNumberValue(900);
// 添加圖標(biāo)集條件格式
XlsConditionalFormats xcfs = sheet.getConditionalFormats().add();
xcfs.addRange(sheet.getAllocatedRange());
IConditionalFormat format = xcfs.addCondition();
format.setFormatType(ConditionalFormatType.IconSet);
format.getIconSet().setIconSetType(IconSetType.ThreeTrafficLights1);
workbook.saveToFile("output/ApplyIconSets.xlsx", ExcelVersion.Version2013);
workbook.dispose();
}
}說(shuō)明:
ConditionalFormatType.IconSet表示圖標(biāo)集類(lèi)型IconSetType.ThreeTrafficLights1使用三色交通燈圖標(biāo)- 其他圖標(biāo)類(lèi)型包括:
ThreeArrows、ThreeSymbols、FourRating、FiveQuarters等
使用場(chǎng)景: 適用于狀態(tài)指示、績(jī)效評(píng)估、風(fēng)險(xiǎn)等級(jí)劃分等場(chǎng)景。

5. 高亮重復(fù)值和唯一值
在數(shù)據(jù)清洗和驗(yàn)證過(guò)程中,識(shí)別重復(fù)值和唯一值非常重要。
import com.spire.xls.*;
import com.spire.xls.core.IConditionalFormat;
import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats;
import java.awt.*;
public class HighlightDuplicateUnique {
public static void main(String[] args) throws Exception {
Workbook workbook = new Workbook();
workbook.loadFromFile("data/Template_Xls_6.xlsx");
Worksheet sheet = workbook.getWorksheets().get(0);
// 高亮重復(fù)值(紅色背景)
XlsConditionalFormats xcfs1 = sheet.getConditionalFormats().add();
xcfs1.addRange(sheet.getCellRange("C2:C10"));
IConditionalFormat format1 = xcfs1.addCondition();
format1.setFormatType(ConditionalFormatType.DuplicateValues);
format1.setBackColor(Color.RED);
// 高亮唯一值(黃色背景)
XlsConditionalFormats xcfs2 = sheet.getConditionalFormats().add();
xcfs2.addRange(sheet.getCellRange("C2:C10"));
IConditionalFormat format2 = xcfs2.addCondition();
format2.setFormatType(ConditionalFormatType.UniqueValues);
format2.setBackColor(Color.YELLOW);
workbook.saveToFile("output/HighlightDuplicateUnique.xlsx", ExcelVersion.Version2013);
workbook.dispose();
}
}說(shuō)明:
ConditionalFormatType.DuplicateValues高亮重復(fù)出現(xiàn)的值ConditionalFormatType.UniqueValues高亮只出現(xiàn)一次的值- 兩種格式可以同時(shí)應(yīng)用,便于數(shù)據(jù)質(zhì)量分析
使用場(chǎng)景: 數(shù)據(jù)去重、數(shù)據(jù)質(zhì)量檢查、唯一性驗(yàn)證等。
6. 高亮高于或低于平均值的單元格
平均值條件格式可以快速識(shí)別高于或低于平均水平的數(shù)值。
import com.spire.xls.*;
import com.spire.xls.core.IConditionalFormat;
import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats;
import java.awt.*;
public class HighlightAverageValues {
public static void main(String[] args) throws Exception {
Workbook workbook = new Workbook();
workbook.loadFromFile("data/Template_Xls_6.xlsx");
Worksheet sheet = workbook.getWorksheets().get(0);
// 高亮低于平均值的單元格(藍(lán)色背景)
XlsConditionalFormats xcfs1 = sheet.getConditionalFormats().add();
xcfs1.addRange(sheet.getCellRange("E2:E10"));
IConditionalFormat cf1 = xcfs1.addAverageCondition(AverageType.Below);
cf1.setBackColor(Color.BLUE);
// 高亮高于平均值的單元格(橙色背景)
XlsConditionalFormats xcfs2 = sheet.getConditionalFormats().add();
xcfs2.addRange(sheet.getCellRange("E2:E10"));
IConditionalFormat cf2 = xcfs2.addAverageCondition(AverageType.Above);
cf2.setBackColor(Color.ORANGE);
workbook.saveToFile("output/HighlightAverageValues.xlsx", ExcelVersion.Version2013);
workbook.dispose();
}
}說(shuō)明:
addAverageCondition(AverageType.Below)創(chuàng)建低于平均值的條件addAverageCondition(AverageType.Above)創(chuàng)建高于平均值的條件- 平均值會(huì)根據(jù)選定范圍內(nèi)的數(shù)據(jù)自動(dòng)計(jì)算
使用場(chǎng)景: 績(jī)效評(píng)估、銷(xiāo)售分析、成績(jī)排名等需要與平均水平對(duì)比的場(chǎng)景。
7. 高亮排名前N或后N的值
排名條件格式可以快速識(shí)別最高或最低的N個(gè)值。
import com.spire.xls.*;
import com.spire.xls.core.IConditionalFormat;
import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats;
import java.awt.*;
public class HighlightRankedValues {
public static void main(String[] args) throws Exception {
Workbook workbook = new Workbook();
workbook.loadFromFile("data/Template_Xls_6.xlsx");
Worksheet sheet = workbook.getWorksheets().get(0);
// 高亮前2名(紅色背景)
XlsConditionalFormats xcfs1 = sheet.getConditionalFormats().add();
xcfs1.addRange(sheet.getCellRange("D2:D10"));
IConditionalFormat format1 = xcfs1.addTopBottomCondition(TopBottomType.Top, 2);
format1.setFormatType(ConditionalFormatType.TopBottom);
format1.setBackColor(Color.RED);
// 高亮后2名(綠色背景)
XlsConditionalFormats xcfs2 = sheet.getConditionalFormats().add();
xcfs2.addRange(sheet.getCellRange("E2:E10"));
IConditionalFormat format2 = xcfs2.addTopBottomCondition(TopBottomType.Bottom, 2);
format2.setFormatType(ConditionalFormatType.TopBottom);
format2.setBackColor(Color.GREEN);
workbook.saveToFile("output/HighlightRankedValues.xlsx", ExcelVersion.Version2013);
workbook.dispose();
}
}說(shuō)明:
addTopBottomCondition(TopBottomType.Top, 2)高亮前2名addTopBottomCondition(TopBottomType.Bottom, 2)高亮后2名- 可以根據(jù)需要調(diào)整排名數(shù)量
使用場(chǎng)景: 識(shí)別最佳/最差表現(xiàn)、Top N 分析、異常值檢測(cè)等。
8. 基于公式的條件格式
公式條件格式提供了最大的靈活性,可以根據(jù)自定義公式應(yīng)用格式。
import com.spire.xls.*;
import com.spire.xls.core.IConditionalFormat;
import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats;
public class FormulaConditionalFormat {
public static void main(String[] args) throws Exception {
Workbook workbook = new Workbook();
workbook.loadFromFile("data/Template_Xls_6.xlsx");
Worksheet sheet = workbook.getWorksheets().get(0);
// 獲取第一列的范圍
CellRange range = sheet.getColumns()[0];
// 添加公式條件格式:當(dāng)A列值小于B列值時(shí)高亮
XlsConditionalFormats xcfs = sheet.getConditionalFormats().add();
xcfs.addRange(range);
IConditionalFormat conditional = xcfs.addCondition();
conditional.setFormatType(ConditionalFormatType.Formula);
conditional.setFirstFormula("=($A1<$B1)");
conditional.setBackKnownColor(ExcelColors.Yellow);
workbook.saveToFile("output/FormulaConditionalFormat.xlsx", ExcelVersion.Version2013);
workbook.dispose();
}
}說(shuō)明:
ConditionalFormatType.Formula表示基于公式的條件格式setFirstFormula()設(shè)置條件公式,公式需要以等號(hào)開(kāi)頭setBackKnownColor()使用預(yù)定義顏色設(shè)置背景
使用場(chǎng)景: 復(fù)雜條件判斷、跨列比較、自定義業(yè)務(wù)規(guī)則等。
9. 基于日期的條件格式
日期條件格式可以高亮特定時(shí)間段的日期,如最近7天、上個(gè)月等。
import com.spire.xls.*;
import com.spire.xls.core.IConditionalFormat;
import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats;
import java.awt.*;
public class DateConditionalFormat {
public static void main(String[] args) {
Workbook workbook = new Workbook();
workbook.loadFromFile("data/Template_Xls_6.xlsx");
Worksheet sheet = workbook.getWorksheets().get(0);
// 高亮最近7天的日期
XlsConditionalFormats xcfs = sheet.getConditionalFormats().add();
xcfs.addRange(sheet.getAllocatedRange());
IConditionalFormat conditionalFormat = xcfs.addTimePeriodCondition(TimePeriodType.Last7Days);
conditionalFormat.setBackColor(Color.ORANGE);
workbook.saveToFile("output/DateConditionalFormat.xlsx", ExcelVersion.Version2013);
workbook.dispose();
}
}說(shuō)明:
addTimePeriodCondition(TimePeriodType.Last7Days)創(chuàng)建最近7天的條件- 其他時(shí)間周期包括:
LastMonth、LastWeek、NextMonth、NextWeek、ThisMonth、ThisWeek、Today、Tomorrow、Yesterday等
使用場(chǎng)景: 項(xiàng)目進(jìn)度跟蹤、任務(wù)管理、時(shí)間敏感數(shù)據(jù)分析等。
10. 帶邊框樣式的條件格式
除了顏色和圖案,條件格式還可以設(shè)置邊框樣式。
import com.spire.xls.*;
import com.spire.xls.core.IConditionalFormat;
import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats;
import java.awt.*;
public class BorderConditionalFormat {
public static void main(String[] args) throws Exception {
Workbook workbook = new Workbook();
workbook.loadFromFile("data/ConditionalFormatRuntime.xlsx");
Worksheet sheet = workbook.getWorksheets().get(0);
// 添加條件格式:小于500的值顯示藍(lán)色邊框
XlsConditionalFormats xcfs = sheet.getConditionalFormats().add();
xcfs.addRange(sheet.getCellRange("A2:D2"));
IConditionalFormat cf = xcfs.addCondition();
cf.setFormatType(ConditionalFormatType.CellValue);
cf.setFirstFormula("500");
cf.setOperator(ComparisonOperatorType.Less);
// 設(shè)置邊框顏色和樣式
cf.setLeftBorderColor(Color.BLUE);
cf.setRightBorderColor(Color.BLUE);
cf.setTopBorderColor(Color.GREEN);
cf.setBottomBorderColor(Color.GREEN);
cf.setLeftBorderStyle(LineStyleType.Medium);
cf.setRightBorderStyle(LineStyleType.Thick);
cf.setTopBorderStyle(LineStyleType.Double);
cf.setBottomBorderStyle(LineStyleType.Double);
workbook.saveToFile("output/BorderConditionalFormat.xlsx", ExcelVersion.Version2013);
workbook.dispose();
}
}說(shuō)明:
setLeftBorderColor()、setRightBorderColor()等設(shè)置各邊框顏色setLeftBorderStyle()、setRightBorderStyle()等設(shè)置各邊框樣式- 邊框樣式包括:
Thin、Medium、Thick、Double等
使用場(chǎng)景: 需要突出顯示但不改變背景色的場(chǎng)景,如打印報(bào)表。
關(guān)鍵類(lèi)與方法解析
核心類(lèi)
| 類(lèi)名 | 說(shuō)明 |
|---|---|
Workbook | Excel 工作簿對(duì)象,用于創(chuàng)建、加載和保存 Excel 文件 |
Worksheet | Excel 工作表對(duì)象,提供訪問(wèn)單元格和條件格式的功能 |
XlsConditionalFormats | 條件格式集合,用于管理和添加條件格式規(guī)則 |
IConditionalFormat | 條件格式接口,定義具體的格式規(guī)則和樣式 |
CellRange | 單元格范圍對(duì)象,表示一個(gè)或多個(gè)單元格 |
條件格式類(lèi)型
| 類(lèi)型 | 枚舉值 | 說(shuō)明 |
|---|---|---|
| 數(shù)值比較 | ConditionalFormatType.CellValue | 基于單元格值與指定值的比較 |
| 數(shù)據(jù)條 | ConditionalFormatType.DataBar | 在單元格中顯示漸變條形圖 |
| 色階 | ConditionalFormatType.ColorScale | 使用顏色漸變表示數(shù)值分布 |
| 圖標(biāo)集 | ConditionalFormatType.IconSet | 使用圖標(biāo)表示數(shù)據(jù)等級(jí) |
| 重復(fù)值 | ConditionalFormatType.DuplicateValues | 高亮重復(fù)出現(xiàn)的值 |
| 唯一值 | ConditionalFormatType.UniqueValues | 高亮只出現(xiàn)一次的值 |
| 公式 | ConditionalFormatType.Formula | 基于自定義公式的條件 |
| 排名 | ConditionalFormatType.TopBottom | 高亮前N或后N名 |
比較運(yùn)算符
| 運(yùn)算符 | 枚舉值 | 說(shuō)明 |
|---|---|---|
| 大于 | ComparisonOperatorType.Greater | 單元格值 > 指定值 |
| 小于 | ComparisonOperatorType.Less | 單元格值 < 指定值 |
| 介于 | ComparisonOperatorType.Between | 指定值1 ≤ 單元格值 ≤ 指定值2 |
| 不介于 | ComparisonOperatorType.NotBetween | 單元格值不在指定范圍內(nèi) |
| 等于 | ComparisonOperatorType.Equal | 單元格值 = 指定值 |
| 不等于 | ComparisonOperatorType.NotEqual | 單元格值 ≠ 指定值 |
常用方法
| 方法 | 說(shuō)明 |
|---|---|
addCondition() | 添加一個(gè)新的條件格式規(guī)則 |
addRange(CellRange) | 為條件格式添加應(yīng)用范圍 |
addAverageCondition(AverageType) | 添加平均值條件 |
addTopBottomCondition(TopBottomType, int) | 添加排名條件 |
addTimePeriodCondition(TimePeriodType) | 添加時(shí)間周期條件 |
setFormatType(ConditionalFormatType) | 設(shè)置條件格式類(lèi)型 |
setFirstFormula(String) | 設(shè)置第一個(gè)公式或比較值 |
setSecondFormula(String) | 設(shè)置第二個(gè)公式或比較值(用于范圍條件) |
setOperator(ComparisonOperatorType) | 設(shè)置比較運(yùn)算符 |
setFontColor(Color) | 設(shè)置字體顏色 |
setBackColor(Color) | 設(shè)置背景顏色 |
setBackKnownColor(ExcelColors) | 使用預(yù)定義顏色設(shè)置背景 |
setFillPattern(ExcelPatternType) | 設(shè)置填充圖案 |
getDataBar() | 獲取數(shù)據(jù)條對(duì)象 |
getIconSet() | 獲取圖標(biāo)集對(duì)象 |
總結(jié)
通過(guò)本文示例,你已經(jīng)了解如何使用 Java 在 Excel 工作表中應(yīng)用各種條件格式。從基礎(chǔ)的數(shù)值比較到高級(jí)的數(shù)據(jù)條、色階、圖標(biāo)集,再到重復(fù)值檢測(cè)、平均值高亮和排名分析,整個(gè)過(guò)程高度自動(dòng)化,特別適用于數(shù)據(jù)分析、報(bào)表制作和數(shù)據(jù)質(zhì)量檢查場(chǎng)景。
相比手動(dòng)設(shè)置條件格式,代碼方式具有以下優(yōu)勢(shì):
- 批量處理:可以一次性為多個(gè)工作表或多個(gè)范圍應(yīng)用條件格式
- 一致性:確保所有報(bào)表使用統(tǒng)一的格式規(guī)則
- 可維護(hù)性:格式規(guī)則集中管理,便于修改和擴(kuò)展
- 自動(dòng)化:集成到數(shù)據(jù)處理流程中,無(wú)需人工干預(yù)
你可以在此基礎(chǔ)上擴(kuò)展更多能力,例如:
- 結(jié)合數(shù)據(jù)驗(yàn)證規(guī)則,實(shí)現(xiàn)數(shù)據(jù)質(zhì)量自動(dòng)檢查
- 根據(jù)業(yè)務(wù)規(guī)則動(dòng)態(tài)生成條件格式
- 批量處理多個(gè) Excel 文件,統(tǒng)一應(yīng)用格式標(biāo)準(zhǔn)
- 與數(shù)據(jù)庫(kù)集成,實(shí)現(xiàn)報(bào)表自動(dòng)化生成
如果你正在處理數(shù)據(jù)分析、報(bào)表制作或數(shù)據(jù)質(zhì)量檢查相關(guān)需求,這種基于 Java 的條件格式方案將為你的工作帶來(lái)顯著提升。
以上就是使用Java程序化地在Excel工作表中應(yīng)用各種條件格式的詳細(xì)內(nèi)容,更多關(guān)于Java在Excel表中應(yīng)用條件格式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java實(shí)現(xiàn)矩陣乘法以及優(yōu)化的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)矩陣乘法以及優(yōu)化的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Java實(shí)戰(zhàn)之用springboot+netty實(shí)現(xiàn)簡(jiǎn)單的一對(duì)一聊天
這篇文章主要介紹了Java實(shí)戰(zhàn)之用springboot+netty實(shí)現(xiàn)簡(jiǎn)單的一對(duì)一聊天,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)Java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
springboot引用kettle實(shí)現(xiàn)對(duì)接oracle數(shù)據(jù)的示例代碼
這篇文章主要介紹了springboot引用kettle實(shí)現(xiàn)對(duì)接oracle數(shù)據(jù),其實(shí)kettle集成到springboot里面沒(méi)有多少代碼,這個(gè)功能最主要的還是ktr文件的編寫(xiě),只要ktr編寫(xiě)好了,放到指定文件夾下,寫(xiě)個(gè)定時(shí)任務(wù)就完事了,需要的朋友可以參考下2022-12-12
springboot嵌套子類(lèi)使用方式—前端與后臺(tái)開(kāi)發(fā)的注意事項(xiàng)
這篇文章主要介紹了springboot嵌套子類(lèi)使用方式—前端與后臺(tái)開(kāi)發(fā)的注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
java并發(fā)編程之深入理解Synchronized的使用
文詳細(xì)講述了線程、進(jìn)程的關(guān)系及在操作系統(tǒng)中的表現(xiàn),這是多線程學(xué)習(xí)必須了解的基礎(chǔ)。本文將接著講一下Java線程同步中的一個(gè)重要的概念synchronized,希望能夠給你有所幫助2021-06-06
Java Spring開(kāi)發(fā)環(huán)境搭建及簡(jiǎn)單入門(mén)示例教程
這篇文章主要介紹了Java Spring開(kāi)發(fā)環(huán)境搭建及簡(jiǎn)單入門(mén)示例,結(jié)合實(shí)例形式分析了spring環(huán)境搭建、配置、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-11-11
Java正則表達(dá)式之Pattern類(lèi)實(shí)例詳解
Pattern類(lèi)的作用在于編譯正則表達(dá)式后創(chuàng)建一個(gè)匹配模式,下面這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式之Pattern類(lèi)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01

