DataTableExtensions.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System.Data;
  2. using KYFramework;
  3. using MongoDB.Bson;
  4. using NPOI.HSSF.UserModel;
  5. using NPOIHelper;
  6. namespace SimulationServer.Utils;
  7. public static class DataTableExtensions
  8. {
  9. public static T ToEntity<T>(this DataTable table) where T : new()
  10. {
  11. T entity = new T();
  12. for (int i = 0; i < table.Rows.Count; i++)
  13. {
  14. var properties = entity.GetType().GetProperties();
  15. for (int j = 0; j < properties.Length; j++)
  16. {
  17. Type newType = properties[j].PropertyType;
  18. //判断type类型是否为泛型,因为nullable是泛型类,
  19. if (newType.IsGenericType
  20. && newType.GetGenericTypeDefinition() == typeof(Nullable<>)) //判断convertsionType是否为nullable泛型类
  21. {
  22. //如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换
  23. System.ComponentModel.NullableConverter nullableConverter =
  24. new System.ComponentModel.NullableConverter(newType);
  25. //将type转换为nullable对的基础基元类型
  26. newType = nullableConverter.UnderlyingType;
  27. }
  28. properties[j].SetValue(entity, Convert.ChangeType(table.Rows[i][j], newType), null);
  29. }
  30. }
  31. return entity;
  32. }
  33. public static List<T> ToEntities<T>(this DataTable table) where T : new()
  34. {
  35. List<T> entities = new List<T>();
  36. if (table == null)
  37. return null;
  38. for (int i = 0; i < table.Rows.Count; i++)
  39. {
  40. T entity = new T();
  41. var properties = entity.GetType().GetProperties();
  42. for (int j = 0; j < properties.Length; j++)
  43. {
  44. Type newType = properties[j].PropertyType;
  45. //判断type类型是否为泛型,因为nullable是泛型类,
  46. if (newType.IsGenericType
  47. && newType.GetGenericTypeDefinition() == typeof(Nullable<>)) //判断convertsionType是否为nullable泛型类
  48. {
  49. //如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换
  50. System.ComponentModel.NullableConverter nullableConverter =
  51. new System.ComponentModel.NullableConverter(newType);
  52. //将type转换为nullable对的基础基元类型
  53. newType = nullableConverter.UnderlyingType;
  54. }
  55. properties[j].SetValue(entity, Convert.ChangeType(table.Rows[i][j], newType), null);
  56. }
  57. entities.Add(entity);
  58. }
  59. return entities;
  60. }
  61. public static void SaveToExcel(string path, Dictionary<string, Dictionary<string, List<string>>> report, bool withA = false, bool zt = false, string s = "")
  62. {
  63. if (!File.Exists(path)) File.Create(path).Close();
  64. // 创建一个新的Excel文件
  65. var workbook = new HSSFWorkbook();
  66. foreach (KeyValuePair<string, Dictionary<string, List<string>>> kv in report)
  67. {
  68. // 创建一个新的工作表
  69. var sheet = workbook.CreateSheet(kv.Key);
  70. // 创建标题行
  71. var headerRow = sheet.CreateRow(0);
  72. var headerCell = headerRow.CreateCell(0);
  73. headerCell.SetCellValue("指标名");
  74. // 遍历字典,将数据写入Excel
  75. int rowIndex = 1;
  76. foreach (KeyValuePair<string, List<string>> kv1 in kv.Value)
  77. {
  78. var row = sheet.CreateRow(rowIndex);
  79. row.CreateCell(0).SetCellValue(kv1.Key);
  80. if (withA)
  81. {
  82. for (int i = 0; i < kv1.Value.Count - 1; i++)
  83. {
  84. var headerCell1 = headerRow.CreateCell(i + 1);
  85. headerCell1.SetCellValue($"仿真轮次{i + 1}");
  86. row.CreateCell(i + 1).SetCellValue(kv1.Value[i]);
  87. }
  88. var headerCell1End = headerRow.CreateCell(kv1.Value.Count);
  89. headerCell1End.SetCellValue($"平均值");
  90. row.CreateCell(kv1.Value.Count).SetCellValue(kv1.Value[kv1.Value.Count - 1]);
  91. }
  92. else
  93. {
  94. for (int i = 0; i < kv1.Value.Count; i++)
  95. {
  96. var headerCell1 = headerRow.CreateCell(i + 1);
  97. headerCell1.SetCellValue($"仿真轮次{i + 1}");
  98. row.CreateCell(i + 1).SetCellValue(kv1.Value[i]);
  99. }
  100. }
  101. rowIndex++;
  102. }
  103. }
  104. if (zt)
  105. {
  106. var sheet = workbook.CreateSheet("任务成功率");
  107. var Row0 = sheet.CreateRow(0);
  108. var Row0Cell0 = Row0.CreateCell(0);
  109. Row0Cell0.SetCellValue("指标名");
  110. var Row0Cell1 = Row0.CreateCell(1);
  111. Row0Cell1.SetCellValue("数值");
  112. var Row1 = sheet.CreateRow(1);
  113. var Row1Cell0 = Row1.CreateCell(0);
  114. Row1Cell0.SetCellValue("任务成功率");
  115. var Row1Cell1 = Row1.CreateCell(1);
  116. Row1Cell1.SetCellValue(s);
  117. }
  118. // 保存Excel文件
  119. using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))
  120. {
  121. workbook.Write(fs);
  122. }
  123. }
  124. public static void SaveToExcel(string path, Dictionary<string, Dictionary<string, string>> report)
  125. {
  126. // 创建一个新的Excel文件
  127. var workbook = new HSSFWorkbook();
  128. foreach (KeyValuePair<string, Dictionary<string, string>> kv in report)
  129. {
  130. // 创建一个新的工作表
  131. var sheet = workbook.CreateSheet(kv.Key);
  132. // 创建标题行
  133. var headerRow = sheet.CreateRow(0);
  134. var headerCell = headerRow.CreateCell(0);
  135. var headerRow1 = sheet.CreateRow(1);
  136. var headerCell1 = headerRow1.CreateCell(1);
  137. headerCell.SetCellValue("指标名");
  138. headerCell1.SetCellValue("仿真轮次");
  139. // 遍历字典,将数据写入Excel
  140. int rowIndex = 1;
  141. foreach (KeyValuePair<string, string> kv1 in kv.Value)
  142. {
  143. var row = sheet.CreateRow(rowIndex);
  144. row.CreateCell(0).SetCellValue(kv1.Key);
  145. row.CreateCell(1).SetCellValue(kv1.Value);
  146. rowIndex++;
  147. }
  148. }
  149. // 保存Excel文件
  150. using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))
  151. {
  152. workbook.Write(fs);
  153. }
  154. }
  155. }