概述

在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——应用条件格式高亮重复值、唯一值、峰值、高于或低于平均值

  1. import com.spire.xls.*;
  2. import com.spire.xls.core.IConditionalFormat;
  3. import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats;
  4. import com.spire.xls.core.spreadsheet.conditionalformatting.TimePeriodType;
  5.  
  6. import java.awt.*;
  7.  
  8. public class AddConditionalFormat {
  9. public static void main(String[] args) {
  10. //创建实例,加载测试文档
  11. Workbook wb = new Workbook();
  12. wb.loadFromFile("test.xlsx");
  13.  
  14. //获取第一个工作表
  15. Worksheet sheet = wb.getWorksheets().get(0);
  16.  
  17. //添加条件格式1并指定数据范围
  18. XlsConditionalFormats format1 = sheet.getConditionalFormats().add();
  19. format1.addRange(sheet.getCellRange("A2:A12"));
  20. //高亮低于平均数值的单元格
  21. IConditionalFormat cf1 = format1.addAverageCondition(AverageType.Below);
  22. cf1.setBackColor(new Color(230,230,250));
  23. //高亮高于平均数值的单元格
  24. IConditionalFormat cf2 = format1.addAverageCondition(AverageType.Above);
  25. cf2.setBackColor(new Color(224,255,255));
  26.  
  27. //添加条件格式2并指定数据范围
  28. XlsConditionalFormats format2 = sheet.getConditionalFormats().add();
  29. format2.addRange(sheet.getCellRange("B2:B12"));
  30. //高亮最高值
  31. IConditionalFormat cf3 = format2.addTopBottomCondition(TopBottomType.Top, 1);
  32. cf3.setBackColor(new Color(144,238,144));
  33. //高亮最低值单元格
  34. IConditionalFormat cf4 = format2.addTopBottomCondition(TopBottomType.Bottom, 1);
  35. cf4.setBackColor(new Color(221,160,221));
  36.  
  37. //添加条件格式3并指定数据范围
  38. XlsConditionalFormats format3 = sheet.getConditionalFormats().add();
  39. format3.addRange(sheet.getCellRange("C2:C12"));
  40. //高亮唯一值的单元格
  41. IConditionalFormat cf5 = format3.addDuplicateValuesCondition();
  42. cf5.setFormatType(ConditionalFormatType.UniqueValues);
  43. cf5.setBackColor(new Color(0,255,255));
  44.  
  45. //添加条件格式4并指定数据范围
  46. XlsConditionalFormats format4 = sheet.getConditionalFormats().add();
  47. format4.addRange(sheet.getCellRange("D2:D12"));
  48. //高亮重复数值的单元格
  49. IConditionalFormat cf6 = format4.addDuplicateValuesCondition();
  50. cf6.setFormatType(ConditionalFormatType.DuplicateValues);
  51. cf6.setBackColor(new Color(255,228,196));
  52.  
  53. //添加条件格式5并指定数据范围
  54. XlsConditionalFormats format5 = sheet.getConditionalFormats().add();
  55. format5.addRange(sheet.getCellRange("E2:E12"));
  56. //高亮本周日期的单元格
  57. IConditionalFormat cf7 = format5.addTimePeriodCondition(TimePeriodType.ThisWeek);
  58. cf7.setBackColor(new Color(255,165,0));
  59.  
  60. //保存文档
  61. wb.saveToFile("AddConditionalFormat.xlsx", ExcelVersion.Version2013);
  62. wb.dispose();
  63. }
  64. }

条件格式应用效果:

Java示例2——应用单元格值、公式及数据条类型的条件格式

  1. import com.spire.xls.*;
  2.  
  3. import java.awt.*;
  4.  
  5. public class AddConditionalFormat {
  6. public static void main(String[] args) {
  7. //创建实例,加载测试文档
  8. Workbook wb = new Workbook();
  9. wb.loadFromFile("sample.xlsx");
  10.  
  11. //获取第一个工作表
  12. Worksheet sheet = wb.getWorksheets().get(0);
  13.  
  14. //获取应用条件格式的数据范围
  15. CellRange range = sheet.getCellRange("A2:H27");
  16.  
  17. //添加条件格式1
  18. ConditionalFormatWrapper format1 = range.getConditionalFormats().addCondition();
  19. //条件格式类型1基于单元格值
  20. format1.setFormatType(ConditionalFormatType.CellValue);
  21. //将数值在60到90之间的单元格进行字体加粗,并设置字体颜色为橙色
  22. format1.setFirstFormula("90");
  23. format1.setSecondFormula("100");
  24. format1.setOperator(ComparisonOperatorType.Between);
  25. format1.setFontColor(new Color(30,144,255));
  26. //format1.setBackColor(Color.orange);
  27.  
  28. //添加条件格式2
  29. ConditionalFormatWrapper format2 = range.getConditionalFormats().addCondition();
  30. format2.setFormatType(ConditionalFormatType.CellValue);
  31. format2.setFirstFormula("60");
  32. format2.setOperator(ComparisonOperatorType.Less);
  33. format2.setFontColor(Color.red);
  34. //format2.setBackColor(Color.red);
  35. format2.isBold();
  36. //添加边框格式(边框颜色、边框类型)到条件格式2
  37. format2.setLeftBorderColor(Color.red);
  38. format2.setRightBorderColor(new Color(0,0,139));
  39. format2.setTopBorderColor(new Color(123,104,238));
  40. format2.setBottomBorderColor(new Color(50,205,50));
  41. format2.setLeftBorderStyle(LineStyleType.Medium);
  42. format2.setRightBorderStyle(LineStyleType.Thick);
  43. format2.setTopBorderStyle(LineStyleType.Double);
  44. format2.setBottomBorderStyle(LineStyleType.Double);
  45.  
  46. //条件格式3的类型为公式
  47. ConditionalFormatWrapper format3 = range.getConditionalFormats().addCondition();
  48. format3.setFormatType(ConditionalFormatType.Formula);
  49.  
  50. //自定义公式将低于60的单元格所在的行填充背景色
  51. format3.setFirstFormula("=OR($C2<60,$D2<60,$E2<60,$F2<60,$G2<60,$H2<60)");
  52. format3.setBackColor(Color.lightGray);
  53.  
  54. //获取第二个工作表
  55. Worksheet sheet2 = wb.getWorksheets().get(1);
  56.  
  57. //获取应用条件格式的数据范围
  58. CellRange range2 = sheet2.getCellRange("B2:D7");
  59.  
  60. //添加条件类型4为data bars
  61. ConditionalFormatWrapper format4 = range2.getConditionalFormats().addCondition();
  62. format4.setFormatType(ConditionalFormatType.DataBar);
  63. format4.getDataBar().setBarColor(new Color(152,251,152));
  64.  
  65. //保存文档
  66. wb.saveToFile("AddConditionalFormat2.xlsx", ExcelVersion.Version2013);
  67. wb.dispose();
  68. }
  69. }

条件格式应用效果:

Java示例3——删除条件格式

(这里测试文档以示例1中生成的文档为例)

  1. import com.spire.xls.*;
  2.  
  3. public class RemoveConditionalFormat {
  4. public static void main(String[] args) {
  5. Workbook wb = new Workbook();
  6. wb.loadFromFile("AddConditionalFormat.xlsx");
  7.  
  8. //获取第一个工作表
  9. Worksheet sheet = wb.getWorksheets().get(0);
  10.  
  11. //删除指定单元格范围中的条件格式
  12. sheet.getCellRange("A5:H5").getConditionalFormats().removeAt(3);
  13.  
  14. //保存并打开文档
  15. wb.saveToFile("RemoveConditionalFormat.xlsx", ExcelVersion.Version2010);
  16. wb.dispose();
  17. }
  18. }

条件格式删除效果:

