参考 :http://www.componentcn.com/kongjianjishu/kongjianjishu/2015-06-04/2781.html

Aspose Cells是一款操作和处理以及转换Excel文件的类库,支持.NET和JAVA版,几乎所有Excel能实现的功能,Aspose Cells都可以实现,Microsoft Excel支持多种不同类型的数据验证,每一种类型用于控制数据输入或者单元格范围,比如经常用到的:整数验证、小数位数验证、值范围验证、时间验证、文本字符长度验证等。

控件中国网Aspose Cells在中国的官方授权销售商和技术支持提供商,提供Aspose Cells控件下载、Aspose Cells授权、Aspose Cells正版产品销售等服务

在Aspose Cell要实现单元格的数据验证也是相对简单的,开发人员能够为用户提供一个选择列表、限制数据输入或者是指定数据的类型和大小,在Aspose Cell里,每个工作表类都有一个Validations对象,该对象包含了一系列验证对象,包含了下面的一些属性:

  • Type:用于表示验证类型,用于指定ValidationType枚举里的类型预定值
  • Operator:用于表示使用何种运算符或者运算规则,用于指定OperatorType枚举里的预定义值
  • Formula1:首选的验证公式和表达式
  • Formula2:次选的验证公式和表达式

数据验证类型,控件提供的ValidationType枚举有下列值:

  • AnyValue 指示一个值可以是任何类型
  • WholeNumber 指示验证类型只能是整数数字
  • Decimal 指示验证的类型只能是小数
  • List 指示验证的类型是下拉列表
  • Date 指示验证的类型是日期
  • Time 指示验证类型是时间
  • TextLength 指示验证的类型是文本长度
  • Custom 自定义验证类型

下面的事例介绍了,如何验证一定范围内的单元格输入只能是整数,具体代码如下:

ValidationCollection validations = workbook.Worksheets[0].Validations;

//Creating a Validation object
Validation validation = validations[validations.Add()];

//Setting the validation type to whole number
validation.Type = ValidationType.WholeNumber;

//Setting the operator for validation to Between
validation.Operator = OperatorType.Between;

//Setting the minimum value for the validation
validation.Formula1 = "10";

//Setting the maximum value for the validation
validation.Formula2 = "1000";

//Applying the validation to a range of cells from A1 to B2 using the
//CellArea structure
CellArea area;
area.StartRow = 0;
area.EndRow = 1;
area.StartColumn = 0;
area.EndColumn = 1;

//Adding the cell area to Validation
validation.AreaList.Add(area);

下面的事例介绍了如何使用小数数据验证:
// Create a workbook object.
Workbook workbook = new Workbook();

// Create a worksheet and get the first worksheet.
Worksheet ExcelWorkSheet = workbook.Worksheets[0];

// Obtain the existing Validations collection.
ValidationCollection validations = ExcelWorkSheet.Validations;

// Create a validation object adding to the collection list.
Validation validation = validations[validations.Add()];

// Set the validation type.
validation.Type = ValidationType.Decimal;

// Specify the operator.
validation.Operator = OperatorType.Between;

// Set the lower and upper limits.
validation.Formula1 = Decimal.MinValue.ToString();
validation.Formula2 = Decimal.MaxValue.ToString();

// Set the error message.
validation.ErrorMessage = "Please enter a valid integer or decimal number";

// Specify the validation area of cells.
CellArea area;
area.StartRow = 0;
area.EndRow = 9;
area.StartColumn = 0;
area.EndColumn = 0;

// Add the area.
validation.AreaList.Add(area);

// Set the number formats to 2 decimal places for the validation area.
for (int i = 0; i < 10; i++)
{
    ExcelWorkSheet.Cells[i, 0].GetStyle().Custom = "0.00";
}

// Save the workbook.
workbook.Save("d:\\test\\decimalvalidation.xls");

下面的事例介绍了List数据验证//注:用的较多
// Create a workbook object.
Workbook workbook = new Workbook();

// Get the first worksheet.
Worksheet worksheet1 = workbook.Worksheets[0];

// Add a new worksheet and access it.
int i = workbook.Worksheets.Add();
Worksheet worksheet2 = workbook.Worksheets[i];

// Create a range in the second worksheet.
Range range = worksheet2.Cells.CreateRange("E1", "E4");

// Name the range.
range.Name = "MyRange";

// Fill different cells with data in the range.
range[0, 0].PutValue("Blue");
range[1, 0].PutValue("Red");
range[2, 0].PutValue("Green");
range[3, 0].PutValue("Yellow");

// Get the validations collection.
ValidationCollection validations = worksheet1.Validations;

// Create a new validation to the validations list.
Validation validation = validations[validations.Add()];

// Set the validation type.
validation.Type = Aspose.Cells.ValidationType.List;

