Java 设置Excel数据验证
数据验证是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数据验证相关推荐
- JAVA设置excel下拉框默认值,Java设置Excel数据验证(下拉列表)
测试Excel表格设置数据下拉列表 /** * 功能描述: 测试Excel表格设置数据下拉列表 * * @author Jack_Liberty * @date 2021-02-21 16:30 */ ...
- java excel 数据有效性_Java设置Excel数据验证(数据有效性)
数据验证是Excel 2013版本中,数据功能组下面的一个功能,在Excel2013之前的版本,包含Excel2010 Excel2007称为数据有效性.通过在excel表格中设置数据验证可有效规范数 ...
- java excel 数据有效性_Java 设置Excel数据验证(数据有效性)
数据验证是Excel 2013版本中,数据功能组下面的一个功能,在Excel2013之前的版本,包含Excel2010 Excel2007称为数据有效性.通过在excel表格中设置数据验证可有效规范数 ...
- java判断excel格式_Java 设置Excel数据验证
import com.spire.xls.*;public classDataValidation {public static voidmain(String[] args) {//创建Workbo ...
- excel导入数据校验_使用Excel数据验证限制日期范围
excel导入数据校验 Yesterday, one of my clients emailed to let me know that she was having trouble entering ...
- java设置excel单元格格式为百分比_Java 设置Excel条件格式示例代码(高亮条件值、应用单元格值/公式/数据条等类型)...
概述 在Excel中,应用条件格式功能可以在很大程度上改进表格的设计和可读性,用户可以指定单个或者多个单元格区域应用一种或者多种条件格式.本篇文章,将通过Java程序示例介绍条件格式的设置方法,设置条 ...
- excel导入数据校验_从Excel数据验证列表中选择多个项目
excel导入数据校验 You've probably used an Excel data validation drop down list, where you can click the ar ...
- php excel 数值验证,excel数据验证找不到怎么办?
excel数据验证找不到的解决办法:首先选中单元格,依次选择[数据-数据验证]:然后在[数据验证]对话框的[设置]选项卡下,[允许]下拉列表中选择[序列]选项:最后在[来源]文本框中输入所有的部门名称 ...
- excel数据验证,过滤空白值
2019独角兽企业重金招聘Python工程师标准>>> excel数据验证,限定某一单元格的值引用自某一列的非空值,函数用法如下: 选中要设置数据验证的单元格,然后 //下面公示表示 ...
最新文章
- 【异步编程】Part3:取消异步操作
- Java---定义一个“点”(Point)类用来表示三维空间中的点(有三个坐标)
- nyoj68三点顺序
- 安装cocoa pods时出现Operation not permitted - /usr/bin/xcodeproj的问题
- 开源的商业意义_为开源项目提供资金具有良好的商业意义
- Bootstrap HTML 编码规范之实用为王
- 两本关于sharepoint 2010的书值得参考
- cad2016中选择全图字体怎么操作_在学习CAD的过程中,经常会遇到的10个问题,你遇到过吗...
- LabelImg 图片标注工具 for Mac
- PowerApps 中的单选控件
- electron打包iOS,dmg文件
- 关于即将到来的软件行业的降维打击的几点想法
- Javascript使用三大家族和事件来DIY动画效果相关笔记(四)
- Sleepy Cow Sorting(树状数组)
- duilib--corner属性总结
- 14位数仿科学真计算机,十四位数仿真计算器
- 最新研究动向:智能制造中可应用的技术【区块链与数字孪生】
- phpstudy打不开localhost
- 神经网络学习的步骤(不是教你怎么学神经网络)
- MySQL参数binlog-do-db对binlogs写入的影响