ExcelHelper.cs 6.1 KB

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