ExcelHelper.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using ExcelDataReader;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Http.Internal;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. namespace ZR.Common
  11. {
  12. public class ExcelHelper<T> where T : new()
  13. {
  14. /// <summary>
  15. /// 导入数据
  16. /// </summary>
  17. /// <param name="stream"></param>
  18. /// <returns></returns>
  19. //public static IEnumerable<T> ImportData(Stream stream)
  20. //{
  21. // using ExcelPackage package = new(stream);
  22. // //ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
  23. // ExcelWorksheet worksheet = package.Workbook.Worksheets[0];//读取第1个sheet
  24. // //获取表格的列数和行数
  25. // int colStart = worksheet.Dimension.Start.Column;
  26. // int colEnd = worksheet.Dimension.End.Column;
  27. // int rowStart = worksheet.Dimension.Start.Row;
  28. // int rowEnd = worksheet.Dimension.End.Row;
  29. // //int rowCount = worksheet.Dimension.Rows;
  30. // //int ColCount = worksheet.Dimension.Columns;
  31. // List<T> resultList = new();
  32. // List<PropertyInfo> propertyInfos = new();// new(typeof(T).GetProperties());
  33. // Dictionary<string, int> dictHeader = new();
  34. // for (int i = colStart; i < colEnd; i++)
  35. // {
  36. // var name = worksheet.Cells[rowStart, i].Value?.ToString();
  37. // dictHeader[name] = i;
  38. // PropertyInfo propertyInfo = MapPropertyInfo(name);
  39. // if (propertyInfo != null)
  40. // {
  41. // propertyInfos.Add(propertyInfo);
  42. // }
  43. // }
  44. // for (int row = rowStart + 1; row <= rowEnd; row++)
  45. // {
  46. // T result = new();
  47. // foreach (PropertyInfo p in propertyInfos)
  48. // {
  49. // try
  50. // {
  51. // ExcelRange cell = worksheet.Cells[row, dictHeader[p.Name]];
  52. // if (cell.Value == null)
  53. // {
  54. // continue;
  55. // }
  56. // switch (p.PropertyType.Name.ToLower())
  57. // {
  58. // case "string":
  59. // p.SetValue(result, cell.GetValue<string>());
  60. // break;
  61. // case "int16":
  62. // p.SetValue(result, cell.GetValue<short>()); break;
  63. // case "int32":
  64. // p.SetValue(result, cell.GetValue<int>()); break;
  65. // case "int64":
  66. // p.SetValue(result, cell.GetValue<long>()); break;
  67. // case "decimal":
  68. // p.SetValue(result, cell.GetValue<decimal>());
  69. // break;
  70. // case "double":
  71. // p.SetValue(result, cell.GetValue<double>()); break;
  72. // case "datetime":
  73. // p.SetValue(result, cell.GetValue<DateTime>()); break;
  74. // case "boolean":
  75. // p.SetValue(result, cell.GetValue<bool>()); break;
  76. // case "char":
  77. // p.SetValue(result, cell.GetValue<string>()); break;
  78. // default:
  79. // break;
  80. // }
  81. // }
  82. // catch (KeyNotFoundException ex)
  83. // {
  84. // Console.WriteLine("未找到该列将继续循环," + ex.Message);
  85. // continue;
  86. // }
  87. // }
  88. // resultList.Add(result);
  89. // }
  90. // return resultList;
  91. //}
  92. /// <summary>
  93. /// 查找Excel列名对应的实体属性
  94. /// </summary>
  95. /// <param name="columnName"></param>
  96. /// <returns></returns>
  97. public static PropertyInfo MapPropertyInfo(string columnName)
  98. {
  99. PropertyInfo[] propertyList = GetProperties(typeof(T));
  100. PropertyInfo propertyInfo = propertyList.Where(p => p.Name == columnName).FirstOrDefault();
  101. if (propertyInfo != null)
  102. {
  103. return propertyInfo;
  104. }
  105. else
  106. {
  107. foreach (PropertyInfo tempPropertyInfo in propertyList)
  108. {
  109. System.ComponentModel.DescriptionAttribute[] attributes = (System.ComponentModel.DescriptionAttribute[])tempPropertyInfo.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
  110. if (attributes.Length > 0)
  111. {
  112. if (attributes[0].Description == columnName)
  113. {
  114. return tempPropertyInfo;
  115. }
  116. }
  117. }
  118. }
  119. return null;
  120. }
  121. /// <summary>
  122. /// 得到类里面的属性集合
  123. /// </summary>
  124. /// <param name="type"></param>
  125. /// <param name="columns"></param>
  126. /// <returns></returns>
  127. public static PropertyInfo[] GetProperties(Type type, string[] columns = null)
  128. {
  129. PropertyInfo[] properties = null;
  130. properties = type.GetProperties();
  131. if (columns != null && columns.Length > 0)
  132. {
  133. // 按columns顺序返回属性
  134. var columnPropertyList = new List<PropertyInfo>();
  135. foreach (var column in columns)
  136. {
  137. var columnProperty = properties.Where(p => p.Name == column).FirstOrDefault();
  138. if (columnProperty != null)
  139. {
  140. columnPropertyList.Add(columnProperty);
  141. }
  142. }
  143. return columnPropertyList.ToArray();
  144. }
  145. else
  146. {
  147. return properties;
  148. }
  149. }
  150. }
  151. }