C# 委托
原文鏈接:http://www.rzrgm.cn/ysmc/p/18800819
C# 委托(Delegate)
介紹(摘至網絡)
在 C# 中,委托(Delegate) 是一種類型安全的函數指針,它允許將方法作為參數傳遞給其他方法
C# 中的委托(Delegate)類似于 C 或 C++ 中函數的指針。委托(Delegate) 是存有對某個方法的引用的一種引用類型變量,引用可在運行時被改變
委托在 C# 中非常常見,用于事件處理、回調函數、LINQ 等操作
所有的委托(Delegate)都派生自 System.Delegate 類
正文
委托,相信小伙伴們都知道這個玩意,但是很多小伙伴不知道這個東西在開發中能有什么用,我這里給小伙伴們一個方向,后續大家就可以擴散一下了
在工作中,大家應該是逃不了導出excel的,但是每有一個需求,就要寫一遍重復代碼,時間久了,就會自己寫出一個通用的導出功能,但是只是適合簡單的導出,晚上隨便找個包都能解決,往往也會出現一些較為復雜的需求,例如某一個字段值根據區間給予不同的背景色,或者是需要加粗字體、給不同的字體顏色等等。
我這里就舉一個例子,班級的學生成績導出,不及格(60以下)的科目成績給予紅色背景色,一班為重點班不到80分也記為紅色背景色
例子
首先我們寫一個學生成績類 student.cs
1 public class StudentGrade 2 { 3 /// <summary> 4 /// 班級 5 /// </summary> 6 [NotNull] 7 [DisplayName("班級")] 8 public string? Class { get; set; } 9 10 /// <summary> 11 /// 學號 12 /// </summary> 13 [NotNull] 14 [DisplayName("學號")] 15 public string? StudentId { get; set; } 16 17 /// <summary> 18 /// 科目 19 /// </summary> 20 [NotNull] 21 [DisplayName("科目")] 22 public string? Subject { get; set; } 23 24 /// <summary> 25 /// 成績 26 /// </summary> 27 [NotNull] 28 [DisplayName("成績")] 29 public double Grade { get; set; } 30 }
然后,我們來一個導出excel的函數,我比較懶,意思意思能懂就好了
1 /// <summary> 2 /// 導出數據到 Excel 3 /// </summary> 4 /// <typeparam name="T"></typeparam> 5 /// <param name="data"></param> 6 /// <param name="func"></param> 7 /// <returns></returns> 8 public async Task<Stream> ExportToExcelAsync<T>(IEnumerable<T> data, Func<string, T, CellStyle?>? func) 9 { 10 using var package = new ExcelPackage(); 11 var worksheet = package.Workbook.Worksheets.Add("StudentGrades"); 12 13 var properties = typeof(T).GetProperties(); 14 // 處理表頭,獲取屬性上的 DisplayNameAttribute 特性 15 foreach (var property in properties) 16 { 17 var index = Array.IndexOf(properties, property); 18 var attributes = property.GetCustomAttributes(typeof(DisplayNameAttribute), false) as IEnumerable<DisplayNameAttribute>; 19 if (attributes == null || !attributes.Any()) 20 { 21 worksheet.Cells[1, index + 1].Value = property.Name; 22 } 23 else 24 { 25 worksheet.Cells[1, index + 1].Value = attributes.First().DisplayName; 26 } 27 } 28 29 // 處理數據 30 for (int i = 0; i < data.Count(); i++) 31 { 32 var item = data.ElementAt(i); 33 for (int j = 0; j < properties.Length; j++) 34 { 35 var value = properties[j].GetValue(item); 36 if (func != null) 37 { 38 var cellStyle = func(properties[j].Name, item); 39 if (cellStyle != null) 40 { 41 worksheet.Cells[i + 2, j + 1].Style.Font.Color.SetColor(cellStyle.FontColor); 42 worksheet.Cells[i + 2, j + 1].Style.Font.Size = cellStyle.FontSize; 43 worksheet.Cells[i + 2, j + 1].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; 44 worksheet.Cells[i + 2, j + 1].Style.Fill.BackgroundColor.SetColor(cellStyle.BackgroundColor); 45 } 46 } 47 worksheet.Cells[i + 2, j + 1].Value = value; 48 } 49 } 50 51 return await Task.FromResult(package.Stream); 52 }
單元格樣式 CellStyle
1 public class CellStyle 2 { 3 /// <summary> 4 /// 字體顏色 5 /// </summary> 6 public Color FontColor { get; set; } 7 8 /// <summary> 9 /// 字體大小 10 /// </summary> 11 public float FontSize { get; set; } 12 13 /// <summary> 14 /// 背景色 15 /// </summary> 16 public Color BackgroundColor { get; set; } 17 }
這樣,當你在調用通用導出的時候,由你自己處理每一個單元格的格式就好了,處理的字段columnName,以及整一行數據都給你了
1 var data = new List<StudentGrade> 2 { 3 new StudentGrade { Class = "一班", StudentId = "001", Subject = "語文", Grade = 90 }, 4 new StudentGrade { Class = "一班", StudentId = "001", Subject = "數學", Grade = 80 }, 5 new StudentGrade { Class = "一班", StudentId = "001", Subject = "英語", Grade = 70 }, 6 new StudentGrade { Class = "一班", StudentId = "002", Subject = "語文", Grade = 85 }, 7 new StudentGrade { Class = "一班", StudentId = "002", Subject = "數學", Grade = 75 }, 8 new StudentGrade { Class = "一班", StudentId = "002", Subject = "英語", Grade = 65 }, 9 new StudentGrade { Class = "二班", StudentId = "003", Subject = "語文", Grade = 95 }, 10 new StudentGrade { Class = "二班", StudentId = "003", Subject = "數學", Grade = 85 }, 11 new StudentGrade { Class = "二班", StudentId = "003", Subject = "英語", Grade = 75 }, 12 new StudentGrade { Class = "二班", StudentId = "004", Subject = "語文", Grade = 100 }, 13 new StudentGrade { Class = "二班", StudentId = "004", Subject = "數學", Grade = 90 }, 14 new StudentGrade { Class = "二班", StudentId = "004", Subject = "英語", Grade = 80 } 15 }; 16 var cellStyle = new CellStyle 17 { 18 FontColor = Color.Black, 19 FontSize = 12, 20 BackgroundColor = Color.Red 21 }; 22 var stream = await ExportToExcelAsync(data, (columnName, item) => 23 { 24 if (columnName == "Grade" && (item.Grade < 60 25 || (item.Class == "一班" && item.Grade < 80))) 26 { 27 return cellStyle; 28 } 29 return null; 30 }); 31 using var fileStream = new FileStream("StudentGrades.xlsx", FileMode.Create); 32 stream.CopyTo(fileStream);
好了,感謝大佬們的觀看!
本文來自博客園,作者:一事冇誠,轉載請注明原文鏈接:http://www.rzrgm.cn/ysmc/p/18800819

浙公網安備 33010602011771號