天天看點

Java 設定Excel資料驗證

資料驗證是Excel 2013版本中,資料功能組下面的一個功能,在Excel2013之前的版本,包含Excel2010 Excel2007稱為資料有效性。通過在excel表格中設定資料驗證可有效規範資料輸入。設定資料類型時,可設定如驗證數字(數字區間/數字類型)、日期、文本長度等。下面通過Java程式代碼示範資料驗證的設定方法及結果。

工具:Free Spire.XLS for Java (免費版)

注:可通過官網下載下傳,并解壓将lib檔案夾下的jar檔案導入java程式;或者通過maven下載下傳導入。

參考如下Jar導入效果:

Java 設定Excel資料驗證
Java示例(供參考)

import com.spire.xls.*;

public class DataValidation {
    public static void main(String[] args) {
        //建立Workbook對象
        Workbook workbook = new Workbook();

        //擷取第一個工作表
        Worksheet sheet = workbook.getWorksheets().get(0);

        //在單元格B3中設定數字驗證-僅允許輸入1到100之間的數
        sheet.getCellRange("B2").setText("請輸入1-100之間的數:");
        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個字元以内的文本
        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設定數字驗證-僅允許輸入大于等于18的整數
        sheet.getCellRange("B11").setText("請輸入大于等于18的整數:");
        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);
    }
}      

資料驗證設定效果:

Java 設定Excel資料驗證

(本文完)