Java 设置Excel条件格式(高亮条件值、应用单元格值/公式/数据条等类型)的更多相关文章

  1. FineReport——获取控件值和单元格值

    设置单元格的值(填报预览): //contentPane.setCellValue(1,0,"abc");//参数面板给单元格赋实际值,即可填报 contentPane.curLG ...

  2. DEV gridview根据单元格值改变其他单元格格式

    string style = ""; private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid. ...

  3. java poi 获取单元格值时间

    完整帮助类:JAVA poi 帮助类 /* * poi特殊日期格式:数字格式化成-yyyy年MM月dd日,格式 * */ private static ArrayList<String> ...

  4. C# 对Excel操作时,单元格值的读取

    一.Range中Value与Value2的区别 当range("A1:B10")设置为 Currency (货币)和 Date (日期.日期时间)数据类型时,range2将返回对应 ...

  5. ExcelUtility 对excel的序列化与反序列化,支持当单元格中数据为空时将属性赋值为指定类型的默认值

    源码https://github.com/leoparddne/EPPlusHelper 安装: Install-Package ExcelUtility -Version 1.1.4 需要为对象添加 ...

  6. 个人永久性免费-Excel催化剂功能第81波-指定单元格区域内容及公式填充

    在日常数据处理过程中,需要对缺失数据进行填充时,按一定逻辑规则进行处理,实现快速填充,规范数据源.此篇给大家带来多种填充数据的场景. 业务使用场景 对各种系统中导出的数据,很多时候存在数据缺失的情况, ...

  7. POI单元格添加公式以及读取公式结果的值

    POI提供了为单元格添加条件样式的方法,但是我并没有找到获取单元格改变后样式的方法,获取到样式依旧是没有改变之前的. 比如为单元格添加条件样式用于监听单元格值是否被修改,如果单元格值被修改那么字体颜色 ...

  8. DEV GridControl 根据单元格值改变背景色

    GridControl 根据单元格值改变背景色(需要用到CustomDrawCell事件) 方法1: private void gdvClient_CustomDrawCell(object send ...

  9. 无法读取Excel中的数据单元格。有数据,但是读出来全是空值

    C#读取Excel,取值为空的解决办法! C#读取Excel遇到无法读取的解决方法是什么呢?这样在C#读取Excel的过程中有很多问题,那么本文就向你介绍如何解决C#读取Excel遇到无法读取的解决方 ...

随机推荐

  1. 【Home Page】本博客使用指南

    [关于] 坐标:ZJ.HZ.XJ. 高一现役 OIer,经常被吊打. Luogu:_Wallace_ [近期] 浙大 ICPC-ACM 2020 部分题解: 关键字「ZJU-ICPC Summer T ...

  2. 模块urllib requests json xml configparser 学习笔记

    发起http请求 获取返回值 返回值是字符串 第三方模块安装 pip install requests 返回值格式 xml  html  jaon json 功能  loads   字符串>&g ...

  3. Array的简单使用(Boost和STL通用)

    目录 目录 介绍 使用 Boost和STL的区别 介绍 本来这一次是想简单介绍一下Boost里面的协程库的使用的,但是Boost.Coroutine已经被废弃了,而Boost.Coroutine2目前 ...

  4. PCRE正则表达式语法

    字符 描述 \ 将下一个字符标记为一个特殊字符,或一个原义字符,或一个向后引用,或一个八进制转义符.例如,"\n"匹配一个换行符. ^ 匹配输入字符串的开始位置. $ 匹配输入字符 ...

  5. Java中CAS原理分析(volatile和synchronized浅析)

    CAS是什么? CAS英文解释是比较和交换,是cpu底层的源语,是解决共享变量原子性实现方案,它定义了三个变量,内存地址值对应V,期待值E和要修改的值U,如下图所示,这些变量都是在高速缓存中的,如果两 ...

  6. oracle修改数据文件目录

    一.停库修改数据文件目录.文件名 1.当前数据文件目录 SQL> select file_name from dba_data_files; FILE_NAME ---------------- ...

  7. 一个java文件被执行的历程

    学习java以来,都是以语法,类库入手,最基本的也是最基础的java编译过程往往被我遗忘,先解释一下学习java第一课时,都听到过的一句话,"java是半解释语言".什么是半解释语 ...

  8. Spark-5-如何定位导致数据倾斜的代码

    数据倾斜只会发生在shuffle过程中.这里给大家罗列一些常用的并且可能会触发shuffle操作的算子:distinct.groupByKey.reduceByKey.aggregateByKey.j ...

  9. .net下com调用支持x86/x64

    起因 项目涉及u3d/wpf端的渲染图形合成,采用了开源项目spout,为了便捷,采用了spout的com版本作为c#端的调用 项目调整后,细节已经捋清楚了. 但是考虑桌面应用采用anypc,根据运行 ...

  10. PHP功能代码片段

    1.连接MYSQL数据库代码  <?php  $connec=mysql_connect("localhost","root","root&qu ...