// Set the operator.
validation.Operator = OperatorType.None;

// Set the in cell drop down.
validation.InCellDropDown = true;

// Set the formula1.
validation.Formula1 = "此处是下拉列表中的数据,一般是以逗号分割的字符串";

// Enable it to show error.
validation.ShowError = true;

// Set the alert type severity level.
validation.AlertStyle = ValidationAlertType.Stop;

// Set the error title.
validation.ErrorTitle = "Error";

// Set the error message.
validation.ErrorMessage = "Please select a color from the list";

// Specify the validation area.
CellArea area;
area.StartRow = 0;
area.EndRow = 4;
area.StartColumn = 0;
area.EndColumn = 0;

// Add the validation area.
validation.AreaList.Add(area);

// Save the Excel file.
workbook.Save("d:\\test\\validationtypelist.xls");

下面的事例介绍了,如何使用日期验证:
// Create a workbook.
 Workbook workbook = new Workbook();

// Obtain the cells of the first worksheet.
 Cells cells = workbook.Worksheets[0].Cells;

// Put a string value into the A1 cell.
 cells["A1"].PutValue("Please enter Date b/w 1/1/1970 and 12/31/1999");

// Wrap the text.
 cells["A1"].GetStyle().IsTextWrapped = true;

// Set row height and column width for the cells.
 cells.SetRowHeight(0, 31);
 cells.SetColumnWidth(0, 35);

// Get the validations collection.
 ValidationCollection validations = workbook.Worksheets[0].Validations;

// Add a new validation.
 Validation validation = validations[validations.Add()];

// Set the data validation type.
 validation.Type = ValidationType.Date;

// Set the operator for the data validation
 validation.Operator = OperatorType.Between;

// Set the value or expression associated with the data validation.
 validation.Formula1 = "1/1/1970";

// The value or expression associated with the second part of the data validation.
 validation.Formula2 = "12/31/1999";

// Enable the error.
 validation.ShowError = true;

// Set the validation alert style.
 validation.AlertStyle = ValidationAlertType.Stop;

// Set the title of the data-validation error dialog box
 validation.ErrorTitle = "Date Error";

// Set the data validation error message.
 validation.ErrorMessage = "Enter a Valid Date";

// Set and enable the data validation input message.
 validation.InputMessage = "Date Validation Type";
 validation.IgnoreBlank = true;
 validation.ShowInput = true;

// Set a collection of CellArea which contains the data validation settings.
 CellArea cellArea;
 cellArea.StartRow = 0;
 cellArea.EndRow = 0;
 cellArea.StartColumn = 1;
 cellArea.EndColumn = 1;

// Add the validation area.
 validation.AreaList.Add(cellArea);

// Save the Excel file.
 workbook.Save("d:\\test\\datevalidation.xls");

下面的事例介绍了,如何使用文本长度验证:
// Create a new workbook.
Workbook workbook = new Workbook();

// Obtain the cells of the first worksheet.
Cells cells = workbook.Worksheets[0].Cells;

//Put a string value into A1 cell.
cells["A1"].PutValue("Please enter a string not more than 5 chars");

// Wrap the text.
cells["A1"].GetStyle().IsTextWrapped = true;

// Set row height and column width for the cell.
cells.SetRowHeight(0, 31);
cells.SetColumnWidth(0, 35);

// Get the validations collection.
ValidationCollection validations = workbook.Worksheets[0].Validations;

// Add a new validation.
Validation validation = validations[validations.Add()];

// Set the data validation type.
validation.Type = ValidationType.TextLength;

// Set the operator for the data validation.
validation.Operator = OperatorType.LessOrEqual;

// Set the value or expression associated with the data validation.
validation.Formula1 = "5";

// Enable the error.
validation.ShowError = true;

// Set the validation alert style.
validation.AlertStyle = ValidationAlertType.Warning;

// Set the title of the data-validation error dialog box.
validation.ErrorTitle = "Text Length Error";

// Set the data validation error message.
validation.ErrorMessage = " Enter a Valid String";

// Set and enable the data validation input message.
validation.InputMessage = "TextLength Validation Type";
validation.IgnoreBlank = true;
validation.ShowInput = true;

// Set a collection of CellArea which contains the data validation settings.
CellArea cellArea;
cellArea.StartRow = 0;
cellArea.EndRow = 0;
cellArea.StartColumn = 1;
cellArea.EndColumn = 1;

// Add the validation area.
validation.AreaList.Add(cellArea);

// Save the Excel file.
workbook.Save("d:\\test\\textlengthvalidation.xls");

