數據驗證是Excel 2013版本中,數據功能組下面的一個功能,在Excel2013之前的版本,包含Excel2010 Excel2007稱爲數據有效性。通過在excel表格中設置數據驗證可有效規範數據輸入。設置數據類型時,可設置如驗證數字(數字區間/數字類型)、日期、文本長度等。下面通過Java程序代碼演示數據驗證的設置方法及結果。
工具:Free Spire.XLS for Java (免費版)
注:可通過官網下載,並解壓將lib文件夾下的jar文件導入java程序;或者通過maven下載導入。
參考如下Jar導入效果:
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);
}
}
數據驗證設置效果:
(本文完)