Java設置Excel數(shù)據(jù)驗證的示例代碼
數(shù)據(jù)驗證是Excel 2013版本中,數(shù)據(jù)功能組下面的一個功能,在Excel2013之前的版本,包含Excel2010 Excel2007稱為數(shù)據(jù)有效性。通過在excel表格中設置數(shù)據(jù)驗證可有效規(guī)范數(shù)據(jù)輸入。設置數(shù)據(jù)類型時,可設置如驗證數(shù)字(數(shù)字區(qū)間/數(shù)字類型)、日期、文本長度等。下面通過Java程序代碼演示數(shù)據(jù)驗證的設置方法及結(jié)果。
工具:Free Spire.XLS for Java (免費版)
注:可通過官網(wǎng)下載,并解壓將lib文件夾下的jar文件導入java程序;或者通過maven下載導入。
參考如下Jar導入效果:
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ù)字驗證-僅允許輸入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中設置日期驗證-僅允許輸入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個字符以內(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ù)字驗證-僅允許輸入大于等于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); //第二列自適應寬度 sheet.autoFitColumn(2); //保存文檔 workbook.saveToFile("DataValidation.xlsx", ExcelVersion.Version2016); } }
數(shù)據(jù)驗證設置效果:
到此這篇關于Java設置Excel數(shù)據(jù)驗證的示例代碼的文章就介紹到這了,更多相關Java Excel數(shù)據(jù)驗證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
mybatis注解動態(tài)sql注入map和list方式(防sql注入攻擊)
這篇文章主要介紹了mybatis注解動態(tài)sql注入map和list方式(防sql注入攻擊),具有很好的參考價值,希望對大家有所幫助。2021-11-11SpringBoot通過源碼探究靜態(tài)資源的映射規(guī)則實現(xiàn)
這篇文章主要介紹了SpringBoot通過源碼探究靜態(tài)資源的映射規(guī)則實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05Java中的MarkerFilter的應用場景及使用示例詳解
這篇文章主要介紹了Java中的MarkerFilter的應用場景及使用示例詳解,使用log4j2,負責從消息隊列收集日志的,現(xiàn)在系統(tǒng)收集到的日志能和這個系統(tǒng)本身的日志分開,需要的朋友可以參考下2024-01-01