数据验证
数据验证用于控制数据类型或用户输入到单元格中的值。相比保护,数据验证不限制用户的输入,而是确保输入的数据符合一定的规则。例如,用户可能被限制只能输入日期、整数、十进制数或预定义值列表中的数据。
数据验证规则
数据验证规则帮助您限制用户输入。此外,您还可以设置一条消息,在电子表格UI控件中显示当前工作簿时,当包含规则的单元格被选中时将显示该消息。
您可以指定在规则不满足时如何通知用户。有三种类型的通知:
- 错误:如果用户输入了无效的数据,一个消息框会给他以下选择:
- Retry:重试。
- 取消更改。
- 取消:如果用户输入了无效的数据,一个消息框会给他以下选择:
- 是:覆盖警告并应用更改。
- 否:再试一次。
- 取消:恢复修改。
- 信息:如果用户输入了无效的数据,一个消息框会给他以下选择:
- OK:覆盖警告并应用更改。
- 取消:恢复修改。
数据验证规则类型
数据验证规则类型包括:
- 任何价值
- 整数
- 小数
- 列表
- 日期
- 时间
- 文本长度
- 自定义
为了能够创建数据验证规则,您必须向规则传递参数。这是使用数据验证规则上下文完成的。有几种具有不同参数的上下文类型,用于不同类型的数据验证规则。
AnyValueDataValidationRuleContext
的AnyValueDataValidationRuleContext类公开以下属性:
- ShowInputMessag:是否显示输入消息。
- InputMessageTitle:输入消息的标题。
- InputMessageContent:输入消息的内容。
- ShowErrorMessage:是否显示错误信息。
- ErrorStyle:错误消息的样式。可能的选择是错误,警告而且信息.
SingleArgumentdataValidationRuleContext
SingleArgumentdataValidationRuleContext属性所包含的属性AnyValueDataValidationRuleContext类具有,但使用以下属性扩展它们:
- IgnoreBlank:指定验证是否忽略空白值,并将其视为有效值。
- Argument1: Тhe验证所需的参数。
- CellIndex:用于创建验证规则的单元格的单元格索引。
- 工作表:创建数据验证规则的工作表。
NumberDataValidationRuleContext
NumberDataValidationRuleContext属性所包含的属性SingleArgumentDataValidationRuleContext类具有,但使用以下属性扩展它们:
- ComparisonOperator:数据验证规则使用的比较运算符。
- Argument2:数据验证规则所需的第二个参数。
ListDataValidationRuleContext
ListDataValidationRuleContext属性所包含的属性SingleArgumentDataValidationRuleContext类具有,但使用以下属性扩展它们:
- InCellDropdown:指定是否应该显示包含数据验证规则列表值的下拉列表。
任意值规则
任意值数据验证规则为默认规则。它应用于所有单元格,并且不执行任何数据验证。您可以简单地指定一个输入消息。永远不会显示错误消息,因为在此规则中,所有值都被视为有效输入。
示例1显示hot以创建任何值验证规则并将其设置为单元格。
例1:应用任意值规则
AnyValueDataValidationRuleContext context = new AnyValueDataValidationRuleContext();上下文。InputMessageContent = "示例输入消息内容";上下文。InputMessageTitle = "示例输入消息标题";AnyValueDataValidationRule = new AnyValueDataValidationRule(context);工作表。细胞(0,0).SetDataValidationRule(规则);
图1:任意值规则
整数规则
整数数据验证规则允许您将用户输入限制为一定范围内的整数。使用两个参数和一个比较运算符指定范围。在某些情况下,例如当将用户输入限制在0到100之间时,两个参数都要使用。在其他情况下,如限制输入大于100的数字,只使用一个参数。此外,还有一个忽略空白值的选项,默认情况下是打开的。
中的代码片段示例2演示如何创建一个整数数据验证规则,该规则使用两个参数将用户输入限制在0到100之间,并将空白值视为无效。
例2:应用带两个参数的整数规则
CellIndex dataValidationRuleCellIndex = new CellIndex(0,0);NumberDataValidationRuleContext context = new NumberDataValidationRuleContext(工作表,dataValidationRuleCellIndex);上下文。InputMessageTitle = "受限输入";上下文。InputMessageContent = "输入被限制为0到100之间的整数值";上下文。错误Style = ErrorStyle.Stop; context.ErrorAlertTitle = "Wrong value"; context.ErrorAlertContent = "The entered value is not valid. Allowed values are in the range between 0 and 100!"; context.IgnoreBlank = false; context.ComparisonOperator = ComparisonOperator.Between; context.Argument1 = "0"; context.Argument2 = "100"; WholeNumberDataValidationRule rule = new WholeNumberDataValidationRule(context); worksheet.Cells[dataValidationRuleCellIndex].SetDataValidationRule(rule);
图2:整数规则
输入value的结果“测试”在包含数据的单元格中,验证规则显示在图3.
图3:整数规则无效结果
中的代码片段示例3演示如何创建一个整数数据验证规则,该规则将带有一个参数的用户输入限制为大于100的数字。
例3:应用带一个参数的整数规则
CellIndex dataValidationRuleCellIndex = new CellIndex(0,0);NumberDataValidationRuleContext context = new NumberDataValidationRuleContext(工作表,dataValidationRuleCellIndex);上下文。InputMessageTitle = "受限输入";上下文。InputMessageContent = "输入限制为大于100的整数值";上下文。错误Style = ErrorStyle.Stop; context.ErrorAlertTitle = "Wrong value"; context.ErrorAlertContent = "The entered value is not valid. Allowed values are all whole number values greater than 100!"; context.ComparisonOperator = ComparisonOperator.GreaterThan; context.Argument1 = "100"; WholeNumberDataValidationRule rule = new WholeNumberDataValidationRule(context); worksheet.Cells[dataValidationRuleCellIndex].SetDataValidationRule(rule);
例4:用公式应用整数规则
// A1工作表的值。细胞(0,0).SetValue (60);// B1工作表的值。细胞[0,1].SetValue (40);CellIndex dataValidationRuleCellIndex =新的CellIndex(1,0);NumberDataValidationRuleContext context = new NumberDataValidationRuleContext(工作表,dataValidationRuleCellIndex);上下文。InputMessageTitle = "受限输入";上下文。InputMessageContent = "输入限制为大于100的整数值";上下文。错误Style = ErrorStyle.Warning; context.ErrorAlertTitle = "Wrong value"; context.ErrorAlertContent = "The entered value is not valid. Allowed values are all whole number values greater than 100!"; context.ComparisonOperator = ComparisonOperator.LessThan; context.Argument1 = "=Sum(A1+B1)"; WholeNumberDataValidationRule rule = new WholeNumberDataValidationRule(context); worksheet.Cells[dataValidationRuleCellIndex].SetDataValidationRule(rule);
图4:基于公式的整数规则
类的构造函数所传递的单元格索引NumberDataValidationRuleContext为其创建规则的单元格。考虑这样一种情况,使用UI选择了从A2到C5的区域,活动单元格是A2。创建的规则与示例4但它不仅适用于单元格A2,而且适用于单元格范围A2:C5。公式“=SUM(A1+B1)”包含两个相对单元格引用——A1和B1。如果您选择单元格C5并打开数据验证对话框,您将看到指定最小值的公式是“=SUM(C4+D4)”而不是“=SUM(A1+B1)”。公式中的相对引用相对地转换为数据验证规则上下文构造函数中传递的单元格。
小数法则
十进制数据验证规则允许您将用户输入限制为使用两个参数和一个比较运算符指定的特定范围内的十进制数。与整数规则的不同之处在于十进制规则允许在单元格中输入整数和小数。
示例5演示如何创建十进制数据验证规则,该规则将用户输入限制在0到100之间的范围之外。
例5:应用十进制规则
CellIndex dataValidationRuleCellIndex = new CellIndex(0,0);NumberDataValidationRuleContext context = new NumberDataValidationRuleContext(工作表,dataValidationRuleCellIndex);上下文。InputMessageTitle = "受限输入";上下文。InputMessageContent = "输入被限制为0到100范围内的十进制数值";上下文。错误Style = ErrorStyle.Stop; context.ErrorAlertTitle = "Wrong value"; context.ErrorAlertContent = "The entered value is not valid. Allowed values are all decimal number values out of the range between 0 and 100!"; context.ComparisonOperator = ComparisonOperator.Between; context.Argument1 = "0"; context.Argument2 = "100"; DecimalDataValidationRule rule = new DecimalDataValidationRule(context); worksheet.Cells[dataValidationRuleCellIndex].SetDataValidationRule(rule);
规则列表
列表数据验证规则允许您将用户输入限制为一组预定义的值。使用InCellDropdown属性,您可以指定是否在单元格旁边显示包含这些值的下拉列表。
例子6显示列表数据验证规则的创建,该规则将用户输入限制为一周中的一天。
例6:应用列表规则
CellIndex dataValidationRuleCellIndex = new CellIndex(0,0);ListDataValidationRuleContext context = new ListDataValidationRuleContext(工作表,dataValidationRuleCellIndex);上下文。InputMessageTitle = "受限输入";上下文。InputMessageContent = "输入限制在一周内。";上下文。错误Style = ErrorStyle.Stop; context.ErrorAlertTitle = "Wrong value"; context.ErrorAlertContent = "The entered value is not valid. Allowed values are the week days!"; context.InCellDropdown = true; context.Argument1 = "Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday"; ListDataValidationRule rule = new ListDataValidationRule(context); worksheet.Cells[dataValidationRuleCellIndex].SetDataValidationRule(rule);
图5:列表规则
日期的规则
date规则允许您将用户输入限制在使用两个参数和一个比较运算符指定的特定日期范围内。
例7显示了如何将用户输入限制为2013年2月12日至2017年5月22日之间的日期。
例7:应用日期规则
CellIndex dataValidationRuleCellIndex = new CellIndex(0,0);NumberDataValidationRuleContext context = new NumberDataValidationRuleContext(工作表,dataValidationRuleCellIndex);上下文。InputMessageTitle = "受限输入";上下文。InputMessageContent = "输入日期限制在2013年2月12日至2017年5月22日之间。";上下文。错误Style = ErrorStyle.Stop; context.ErrorAlertTitle = "Wrong value"; context.ErrorAlertContent = "The entered value is not valid. Allowed values are all dates between 12 February 2013 and 22 May 2017"; context.ComparisonOperator = ComparisonOperator.Between; context.Argument1 = new DateTime(2013, 2, 12).ToShortDateString(); context.Argument2 = new DateTime(2017, 5, 22).ToShortDateString(); DateDataValidationRule rule = new DateDataValidationRule(context); worksheet.Cells[dataValidationRuleCellIndex].SetDataValidationRule(rule);
时间规则
time规则允许您将用户输入限制在使用两个参数和一个比较运算符指定的一定时间范围内。
中的代码片段示例8演示了如何限制用户输入在10:25 AM和3:45 PM之间的范围:
例8:应用时间规则
CellIndex dataValidationRuleCellIndex = new CellIndex(0,0);NumberDataValidationRuleContext context = new NumberDataValidationRuleContext(工作表,dataValidationRuleCellIndex);上下文。InputMessageTitle = "受限输入";上下文。InputMessageContent = "输入时间限制在上午10:25到下午3:45之间";上下文。错误Style = ErrorStyle.Stop; context.ErrorAlertTitle = "Wrong value"; context.ErrorAlertContent = "The entered value is not valid. Allowed values are all times between 10:25 AM to 3:45 PM."; context.ComparisonOperator = ComparisonOperator.Between; context.Argument1 = new DateTime(2015, 2, 2, 10, 25, 0).ToShortTimeString(); context.Argument2 = new DateTime(2015, 2, 2, 15, 45, 0).ToShortTimeString(); TimeDataValidationRule rule = new TimeDataValidationRule(context); worksheet.Cells[dataValidationRuleCellIndex].SetDataValidationRule(rule);
文本长度规则
文本长度规则允许您将用户输入限制为长度在使用两个参数和一个比较运算符指定的特定范围内的文本。
例9演示如何将用户输入限制为长度在5到10个符号之间的文本。
例9:应用文本长度规则
CellIndex dataValidationRuleCellIndex = new CellIndex(0,0);NumberDataValidationRuleContext context = new NumberDataValidationRuleContext(工作表,dataValidationRuleCellIndex);上下文。InputMessageTitle = "受限输入";上下文。InputMessageContent = "输入仅限于长度在5到10个符号之间的文本";上下文。错误Style = ErrorStyle.Stop; context.ErrorAlertTitle = "Wrong value"; context.ErrorAlertContent = "The entered value is not valid. It is allowed to enter only text with length between 5 and 10 symbols."; context.ComparisonOperator = ComparisonOperator.Between; context.Argument1 = "5"; context.Argument2 = "10"; TextLengthDataValidationRule rule = new TextLengthDataValidationRule(context); worksheet.Cells[dataValidationRuleCellIndex].SetDataValidationRule(rule);
自定义规则
自定义规则允许您使用由公式指定的自定义条件来限制用户输入布尔价值。
中的代码片段示例10显示了如何将用户输入限制为大于或等于单元格A1和B1中的值之和的值。
例10:应用自定义规则
CellIndex dataValidationRuleCellIndex =新的CellIndex(1,0);SingleArgumentDataValidationRuleContext context = new SingleArgumentDataValidationRuleContext(工作表,dataValidationRuleCellIndex);上下文。InputMessageTitle = "受限输入";上下文。InputMessageContent = "输入限制为大于或等于单元格A1和B1中值的总和。";上下文。错误Style = ErrorStyle.Stop; context.ErrorAlertTitle = "Wrong value"; context.ErrorAlertContent = "The entered value is not valid. It is allowed to enter only values that are greater or equal to the sum of the values in the cells A1 and B1"; context.Argument1 = "=A2=Sum(A1, B1)"; CustomDataValidationRule rule = new CustomDataValidationRule(context); worksheet.Cells[dataValidationRuleCellIndex].SetDataValidationRule(rule);
评估规则
为了检查单元格值是否满足规则,您必须使用所需的单元格值来计算规则。每个数据验证规则实现IDataValidationRule接口,该接口公开Evaluate ()方法。该方法接收值所在的工作表、将放置值的单元格索引或包含值的单元格索引以及值本身作为参数。
例11属性对规则求值Evaluate ()方法。
例11:评估规则
Double值= 125;ICellValue cellValue = value.ToCellValue();如果规则。Evaluate(worksheet, 0,0, cellValue)){//规则满足,值125有效}否则{//规则不满足}