Java 设置Excel条件格式(高亮条件值、应用单元格值/公式/数据条等类型)
概述
在Excel中,应用条件格式功能可以在很大程度上改进表格的设计和可读性,用户可以指定单个或者多个单元格区域应用一种或者多种条件格式。本篇文章,将通过Java程序示例介绍条件格式的设置方法,设置条件格式时,因不同设置需要,本文分别从以下示例要点来介绍:
示例1:
1. 应用条件格式用于高亮重复、唯一数值
2. 应用条件格式用于高亮峰值(最高值、最低值)
3. 应用条件格式用于高亮低于或高于平均值的数值
示例2:
1. 应用单元格值类型的条件格式
2. 应用公式类型的条件格式
3. 应用数据条类型的条件格式
示例3:
1. 删除条件格式
程序环境
- Jdk 1.8.0(高于或等于1.6.0版本即可)
- Free Spire.XLS for Java (免费版)
Jar获取及导入:官网下载jar包,并解压将lib文件夹下的jar导入Java程序(或者通过maven下载导入到maven项目程序)。如下导入效果:

程序代码
Java示例1——应用条件格式高亮重复值、唯一值、峰值、高于或低于平均值
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) {
//创建实例,加载测试文档
Workbook wb = new Workbook();
wb.loadFromFile("test.xlsx"); //获取第一个工作表
Worksheet sheet = wb.getWorksheets().get(0); //添加条件格式1并指定数据范围
XlsConditionalFormats format1 = sheet.getConditionalFormats().add();
format1.addRange(sheet.getCellRange("A2:A12"));
//高亮低于平均数值的单元格
IConditionalFormat cf1 = format1.addAverageCondition(AverageType.Below);
cf1.setBackColor(new Color(230,230,250));
//高亮高于平均数值的单元格
IConditionalFormat cf2 = format1.addAverageCondition(AverageType.Above);
cf2.setBackColor(new Color(224,255,255)); //添加条件格式2并指定数据范围
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并指定数据范围
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并指定数据范围
XlsConditionalFormats format4 = sheet.getConditionalFormats().add();
format4.addRange(sheet.getCellRange("D2:D12"));
//高亮重复数值的单元格
IConditionalFormat cf6 = format4.addDuplicateValuesCondition();
cf6.setFormatType(ConditionalFormatType.DuplicateValues);
cf6.setBackColor(new Color(255,228,196)); //添加条件格式5并指定数据范围
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();
}
}
条件格式应用效果:

Java示例2——应用单元格值、公式及数据条类型的条件格式
import com.spire.xls.*;
import java.awt.*;
public class AddConditionalFormat {
public static void main(String[] args) {
//创建实例,加载测试文档
Workbook wb = new Workbook();
wb.loadFromFile("sample.xlsx");
//获取第一个工作表
Worksheet sheet = wb.getWorksheets().get(0);
//获取应用条件格式的数据范围
CellRange range = sheet.getCellRange("A2:H27");
//添加条件格式1
ConditionalFormatWrapper format1 = range.getConditionalFormats().addCondition();
//条件格式类型1基于单元格值
format1.setFormatType(ConditionalFormatType.CellValue);
//将数值在60到90之间的单元格进行字体加粗,并设置字体颜色为橙色
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);
//获取第二个工作表
Worksheet sheet2 = wb.getWorksheets().get(1);
//获取应用条件格式的数据范围
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();
}
}
条件格式应用效果:


Java示例3——删除条件格式
(这里测试文档以示例1中生成的文档为例)
import com.spire.xls.*;
public class RemoveConditionalFormat {
public static void main(String[] args) {
Workbook wb = new Workbook();
wb.loadFromFile("AddConditionalFormat.xlsx");
//获取第一个工作表
Worksheet sheet = wb.getWorksheets().get(0);
//删除指定单元格范围中的条件格式
sheet.getCellRange("A5:H5").getConditionalFormats().removeAt(3);
//保存并打开文档
wb.saveToFile("RemoveConditionalFormat.xlsx", ExcelVersion.Version2010);
wb.dispose();
}
}
条件格式删除效果:

