Spire.XLS for .NET 是一款专业的 .NET Excel 组件, 它可以用在各种.NET 框架中,包括 .NET Core、ASP.NET 和 Windows Forms 等相关的.NET 应用程序。在Excel表格中单元格也是我们经常会使用到的格式,这时我们如何使用Spire.XLS来完成呢?这就是本系列教程将给大家详细讲解的。>>下载Spire.XLS最新试用版
C# Excel 数据排序
我们可以对Excel中的数据进行排序,Spire.XLS支持以下三种排序方式:
- 基于单元格的值
- 基于单元格的背景颜色
- 基于单元格的字体颜色
基于单元格的值
//创建Workbook实例 Workbook workbook = new Workbook(); //加载Excel文档 workbook.LoadFromFile(@"SortData.xlsx"); //获取第一个工作表 Worksheet worksheet = workbook.Worksheets[0]; //指定需要排序的列索引以及排序的方式 (基于单元格的值) SortColumn column = workbook.DataSorter.SortColumns.Add(0, SortComparsionType.Values, OrderBy.Descending); //排序是否包含标题(默认第一个数据为标题,不会对它进行排序) workbook.DataSorter.IsIncludeTitle = false; //指定要排序的单元格范围并进行排序 workbook.DataSorter.Sort(worksheet.Range["A2:A9"]); //保存文档 workbook.SaveToFile(@"SortByValues.xlsx", ExcelVersion.Version2010);
基于单元格的背景颜色
//创建Workbook实例 Workbook workbook = new Workbook(); //加载Excel文档 workbook.LoadFromFile(@"SortData.xlsx"); //获取第二个工作表 Worksheet worksheet = workbook.Worksheets[1]; //指定需要排序的列索引以及排序的方式 (基于单元格的背景颜色) SortColumn column = workbook.DataSorter.SortColumns.Add(0, SortComparsionType.BackgroundColor, OrderBy.Top); //指定背景颜色 column.Color = Color.Red; //排序是否包含标题(默认第一个数据为标题,不会对它进行排序) workbook.DataSorter.IsIncludeTitle = false; //指定要排序的单元格范围并进行排序 workbook.DataSorter.Sort(worksheet.Range["A2:A8"]); //保存文档 workbook.SaveToFile(@"SortByCellColor.xlsx", ExcelVersion.Version2010);
基于单元格的字体颜色
//创建Workbook实例 Workbook workbook = new Workbook(); //加载Excel文档 workbook.LoadFromFile(@"SortData.xlsx"); //获取第三个工作表 Worksheet worksheet = workbook.Worksheets[2]; //指定需要排序的列索引以及排序的方式 (基于单元格的字体颜色) SortColumn column = workbook.DataSorter.SortColumns.Add(0, SortComparsionType.FontColor, OrderBy.Bottom); //指定字体颜色 column.Color = Color.Red; //排序是否包含标题(默认第一个数据为标题,不会对它进行排序) workbook.DataSorter.IsIncludeTitle = false; //指定要排序的单元格范围并进行排序 workbook.DataSorter.Sort(worksheet.Range["A2:A8"]); //保存文档 workbook.SaveToFile(@"SortByFontColor.xlsx", ExcelVersion.Version2010);
C# 设置 Excel 数据验证/数据有效性
Spire.XLS 支持的数据验证类型:
- 数字验证
- 序列验证
- 文本长度验证
- 时间验证
- 日期验证
- 自定义验证
数字验证
Validation numberValidation = sheet.Range["C1"].DataValidation; //整数验证 numberValidation.AllowType = CellDataType.Integer; //小数验证 //numberValidation.AllowType = CellDataType.Decimal; //限制输入1-10之间的整数 numberValidation.CompareOperator = ValidationComparisonOperator.Between; numberValidation.Formula1 = "1"; numberValidation.Formula2 = "10";
序列验证
Validation listValidation = sheet.Range["C2"].DataValidation; listValidation.Values = new string[] { "销售", "人力资源", "研发", "财务" };
文本长度验证
Validation txtLengthvalidation = sheet.Range["C3"].DataValidation; txtLengthvalidation.AllowType = CellDataType.TextLength; txtLengthvalidation.CompareOperator = ValidationComparisonOperator.Between; //限制输入的文本长度在1-5之间 txtLengthvalidation.Formula1 = "1"; txtLengthvalidation.Formula2 = "5";
时间验证
Validation timeValidation = sheet.Range["C4"].DataValidation; timeValidation.AllowType = CellDataType.Time; timeValidation.CompareOperator = ValidationComparisonOperator.Between; //限制输入时间在00.00到24.00之间 timeValidation.Formula1 = "00.00"; timeValidation.Formula2 = "24.00";
日期验证
Validation dateValidation = sheet.Range["C5"].DataValidation; dateValidation.AllowType = CellDataType.Date; dateValidation.CompareOperator = ValidationComparisonOperator.Between; //限制输入日期在2016/5/10到2017/5/10之间 dateValidation.DateTime1 = new DateTime(2016, 5, 10); dateValidation.DateTime2 = new DateTime(2017, 5, 10);
自定义验证
Validation customValidation = sheet.Range["C6"].DataValidation; customValidation.AllowType = CellDataType.User; customValidation.Formula1 = "=A1>10";
完整代码
Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; //数字验证 Validation numberValidation = sheet.Range["C1"].DataValidation; //整数验证 numberValidation.AllowType = CellDataType.Integer; //小数验证 //numberValidation.AllowType = CellDataType.Decimal; numberValidation.CompareOperator = ValidationComparisonOperator.Between; numberValidation.Formula1 = "1"; numberValidation.Formula2 = "10"; //设置错误提示信息 numberValidation.AlertStyle = AlertStyleType.Stop; numberValidation.ShowError = true; numberValidation.ErrorTitle = "Error"; numberValidation.ErrorMessage = "请输入1-10之间的整数"; //序列验证 Validation listValidation = sheet.Range["C2"].DataValidation; listValidation.Values = new string[] { "销售", "人力资源", "研发", "财务" }; listValidation.IsSuppressDropDownArrow = false; //设置错误提示信息 listValidation.AlertStyle = AlertStyleType.Stop; listValidation.ShowError = true; listValidation.ErrorTitle = "Error"; listValidation.ErrorMessage = "请从序列中选择一个项目"; //文本长度验证 Validation txtLengthvalidation = sheet.Range["C3"].DataValidation; txtLengthvalidation.AllowType = CellDataType.TextLength; txtLengthvalidation.CompareOperator = ValidationComparisonOperator.Between; txtLengthvalidation.Formula1 = "1"; txtLengthvalidation.Formula2 = "5"; //设置错误提示信息 txtLengthvalidation.AlertStyle = AlertStyleType.Stop; txtLengthvalidation.ShowError = true; txtLengthvalidation.ErrorTitle = "Error"; txtLengthvalidation.ErrorMessage = "输入的文本长度应该在1-5之间"; //时间验证 Validation timeValidation = sheet.Range["C4"].DataValidation; timeValidation.AllowType = CellDataType.Time; timeValidation.CompareOperator = ValidationComparisonOperator.Between; timeValidation.Formula1 = "00.00"; timeValidation.Formula2 = "24.00"; //设置错误提示信息 timeValidation.AlertStyle = AlertStyleType.Stop; timeValidation.ShowError = true; timeValidation.ErrorTitle = "Error"; timeValidation.ErrorMessage = "输入的时间应该在00.00到24.00之间"; //日期验证 Validation dateValidation = sheet.Range["C5"].DataValidation; dateValidation.AllowType = CellDataType.Date; dateValidation.CompareOperator = ValidationComparisonOperator.Between; dateValidation.DateTime1 = new DateTime(2016, 5, 10); dateValidation.DateTime2 = new DateTime(2017, 5, 10); //设置错误提示信息 dateValidation.AlertStyle = AlertStyleType.Stop; dateValidation.ShowError = true; dateValidation.ErrorTitle = "Error"; dateValidation.ErrorMessage = "输入的日期应该在2016/5/10到2017/5/10之间"; //自定义验证 Validation customValidation = sheet.Range["C6"].DataValidation; customValidation.AllowType = CellDataType.User; customValidation.Formula1 = "=A1>10"; //设置错误提示信息 customValidation.AlertStyle = AlertStyleType.Stop; customValidation.ShowError = true; customValidation.ErrorTitle = "Error"; customValidation.ErrorMessage = "无法输入!A1的数据小于10"; //保存文档 workbook.SaveToFile("Output.xlsx",FileFormat.Version2013);