Java 設(shè)置Excel條件格式示例代碼(高亮條件值、應(yīng)用單元格值/公式/數(shù)據(jù)條等類型)
概述
在Excel中,應(yīng)用條件格式功能可以在很大程度上改進(jìn)表格的設(shè)計(jì)和可讀性,用戶可以指定單個(gè)或者多個(gè)單元格區(qū)域應(yīng)用一種或者多種條件格式。本篇文章,將通過(guò)Java程序示例介紹條件格式的設(shè)置方法,設(shè)置條件格式時(shí),因不同設(shè)置需要,本文分別從以下示例要點(diǎn)來(lái)介紹:
示例1:
1. 應(yīng)用條件格式用于高亮重復(fù)、唯一數(shù)值
2. 應(yīng)用條件格式用于高亮峰值(最高值、最低值)
3. 應(yīng)用條件格式用于高亮低于或高于平均值的數(shù)值
示例2:
1. 應(yīng)用單元格值類型的條件格式
2. 應(yīng)用公式類型的條件格式
3. 應(yīng)用數(shù)據(jù)條類型的條件格式
示例3:
1. 刪除條件格式
程序環(huán)境
- Jdk 1.8.0(高于或等于1.6.0版本即可)
- Free Spire.XLS for Java (免費(fèi)版)
Jar獲取及導(dǎo)入:官網(wǎng)下載jar包,并解壓將lib文件夾下的jar導(dǎo)入Java程序(或者通過(guò)maven下載導(dǎo)入到maven項(xiàng)目程序)。如下導(dǎo)入效果:
程序代碼
Java示例1——應(yīng)用條件格式高亮重復(fù)值、唯一值、峰值、高于或低于平均值
import com.spire.xls.*; import com.spire.xls.core.IConditionalFormat; import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats; import com.spire.xls.core.spreadsheet.conditionalformatting.TimePeriodType; import java.awt.*; public class AddConditionalFormat { public static void main(String[] args) { //創(chuàng)建實(shí)例,加載測(cè)試文檔 Workbook wb = new Workbook(); wb.loadFromFile("test.xlsx"); //獲取第一個(gè)工作表 Worksheet sheet = wb.getWorksheets().get(0); //添加條件格式1并指定數(shù)據(jù)范圍 XlsConditionalFormats format1 = sheet.getConditionalFormats().add(); format1.addRange(sheet.getCellRange("A2:A12")); //高亮低于平均數(shù)值的單元格 IConditionalFormat cf1 = format1.addAverageCondition(AverageType.Below); cf1.setBackColor(new Color(230,230,250)); //高亮高于平均數(shù)值的單元格 IConditionalFormat cf2 = format1.addAverageCondition(AverageType.Above); cf2.setBackColor(new Color(224,255,255)); //添加條件格式2并指定數(shù)據(jù)范圍 XlsConditionalFormats format2 = sheet.getConditionalFormats().add(); format2.addRange(sheet.getCellRange("B2:B12")); //高亮最高值 IConditionalFormat cf3 = format2.addTopBottomCondition(TopBottomType.Top, 1); cf3.setBackColor(new Color(144,238,144)); //高亮最低值單元格 IConditionalFormat cf4 = format2.addTopBottomCondition(TopBottomType.Bottom, 1); cf4.setBackColor(new Color(221,160,221)); //添加條件格式3并指定數(shù)據(jù)范圍 XlsConditionalFormats format3 = sheet.getConditionalFormats().add(); format3.addRange(sheet.getCellRange("C2:C12")); //高亮唯一值的單元格 IConditionalFormat cf5 = format3.addDuplicateValuesCondition(); cf5.setFormatType(ConditionalFormatType.UniqueValues); cf5.setBackColor(new Color(0,255,255)); //添加條件格式4并指定數(shù)據(jù)范圍 XlsConditionalFormats format4 = sheet.getConditionalFormats().add(); format4.addRange(sheet.getCellRange("D2:D12")); //高亮重復(fù)數(shù)值的單元格 IConditionalFormat cf6 = format4.addDuplicateValuesCondition(); cf6.setFormatType(ConditionalFormatType.DuplicateValues); cf6.setBackColor(new Color(255,228,196)); //添加條件格式5并指定數(shù)據(jù)范圍 XlsConditionalFormats format5 = sheet.getConditionalFormats().add(); format5.addRange(sheet.getCellRange("E2:E12")); //高亮本周日期的單元格 IConditionalFormat cf7 = format5.addTimePeriodCondition(TimePeriodType.ThisWeek); cf7.setBackColor(new Color(255,165,0)); //保存文檔 wb.saveToFile("AddConditionalFormat.xlsx", ExcelVersion.Version2013); wb.dispose(); } }
條件格式應(yīng)用效果:
Java示例2——應(yīng)用單元格值、公式及數(shù)據(jù)條類型的條件格式
import com.spire.xls.*; import java.awt.*; public class AddConditionalFormat { public static void main(String[] args) { //創(chuàng)建實(shí)例,加載測(cè)試文檔 Workbook wb = new Workbook(); wb.loadFromFile("sample.xlsx"); //獲取第一個(gè)工作表 Worksheet sheet = wb.getWorksheets().get(0); //獲取應(yīng)用條件格式的數(shù)據(jù)范圍 CellRange range = sheet.getCellRange("A2:H27"); //添加條件格式1 ConditionalFormatWrapper format1 = range.getConditionalFormats().addCondition(); //條件格式類型1基于單元格值 format1.setFormatType(ConditionalFormatType.CellValue); //將數(shù)值在60到90之間的單元格進(jìn)行字體加粗,并設(shè)置字體顏色為橙色 format1.setFirstFormula("90"); format1.setSecondFormula("100"); format1.setOperator(ComparisonOperatorType.Between); format1.setFontColor(new Color(30,144,255)); //format1.setBackColor(Color.orange); //添加條件格式2 ConditionalFormatWrapper format2 = range.getConditionalFormats().addCondition(); format2.setFormatType(ConditionalFormatType.CellValue); format2.setFirstFormula("60"); format2.setOperator(ComparisonOperatorType.Less); format2.setFontColor(Color.red); //format2.setBackColor(Color.red); format2.isBold(); //添加邊框格式(邊框顏色、邊框類型)到條件格式2 format2.setLeftBorderColor(Color.red); format2.setRightBorderColor(new Color(0,0,139)); format2.setTopBorderColor(new Color(123,104,238)); format2.setBottomBorderColor(new Color(50,205,50)); format2.setLeftBorderStyle(LineStyleType.Medium); format2.setRightBorderStyle(LineStyleType.Thick); format2.setTopBorderStyle(LineStyleType.Double); format2.setBottomBorderStyle(LineStyleType.Double); //條件格式3的類型為公式 ConditionalFormatWrapper format3 = range.getConditionalFormats().addCondition(); format3.setFormatType(ConditionalFormatType.Formula); //自定義公式將低于60的單元格所在的行填充背景色 format3.setFirstFormula("=OR($C2<60,$D2<60,$E2<60,$F2<60,$G2<60,$H2<60)"); format3.setBackColor(Color.lightGray); //獲取第二個(gè)工作表 Worksheet sheet2 = wb.getWorksheets().get(1); //獲取應(yīng)用條件格式的數(shù)據(jù)范圍 CellRange range2 = sheet2.getCellRange("B2:D7"); //添加條件類型4為data bars ConditionalFormatWrapper format4 = range2.getConditionalFormats().addCondition(); format4.setFormatType(ConditionalFormatType.DataBar); format4.getDataBar().setBarColor(new Color(152,251,152)); //保存文檔 wb.saveToFile("AddConditionalFormat2.xlsx", ExcelVersion.Version2013); wb.dispose(); } }
條件格式應(yīng)用效果:
Java示例3——?jiǎng)h除條件格式
(這里測(cè)試文檔以示例1中生成的文檔為例)
import com.spire.xls.*; public class RemoveConditionalFormat { public static void main(String[] args) { Workbook wb = new Workbook(); wb.loadFromFile("AddConditionalFormat.xlsx"); //獲取第一個(gè)工作表 Worksheet sheet = wb.getWorksheets().get(0); //刪除指定單元格范圍中的條件格式 sheet.getCellRange("A5:H5").getConditionalFormats().removeAt(3); //保存并打開(kāi)文檔 wb.saveToFile("RemoveConditionalFormat.xlsx", ExcelVersion.Version2010); wb.dispose(); } }
條件格式刪除效果:
到此這篇關(guān)于Java 設(shè)置Excel條件格式示例代碼(高亮條件值、應(yīng)用單元格值/公式/數(shù)據(jù)條等類型)的文章就介紹到這了,更多相關(guān)java excel條件格式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot 設(shè)置局域網(wǎng)訪問(wèn)的實(shí)現(xiàn)步驟
Spring Boot是一個(gè)開(kāi)源Java-based框架,用于創(chuàng)建獨(dú)立的、生產(chǎn)級(jí)別的Spring應(yīng)用,它旨在簡(jiǎn)化Spring應(yīng)用的初始搭建及開(kāi)發(fā)過(guò)程,通過(guò)提供各種自動(dòng)配置的starter包,Spring Boot使得項(xiàng)目配置變得簡(jiǎn)單快速,感興趣的朋友一起看看吧2024-02-02java 中newInstance()方法和new關(guān)鍵字的區(qū)別
這篇文章主要介紹了java 中newInstance()方法和new關(guān)鍵字的區(qū)別的相關(guān)資料,希望通過(guò)本文大家能掌握他們之家的區(qū)別與用法,需要的朋友可以參考下2017-09-09關(guān)于SpringBoot的異?;貪L和事務(wù)的使用詳解
這篇文章主要介紹了關(guān)于SpringBoot的異?;貪L和事務(wù)的使用詳解,Spring中 @Transactional 注解,默認(rèn)情況下,只對(duì)拋出的RuntimeException 異常,才會(huì)事務(wù)回滾,需要的朋友可以參考下2023-05-05java實(shí)現(xiàn)單鏈表中是否有環(huán)的方法詳解
本篇文章介紹了,用java實(shí)現(xiàn)單鏈表中是否有環(huán)的方法詳解。需要的朋友參考下2013-05-05SparkStreaming-Kafka通過(guò)指定偏移量獲取數(shù)據(jù)實(shí)現(xiàn)
這篇文章主要為大家介紹了SparkStreaming-Kafka通過(guò)指定偏移量獲取數(shù)據(jù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06詳談Spring是否支持對(duì)靜態(tài)方法進(jìn)行Aop增強(qiáng)
這篇文章主要介紹了Spring是否支持對(duì)靜態(tài)方法進(jìn)行Aop增強(qiáng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12