Java設(shè)置Excel數(shù)據(jù)驗證的示例代碼
數(shù)據(jù)驗證是Excel 2013版本中,數(shù)據(jù)功能組下面的一個功能,在Excel2013之前的版本,包含Excel2010 Excel2007稱為數(shù)據(jù)有效性。通過在excel表格中設(shè)置數(shù)據(jù)驗證可有效規(guī)范數(shù)據(jù)輸入。設(shè)置數(shù)據(jù)類型時,可設(shè)置如驗證數(shù)字(數(shù)字區(qū)間/數(shù)字類型)、日期、文本長度等。下面通過Java程序代碼演示數(shù)據(jù)驗證的設(shè)置方法及結(jié)果。
工具:Free Spire.XLS for Java (免費版)
注:可通過官網(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();
//獲取第一個工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//在單元格B3中設(shè)置數(shù)字驗證-僅允許輸入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è)置日期驗證-僅允許輸入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è)置字符長度驗證-僅允許輸入5個字符以內(nèi)的文本
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設(shè)置數(shù)字驗證-僅允許輸入大于等于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ù)驗證設(shè)置效果:

到此這篇關(guān)于Java設(shè)置Excel數(shù)據(jù)驗證的示例代碼的文章就介紹到這了,更多相關(guān)Java Excel數(shù)據(jù)驗證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis返回單個實體或者返回List的實現(xiàn)
這篇文章主要介紹了Mybatis返回單個實體或者返回List的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
SpringBoot中TypeExcludeFilter的作用及使用方式
在SpringBoot應(yīng)用程序中,TypeExcludeFilter通過過濾特定類型的組件,使它們不被自動掃描和注冊為bean,這在排除不必要的組件或特定實現(xiàn)類時非常有用,通過創(chuàng)建自定義過濾器并注冊到spring.factories文件中,我們可以在應(yīng)用啟動時生效2025-01-01
PostgreSQL Docker部署+SpringBoot集成方式
本文介紹了如何在Docker中部署PostgreSQL和pgadmin,并通過SpringBoot集成PostgreSQL,主要步驟包括安裝PostgreSQL和pgadmin,配置防火墻,創(chuàng)建數(shù)據(jù)庫和表,以及在SpringBoot中配置數(shù)據(jù)源和實體類2024-12-12
利用線程實現(xiàn)動態(tài)顯示系統(tǒng)時間
編寫Applet小程序,通過在HTML文檔中接收參數(shù),顯示當(dāng)前的系統(tǒng)時間,需要的朋友可以參考下2015-10-10
Mybatis-Plus saveBatch()批量保存失效的解決
本文主要介紹了Mybatis-Plus saveBatch()批量保存失效的解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Spring-基于Spring使用自定義注解及Aspect實現(xiàn)數(shù)據(jù)庫切換操作
這篇文章主要介紹了Spring-基于Spring使用自定義注解及Aspect實現(xiàn)數(shù)據(jù)庫切換操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

