数据验证是Excel 2013版本中,数据功能组下面的一个功能,在Excel2013之前的版本,包含Excel2010 Excel2007称为数据有效性。通过在excel表格中设置数据验证可有效规范数据输入。设置数据类型时,可设置如验证数字(数字区间/数字类型)、日期、文本长度等。下面通过Java程序代码演示数据验证的设置方法及结果。

工具:Free Spire.XLS for Java (免费版)

注:可通过官网下载,并解压将lib文件夹下的jar文件导入java程序;或者通过maven下载导入

参考如下Jar导入效果:

Java示例(供参考)

import com.spire.xls.*;public class DataValidation {public static void main(String[] args) {//创建Workbook对象Workbook workbook = new Workbook();//获取第一个工作表Worksheet sheet = workbook.getWorksheets().get(0);//在单元格B3中设置数字验证-仅允许输入1到100之间的数sheet.getCellRange("B2").setText("请输入1-100之间的数:");CellRange rangeNumber = sheet.getCellRange("B3");rangeNumber.getDataValidation().setCompareOperator(ValidationComparisonOperator.Between);rangeNumber.getDataValidation().setFormula1("1");rangeNumber.getDataValidation().setFormula2("100");rangeNumber.getDataValidation().setAllowType(CellDataType.Decimal);rangeNumber.getDataValidation().setErrorMessage("Please input correct number!");rangeNumber.getDataValidation().setShowError(true);rangeNumber.getCellStyle().setKnownColor(ExcelColors.Color21);//在单元格B6中设置日期验证-仅允许输入1/1/1970到12/31/1970之间的日期sheet.getCellRange("B5").setText("请输入1/1/1970-12/31/1970之间的日期:");CellRange rangeDate = sheet.getCellRange("B6");rangeDate.getDataValidation().setAllowType(CellDataType.Date);rangeDate.getDataValidation().setCompareOperator(ValidationComparisonOperator.Between);rangeDate.getDataValidation().setFormula1("1/1/1970");rangeDate.getDataValidation().setFormula2("12/31/1970");rangeDate.getDataValidation().setErrorMessage("Please input correct date!");rangeDate.getDataValidation().setShowError(true);rangeDate.getDataValidation().setAlertStyle(AlertStyleType.Warning);rangeDate.getCellStyle().setKnownColor(ExcelColors.Color16);//在单元格B9设置字符长度验证-仅允许输入5个字符以内的文本sheet.getCellRange("B8").setText("请输入不超过5个字符的文本:");CellRange rangeTextLength = sheet.getCellRange("B9");rangeTextLength.getDataValidation().setAllowType(CellDataType.TextLength);rangeTextLength.getDataValidation().setCompareOperator(ValidationComparisonOperator.LessOrEqual);rangeTextLength.getDataValidation().setFormula1("5");rangeTextLength.getDataValidation().setErrorMessage("Enter a Valid String!");rangeTextLength.getDataValidation().setShowError(true);rangeTextLength.getDataValidation().setAlertStyle(AlertStyleType.Stop);rangeTextLength.getCellStyle().setKnownColor(ExcelColors.Color14);//在单元格B12设置数字验证-仅允许输入大于等于18的整数sheet.getCellRange("B11").setText("请输入大于等于18的整数:");CellRange rangeinteger = sheet.getCellRange("B12");rangeinteger.getDataValidation().setAllowType(CellDataType.Integer);rangeinteger.getDataValidation().setCompareOperator(ValidationComparisonOperator.GreaterOrEqual);rangeinteger.getDataValidation().setFormula1("18");rangeinteger.getDataValidation().setErrorMessage("Enter a Valid String!");rangeinteger.getDataValidation().setShowError(true);rangeinteger.getDataValidation().setAlertStyle(AlertStyleType.Stop);rangeinteger.getCellStyle().setKnownColor(ExcelColors.LightGreen1);//第二列自适应宽度sheet.autoFitColumn(2);//保存文档workbook.saveToFile("DataValidation.xlsx", ExcelVersion.Version2016);}
}

数据验证设置效果:

(本文完)

Java 设置Excel数据验证相关推荐

  1. JAVA设置excel下拉框默认值,Java设置Excel数据验证(下拉列表)

    测试Excel表格设置数据下拉列表 /** * 功能描述: 测试Excel表格设置数据下拉列表 * * @author Jack_Liberty * @date 2021-02-21 16:30 */ ...

  2. java excel 数据有效性_Java设置Excel数据验证(数据有效性)

    数据验证是Excel 2013版本中,数据功能组下面的一个功能,在Excel2013之前的版本,包含Excel2010 Excel2007称为数据有效性.通过在excel表格中设置数据验证可有效规范数 ...

  3. java excel 数据有效性_Java 设置Excel数据验证(数据有效性)

    数据验证是Excel 2013版本中,数据功能组下面的一个功能,在Excel2013之前的版本,包含Excel2010 Excel2007称为数据有效性.通过在excel表格中设置数据验证可有效规范数 ...

  4. java判断excel格式_Java 设置Excel数据验证

    import com.spire.xls.*;public classDataValidation {public static voidmain(String[] args) {//创建Workbo ...

  5. excel导入数据校验_使用Excel数据验证限制日期范围

    excel导入数据校验 Yesterday, one of my clients emailed to let me know that she was having trouble entering ...

  6. java设置excel单元格格式为百分比_Java 设置Excel条件格式示例代码(高亮条件值、应用单元格值/公式/数据条等类型)...

    概述 在Excel中,应用条件格式功能可以在很大程度上改进表格的设计和可读性,用户可以指定单个或者多个单元格区域应用一种或者多种条件格式.本篇文章,将通过Java程序示例介绍条件格式的设置方法,设置条 ...

  7. excel导入数据校验_从Excel数据验证列表中选择多个项目

    excel导入数据校验 You've probably used an Excel data validation drop down list, where you can click the ar ...

  8. php excel 数值验证,excel数据验证找不到怎么办?

    excel数据验证找不到的解决办法:首先选中单元格,依次选择[数据-数据验证]:然后在[数据验证]对话框的[设置]选项卡下,[允许]下拉列表中选择[序列]选项:最后在[来源]文本框中输入所有的部门名称 ...

  9. excel数据验证,过滤空白值

    2019独角兽企业重金招聘Python工程师标准>>> excel数据验证,限定某一单元格的值引用自某一列的非空值,函数用法如下: 选中要设置数据验证的单元格,然后 //下面公示表示 ...

最新文章

  1. 【异步编程】Part3:取消异步操作
  2. Java---定义一个“点”(Point)类用来表示三维空间中的点(有三个坐标)
  3. nyoj68三点顺序
  4. 安装cocoa pods时出现Operation not permitted - /usr/bin/xcodeproj的问题
  5. 开源的商业意义_为开源项目提供资金具有良好的商业意义
  6. Bootstrap HTML 编码规范之实用为王
  7. 两本关于sharepoint 2010的书值得参考
  8. cad2016中选择全图字体怎么操作_在学习CAD的过程中,经常会遇到的10个问题,你遇到过吗...
  9. LabelImg 图片标注工具 for Mac
  10. PowerApps 中的单选控件
  11. electron打包iOS,dmg文件
  12. 关于即将到来的软件行业的降维打击的几点想法
  13. Javascript使用三大家族和事件来DIY动画效果相关笔记(四)
  14. Sleepy Cow Sorting(树状数组)
  15. duilib--corner属性总结
  16. 14位数仿科学真计算机,十四位数仿真计算器
  17. 最新研究动向:智能制造中可应用的技术【区块链与数字孪生】
  18. phpstudy打不开localhost
  19. 神经网络学习的步骤(不是教你怎么学神经网络)
  20. MySQL参数binlog-do-db对binlogs写入的影响

热门文章

  1. Unity物体系统rigidbody和collider
  2. 电商高并发下会产生的问题——借鉴淘宝
  3. 微信小程序之from表单提交数据到PHP后台
  4. leaf 源码分析01 segment
  5. Xcode使用教程详细讲解(下)
  6. 查找数组的相对“平衡点”
  7. Axure中继器 数据格式的设置
  8. Suricata规则编写
  9. PCB走线延时1inch(1英寸,1000mil,1ps)
  10. Python爬虫数据可视化