Aspose Cells 添加数据验证(动态下拉列表验证)的更多相关文章

  1. C# 基于Aspose.Cells的数据导出到Excel

    using Aspose.Cells;  void WriteToExcel(string filePath, List<object[]> datas, string sheetName ...

  2. .net mvc 使用 aspose.cells导出数据

    public class AsposeCellsHelper { public Workbook workBook; public Worksheet worksheet; Style style; ...

  3. .Net高级进阶,教你如何构建企业模型数据拦截层,动态控制字段验证

    现在,你有一个MVC架构的web项目,你要完成一个注册功能. 前台传了3个值到你的控制器,分别是账号.密码.邮箱. 如图:现在你要在控制器里面判断,账号名称.密码.邮箱不能为空,并且名称和密码不超过1 ...

  4. 基于 Aspose.Cells与XML导入excel 数据----操作类封装

    前言 导入excel数据, 在每个项目中基本上都会遇到,第三方插件或者基于微软office,用的最多的就是npoi,aspose.cells和c#基于office这三种方式,其中各有各的优缺点,在这也 ...

  5. thinkphp自动验证中的静态验证和动态验证和批量验证

    1.静态定义 在模型类里面预先定义好该模型的自动验证规则,我们称为静态定义. 举例说明,我们在模型类里面定义了$_validate属性如下: class UserModel extends Model ...

  6. (六)SSO之CAS框架扩展 改动CAS源代码实现与ESS动态password验证对接

    题记: 偶尔的偶尔我们会听到这个站点的数据泄露了,那个站点的用户数据泄露了.让用户又一次改动登录password,所以,对于用户数据安全性越发的引起我们的重视了,尤其是一些保密性要求高的站点.更须要添 ...

  7. 利用Aspose.Cells完成easyUI中DataGrid数据的Excel导出功能

    我准备在项目中实现该功能之前,google发现大部分代码都是利用一般处理程序HttpHandler实现的服务器端数据的Excel导出,但是这样存在的问题是ashx读取的数据一般都是数据库中视图的数据, ...

  8. wpf binging(五) 数据的转换与验证

    1.数据的验证,有时候需要验证同步的数据是否正常 需要派生一个类 ValidationRule 再把这个类指定给binging 进行验证 在这里如果验证不通过 textbox就会变成红色并且发出警告数 ...

  9. c# Aspose.Cells 通过Excel模板生产excel数据再打印

    多的不说,我们先来利用Northwind做两个小demo.先说说Aspose.Cells的模板语法: &=DataSource.Field,&=[DataSource].[Field] ...

随机推荐

  1. HDU 4513 吉哥系列故事——完美队形II (Manacher变形)

    题意:假设有n个人按顺序的身高分别是h[1], h[2] ... h[n],从中挑出一些人形成一个新的队形,新的队形若满足以下要求,则就是新的完美队形:  1.连续的 2.形成回文串 3.从左到中间那 ...

  2. 给img添加类名可以动态切换图片

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  3. hdu1150 匈牙利

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1150 题目大意:有两台机器A和B以及N个需要运行的任务.每台机器有M种不同的模式,而每个任务都恰好在 ...

  4. XSS 跨站脚本攻击之ShellCode的调用

    1.ShellCode,最初是溢出程序和蠕虫病毒的核心,实际上是指利用一个漏洞是所执行的代码,在XSS跨站脚本中,是指由javascript等脚本编写的XSS利用代码: 2.Exploit,在黑客眼里 ...

  5. Delphi中Messagedlg用法

    if MessageDlg('Welcome to my Delphi application. Exit now?', mtConfirmation, [mbYes, mbNo], 0) = mrY ...

  6. 我的c++学习(3)字符的输入输出

    #include "stdafx.h" #include<iostream> using namespace std; int main(void) { /* char ...

  7. 解决ie8和ie7显示不一致

    解决ie8和ie7显示不一致 当使用 Microsoft Internet Explorer 8 Beta 1 版本时,可能会遇到以下问题之一: • 网页布局不整齐 • 文本或图像重叠 • JavaS ...

  8. HDU5737 : Differencia

    注意到$b$不变,考虑用归并树来维护这个$b$序列,对于每个节点有序地维护$b$,同时在归并的时候预处理出每个元素在左右儿子里的排名. 在归并树上额外维护区间内$a\geq b$的个数以及赋值标记. ...

  9. HIT2715 Matrix3(最小费用最大流)

    题目大概说有一个n×n的矩阵,每个格子都有权值和高度,在这个矩阵中进行最多k次旅行,每次旅行能从当前格子走到相邻且高度更小的格子,走到格子边界就能出去完成这次旅行.每走到一个格子就累加格子的权值然后把 ...

  10. Post请求

    写在前面的话: XMLHttpRequest对象的open方法的第一个参数为request-type,取值可以为get或post.本篇介绍post请求. 使用post方式时,浏览器会把各表单中字段元素 ...