亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java設置Excel數(shù)據(jù)驗證的示例代碼

 更新時間:2022年05月31日 15:39:41   作者:E-iceblue  
數(shù)據(jù)驗證是Excel 2013版本中,數(shù)據(jù)功能組下面的一個功能。本文將通過Java程序代碼演示數(shù)據(jù)驗證的設置方法及結(jié)果,感興趣的可以了解一下

數(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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java利用PDFBox實現(xiàn)PDF文檔基本操作

    Java利用PDFBox實現(xiàn)PDF文檔基本操作

    這篇文章主要為大家詳細介紹了java如何利用PDFBox實現(xiàn)PDF文檔基本操作,例如創(chuàng)建PDF文檔、加載PDF文檔、獲取總頁數(shù)等,需要的小伙伴可以參考下
    2023-11-11
  • java Spring松耦合高效應用簡單實例分析

    java Spring松耦合高效應用簡單實例分析

    在Java項目,龐大的對象依賴關系將一直緊密耦合引起對象難以管理或修改。在這種情況下,可以使用Spring框架作為一個核心模塊輕松高效地管理所有的對象依賴。本文章向大家介紹Spring松耦合的實例,需要的朋友可以參考一下。
    2016-12-12
  • mybatis注解動態(tài)sql注入map和list方式(防sql注入攻擊)

    mybatis注解動態(tài)sql注入map和list方式(防sql注入攻擊)

    這篇文章主要介紹了mybatis注解動態(tài)sql注入map和list方式(防sql注入攻擊),具有很好的參考價值,希望對大家有所幫助。
    2021-11-11
  • SpringBoot通過源碼探究靜態(tài)資源的映射規(guī)則實現(xiàn)

    SpringBoot通過源碼探究靜態(tài)資源的映射規(guī)則實現(xiàn)

    這篇文章主要介紹了SpringBoot通過源碼探究靜態(tài)資源的映射規(guī)則實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-05-05
  • Java8?stream流的map()方法你會使用了嗎

    Java8?stream流的map()方法你會使用了嗎

    在日常的開發(fā)工作中經(jīng)常碰到要處理list中數(shù)據(jù)的問題。本文主要帶大家了解下Java8?stream流中map()方法的使用,感興趣的小伙伴可以跟隨小編一起學習一下
    2022-12-12
  • Java并發(fā)編程示例(九):本地線程變量的使用

    Java并發(fā)編程示例(九):本地線程變量的使用

    這篇文章主要介紹了Java并發(fā)編程示例(九):本地線程變量的使用,有時,我們更希望能在線程內(nèi)單獨使用,而不和其他使用同一對象啟動的線程共享,Java并發(fā)接口提供了一種很清晰的機制來滿足此需求,該機制稱為本地線程變量,需要的朋友可以參考下
    2014-12-12
  • Springcloud eureka搭建高可用集群過程圖解

    Springcloud eureka搭建高可用集群過程圖解

    這篇文章主要介紹了Springcloud eureka搭建高可用集群過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • java實現(xiàn)人員信息管理系統(tǒng)

    java實現(xiàn)人員信息管理系統(tǒng)

    這篇文章主要為大家詳細介紹了java實現(xiàn)人員信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Java中的MarkerFilter的應用場景及使用示例詳解

    Java中的MarkerFilter的應用場景及使用示例詳解

    這篇文章主要介紹了Java中的MarkerFilter的應用場景及使用示例詳解,使用log4j2,負責從消息隊列收集日志的,現(xiàn)在系統(tǒng)收集到的日志能和這個系統(tǒng)本身的日志分開,需要的朋友可以參考下
    2024-01-01
  • 基于SSM框架實現(xiàn)簡單的登錄注冊的示例代碼

    基于SSM框架實現(xiàn)簡單的登錄注冊的示例代碼

    這篇文章主要介紹了基于SSM框架實現(xiàn)簡單的登錄注冊的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12

最新評論