Java 设置Excel条件格式(高亮条件值、应用单元格值/公式/数据条等类型)的更多相关文章
- FineReport——获取控件值和单元格值
设置单元格的值(填报预览): //contentPane.setCellValue(1,0,"abc");//参数面板给单元格赋实际值,即可填报 contentPane.curLG ...
- DEV gridview根据单元格值改变其他单元格格式
string style = ""; private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid. ...
- java poi 获取单元格值时间
完整帮助类:JAVA poi 帮助类 /* * poi特殊日期格式:数字格式化成-yyyy年MM月dd日,格式 * */ private static ArrayList<String> ...
- C# 对Excel操作时,单元格值的读取
一.Range中Value与Value2的区别 当range("A1:B10")设置为 Currency (货币)和 Date (日期.日期时间)数据类型时,range2将返回对应 ...
- ExcelUtility 对excel的序列化与反序列化,支持当单元格中数据为空时将属性赋值为指定类型的默认值
源码https://github.com/leoparddne/EPPlusHelper 安装: Install-Package ExcelUtility -Version 1.1.4 需要为对象添加 ...
- 个人永久性免费-Excel催化剂功能第81波-指定单元格区域内容及公式填充
在日常数据处理过程中,需要对缺失数据进行填充时,按一定逻辑规则进行处理,实现快速填充,规范数据源.此篇给大家带来多种填充数据的场景. 业务使用场景 对各种系统中导出的数据,很多时候存在数据缺失的情况, ...
- POI单元格添加公式以及读取公式结果的值
POI提供了为单元格添加条件样式的方法,但是我并没有找到获取单元格改变后样式的方法,获取到样式依旧是没有改变之前的. 比如为单元格添加条件样式用于监听单元格值是否被修改,如果单元格值被修改那么字体颜色 ...
- DEV GridControl 根据单元格值改变背景色
GridControl 根据单元格值改变背景色(需要用到CustomDrawCell事件) 方法1: private void gdvClient_CustomDrawCell(object send ...
- 无法读取Excel中的数据单元格。有数据,但是读出来全是空值
C#读取Excel,取值为空的解决办法! C#读取Excel遇到无法读取的解决方法是什么呢?这样在C#读取Excel的过程中有很多问题,那么本文就向你介绍如何解决C#读取Excel遇到无法读取的解决方 ...
随机推荐
- 【题解】CIRU - The area of the union of circles [SP8073] \ 圆的面积并 [Bzoj2178]
[题解]CIRU - The area of the union of circles [SP8073] \ 圆的面积并 [Bzoj2178] 传送门: \(\text{CIRU - The area ...
- 膜 zhouakngyang 宝典
持续更新! 注意:本文无 F12. about 周老师:怎么这么强! ZAKY 打 CF 大图:zaky cgr rk1 大图:zaky 传奇 \(1\) ZAKY 打 ATC ZAKY 切题
- Electron安装打包指南
当前环境Debian Linux-Deepin 安装Node 直接下载 命令下载 下载 wget https://nodejs.org/dist/v14.15.1/node-v14.15.1-linu ...
- JavaSE25-Junit&注解
1.Junit单元测试 1.1 测试分类 1. 黑盒测试:不需要写代码,给输入值,看程序是否能够输出期望的值. 2. 白盒测试:需要写代码的.关注程序具体的执行流程. Junit使用:白盒测试 步骤: ...
- go并发之goroutine和channel,并发控制入门篇
并发的概念及其重要性 这段是简单科普,大佬可以跳过 并发:并发程序指同时进行多个任务的程序.在操作系统中,是指一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行 ...
- css进阶 07-CSS面试题
07-CSS面试题 #常见问题 #你是如何理解 HTML 语义化的? 语义化:指对文本内容的结构化(内容语义化),选择合乎语义的标签(代码语义化). 举例:段落用 p,边栏用 aside,主要内容用 ...
- JQuery ajax request及Java服务端乱码问题及设置
今天花了半天功夫才搞定2个乱码问题 1. 原先一直用form提交,现在改作JQuery ajax 提交,发现乱码. 2. window.location url中含有中文提交后,乱码. 第一个问题: ...
- 用DirectX12绘制一个Cube
之前一篇文章讲了DirectX12的初始化流程,现在来看看在此基础上如何绘制一个Cube. 首先,我们要为这个Cube准备一个shader,来告诉GPU绘制的具体流程,DirectX中的shader使 ...
- ActiveMq PUT任意文件上传漏洞(CVE-2016-3088)漏洞复现
漏洞原理 该漏洞出现在fileserver应用中,ActiveMQ中的fileserver服务允许用户通过HTTP PUT方法上传文件到指定目录.Fileserver支持写入文件(不解析jsp),但是 ...
- 这个 bug 让我更加理解 Spring 单例了
我是风筝,公众号「古时的风筝」,一个兼具深度与广度的程序员鼓励师,一个本打算写诗却写起了代码的田园码农! 文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白到大牛要走的路都在 ...