Java設(shè)置Excel數(shù)據(jù)驗(yàn)證的示例代碼
數(shù)據(jù)驗(yàn)證是Excel 2013版本中,數(shù)據(jù)功能組下面的一個(gè)功能,在Excel2013之前的版本,包含Excel2010 Excel2007稱為數(shù)據(jù)有效性。通過在excel表格中設(shè)置數(shù)據(jù)驗(yàn)證可有效規(guī)范數(shù)據(jù)輸入。設(shè)置數(shù)據(jù)類型時(shí),可設(shè)置如驗(yàn)證數(shù)字(數(shù)字區(qū)間/數(shù)字類型)、日期、文本長度等。下面通過Java程序代碼演示數(shù)據(jù)驗(yàn)證的設(shè)置方法及結(jié)果。
工具:Free Spire.XLS for Java (免費(fèi)版)
注:可通過官網(wǎng)下載,并解壓將lib文件夾下的jar文件導(dǎo)入java程序;或者通過maven下載導(dǎo)入。
參考如下Jar導(dǎo)入效果:

Java示例(供參考)
import com.spire.xls.*;
public class DataValidation {
public static void main(String[] args) {
//創(chuàng)建Workbook對象
Workbook workbook = new Workbook();
//獲取第一個(gè)工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//在單元格B3中設(shè)置數(shù)字驗(yàn)證-僅允許輸入1到100之間的數(shù)
sheet.getCellRange("B2").setText("請輸入1-100之間的數(shù):");
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中設(shè)置日期驗(yàn)證-僅允許輸入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設(shè)置字符長度驗(yàn)證-僅允許輸入5個(gè)字符以內(nèi)的文本
sheet.getCellRange("B8").setText("請輸入不超過5個(gè)字符的文本:");
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設(shè)置數(shù)字驗(yàn)證-僅允許輸入大于等于18的整數(shù)
sheet.getCellRange("B11").setText("請輸入大于等于18的整數(shù):");
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);
//第二列自適應(yīng)寬度
sheet.autoFitColumn(2);
//保存文檔
workbook.saveToFile("DataValidation.xlsx", ExcelVersion.Version2016);
}
}數(shù)據(jù)驗(yàn)證設(shè)置效果:

到此這篇關(guān)于Java設(shè)置Excel數(shù)據(jù)驗(yàn)證的示例代碼的文章就介紹到這了,更多相關(guān)Java Excel數(shù)據(jù)驗(yàn)證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java利用PDFBox實(shí)現(xiàn)PDF文檔基本操作
這篇文章主要為大家詳細(xì)介紹了java如何利用PDFBox實(shí)現(xiàn)PDF文檔基本操作,例如創(chuàng)建PDF文檔、加載PDF文檔、獲取總頁數(shù)等,需要的小伙伴可以參考下2023-11-11
java Spring松耦合高效應(yīng)用簡單實(shí)例分析
在Java項(xiàng)目,龐大的對象依賴關(guān)系將一直緊密耦合引起對象難以管理或修改。在這種情況下,可以使用Spring框架作為一個(gè)核心模塊輕松高效地管理所有的對象依賴。本文章向大家介紹Spring松耦合的實(shí)例,需要的朋友可以參考一下。2016-12-12
mybatis注解動(dòng)態(tài)sql注入map和list方式(防sql注入攻擊)
這篇文章主要介紹了mybatis注解動(dòng)態(tài)sql注入map和list方式(防sql注入攻擊),具有很好的參考價(jià)值,希望對大家有所幫助。2021-11-11
SpringBoot通過源碼探究靜態(tài)資源的映射規(guī)則實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot通過源碼探究靜態(tài)資源的映射規(guī)則實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Java8?stream流的map()方法你會(huì)使用了嗎
在日常的開發(fā)工作中經(jīng)常碰到要處理list中數(shù)據(jù)的問題。本文主要帶大家了解下Java8?stream流中map()方法的使用,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-12-12
java實(shí)現(xiàn)人員信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)人員信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Java中的MarkerFilter的應(yīng)用場景及使用示例詳解
這篇文章主要介紹了Java中的MarkerFilter的應(yīng)用場景及使用示例詳解,使用log4j2,負(fù)責(zé)從消息隊(duì)列收集日志的,現(xiàn)在系統(tǒng)收集到的日志能和這個(gè)系統(tǒng)本身的日志分開,需要的朋友可以參考下2024-01-01
基于SSM框架實(shí)現(xiàn)簡單的登錄注冊的示例代碼
這篇文章主要介紹了基于SSM框架實(shí)現(xiàn)簡單的登錄注冊的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12

