溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java如何設(shè)置Excel數(shù)據(jù)驗證

發(fā)布時間:2022-06-01 09:29:51 來源:億速云 閱讀:526 作者:iii 欄目:開發(fā)技術(shù)

這篇“Java如何設(shè)置Excel數(shù)據(jù)驗證”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“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如何設(shè)置Excel數(shù)據(jù)驗證

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è)置效果:

Java如何設(shè)置Excel數(shù)據(jù)驗證

以上就是關(guān)于“Java如何設(shè)置Excel數(shù)據(jù)驗證”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI