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

Java示例(供參考)
import com.spire.xls.*;
public class DataValidation {
public static void main(String[] args) {
//創(chuàng)建Workbook對(duì)象
Workbook workbook = new Workbook();
//獲取第一個(gè)工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//在單元格B3中設(shè)置數(shù)字驗(yàn)證-僅允許輸入1到100之間的數(shù)
sheet.getCellRange("B2").setText("請(qǐng)輸入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("請(qǐng)輸入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è)置字符長(zhǎng)度驗(yàn)證-僅允許輸入5個(gè)字符以內(nèi)的文本
sheet.getCellRange("B8").setText("請(qǐng)輸入不超過(guò)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("請(qǐng)輸入大于等于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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis返回單個(gè)實(shí)體或者返回List的實(shí)現(xiàn)
這篇文章主要介紹了Mybatis返回單個(gè)實(shí)體或者返回List的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
SpringBoot中TypeExcludeFilter的作用及使用方式
在SpringBoot應(yīng)用程序中,TypeExcludeFilter通過(guò)過(guò)濾特定類型的組件,使它們不被自動(dòng)掃描和注冊(cè)為bean,這在排除不必要的組件或特定實(shí)現(xiàn)類時(shí)非常有用,通過(guò)創(chuàng)建自定義過(guò)濾器并注冊(cè)到spring.factories文件中,我們可以在應(yīng)用啟動(dòng)時(shí)生效2025-01-01
PostgreSQL Docker部署+SpringBoot集成方式
本文介紹了如何在Docker中部署PostgreSQL和pgadmin,并通過(guò)SpringBoot集成PostgreSQL,主要步驟包括安裝PostgreSQL和pgadmin,配置防火墻,創(chuàng)建數(shù)據(jù)庫(kù)和表,以及在SpringBoot中配置數(shù)據(jù)源和實(shí)體類2024-12-12
利用線程實(shí)現(xiàn)動(dòng)態(tài)顯示系統(tǒng)時(shí)間
編寫Applet小程序,通過(guò)在HTML文檔中接收參數(shù),顯示當(dāng)前的系統(tǒng)時(shí)間,需要的朋友可以參考下2015-10-10
Mybatis-Plus saveBatch()批量保存失效的解決
本文主要介紹了Mybatis-Plus saveBatch()批量保存失效的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Spring-基于Spring使用自定義注解及Aspect實(shí)現(xiàn)數(shù)據(jù)庫(kù)切換操作
這篇文章主要介紹了Spring-基于Spring使用自定義注解及Aspect實(shí)現(xiàn)數(shù)據(jù)庫(kù)切換操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
Spring Data分頁(yè)與排序的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Spring Data分頁(yè)與排序的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
Java Collection集合遍歷運(yùn)行代碼實(shí)例
這篇文章主要介紹了Java Collection集合遍歷運(yùn)行代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

