文檔首頁>>Spire.XLS 系列教程>>Spire.XLS數(shù)據(jù)處理系列教程:如何數(shù)據(jù)排序和數(shù)據(jù)驗證
Spire.XLS數(shù)據(jù)處理系列教程:如何數(shù)據(jù)排序和數(shù)據(jù)驗證
更多資源查看:Spire.XLS工作表教程 | Spire.Doc系列教程 | Spire.PDF系列教程
Spire.XLS for .NET 是一款專業(yè)的 .NET Excel 組件, 它可以用在各種.NET 框架中,包括 .NET Core、ASP.NET 和 Windows Forms 等相關(guān)的.NET 應用程序。在Excel表格中單元格也是我們經(jīng)常會使用到的格式,這時我們?nèi)绾问褂肧pire.XLS來完成呢?這就是本系列教程將給大家詳細講解的。
C# Excel 數(shù)據(jù)排序
我們可以對Excel中的數(shù)據(jù)進行排序,Spire.XLS支持以下三種排序方式:
- 基于單元格的值
- 基于單元格的背景顏色
- 基于單元格的字體顏色
基于單元格的值
//創(chuàng)建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); //排序是否包含標題(默認第一個數(shù)據(jù)為標題,不會對它進行排序) workbook.DataSorter.IsIncludeTitle = false; //指定要排序的單元格范圍并進行排序 workbook.DataSorter.Sort(worksheet.Range["A2:A9"]); //保存文檔 workbook.SaveToFile(@"SortByValues.xlsx", ExcelVersion.Version2010);
基于單元格的背景顏色
//創(chuàng)建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; //排序是否包含標題(默認第一個數(shù)據(jù)為標題,不會對它進行排序) workbook.DataSorter.IsIncludeTitle = false; //指定要排序的單元格范圍并進行排序 workbook.DataSorter.Sort(worksheet.Range["A2:A8"]); //保存文檔 workbook.SaveToFile(@"SortByCellColor.xlsx", ExcelVersion.Version2010);
基于單元格的字體顏色
//創(chuàng)建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; //排序是否包含標題(默認第一個數(shù)據(jù)為標題,不會對它進行排序) workbook.DataSorter.IsIncludeTitle = false; //指定要排序的單元格范圍并進行排序 workbook.DataSorter.Sort(worksheet.Range["A2:A8"]); //保存文檔 workbook.SaveToFile(@"SortByFontColor.xlsx", ExcelVersion.Version2010);
C# 設(shè)置 Excel 數(shù)據(jù)驗證/數(shù)據(jù)有效性
Spire.XLS 支持的數(shù)據(jù)驗證類型:
- 數(shù)字驗證
- 序列驗證
- 文本長度驗證
- 時間驗證
- 日期驗證
- 自定義驗證
數(shù)字驗證
Validation numberValidation = sheet.Range["C1"].DataValidation; //整數(shù)驗證 numberValidation.AllowType = CellDataType.Integer; //小數(shù)驗證 //numberValidation.AllowType = CellDataType.Decimal; //限制輸入1-10之間的整數(shù) numberValidation.CompareOperator = ValidationComparisonOperator.Between; numberValidation.Formula1 = "1"; numberValidation.Formula2 = "10";
序列驗證
Validation listValidation = sheet.Range["C2"].DataValidation; listValidation.Values = new string[] { "銷售", "人力資源", "研發(fā)", "財務(wù)" };
文本長度驗證
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]; //數(shù)字驗證 Validation numberValidation = sheet.Range["C1"].DataValidation; //整數(shù)驗證 numberValidation.AllowType = CellDataType.Integer; //小數(shù)驗證 //numberValidation.AllowType = CellDataType.Decimal; numberValidation.CompareOperator = ValidationComparisonOperator.Between; numberValidation.Formula1 = "1"; numberValidation.Formula2 = "10"; //設(shè)置錯誤提示信息 numberValidation.AlertStyle = AlertStyleType.Stop; numberValidation.ShowError = true; numberValidation.ErrorTitle = "Error"; numberValidation.ErrorMessage = "請輸入1-10之間的整數(shù)"; //序列驗證 Validation listValidation = sheet.Range["C2"].DataValidation; listValidation.Values = new string[] { "銷售", "人力資源", "研發(fā)", "財務(wù)" }; listValidation.IsSuppressDropDownArrow = false; //設(shè)置錯誤提示信息 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"; //設(shè)置錯誤提示信息 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"; //設(shè)置錯誤提示信息 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); //設(shè)置錯誤提示信息 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"; //設(shè)置錯誤提示信息 customValidation.AlertStyle = AlertStyleType.Stop; customValidation.ShowError = true; customValidation.ErrorTitle = "Error"; customValidation.ErrorMessage = "無法輸入!A1的數(shù)據(jù)小于10"; //保存文檔 workbook.SaveToFile("Output.xlsx",FileFormat.Version2013);
*想要購買Spire.XLS正版授權(quán)的朋友可以聯(lián)系慧都客服哦~