using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using OfficeOpenXml; namespace ARW.Common { public class ExcelHelper where T : new() { /// /// 导入数据 /// /// /// public static IEnumerable ImportData(Stream stream) { using ExcelPackage package = new(stream); ExcelPackage.LicenseContext = LicenseContext.NonCommercial; ExcelWorksheet worksheet = package.Workbook.Worksheets[0];//读取第1个sheet //获取表格的列数和行数 int colStart = worksheet.Dimension.Start.Column; int colEnd = worksheet.Dimension.End.Column; int rowStart = worksheet.Dimension.Start.Row; int rowEnd = worksheet.Dimension.End.Row; //int rowCount = worksheet.Dimension.Rows; //int ColCount = worksheet.Dimension.Columns; List resultList = new(); List propertyInfos = new();// new(typeof(T).GetProperties()); Dictionary dictHeader = new(); for (int i = colStart; i <= colEnd; i++) { var name = worksheet.Cells[rowStart, i].Value.ToString(); dictHeader[name] = i; PropertyInfo propertyInfo = MapPropertyInfo(name); if (propertyInfo != null) { propertyInfos.Add(propertyInfo); } } for (int row = rowStart + 1; row <= rowEnd; row++) { T result = new(); foreach (PropertyInfo p in propertyInfos) { try { foreach (var i in p.CustomAttributes) { foreach (var i2 in i.NamedArguments) { ExcelRange cell = worksheet.Cells[row, dictHeader[i2.TypedValue.Value.ToString()]]; if (cell.Value == null) { continue; } switch (p.PropertyType.Name.ToLower()) { case "string": p.SetValue(result, cell.GetValue()); break; case "int16": p.SetValue(result, cell.GetValue()); break; case "int32": p.SetValue(result, cell.GetValue()); break; case "int64": p.SetValue(result, cell.GetValue()); break; case "decimal": p.SetValue(result, cell.GetValue()); break; case "double": p.SetValue(result, cell.GetValue()); break; case "datetime": p.SetValue(result, cell.GetValue()); break; case "boolean": p.SetValue(result, cell.GetValue()); break; case "char": p.SetValue(result, cell.GetValue()); break; default: break; } } } } catch (KeyNotFoundException ex) { Console.WriteLine("未找到该列将继续循环," + ex.Message); continue; } } resultList.Add(result); } return resultList; } /// /// 查找Excel列名对应的实体属性 /// /// /// public static PropertyInfo MapPropertyInfo(string columnName) { PropertyInfo[] propertyList = GetProperties(typeof(T)); //PropertyInfo propertyInfo = propertyList.Where(p => p.Name == columnName).FirstOrDefault(); foreach (var item in propertyList) { foreach (var i in item.CustomAttributes) { if (i.AttributeType.Name == "EpplusTableColumnAttribute") { foreach (var i2 in i.NamedArguments) { if (columnName == i2.TypedValue.Value.ToString()) { return item; } } } } } return null; } /// /// 得到类里面的属性集合 /// /// /// /// public static PropertyInfo[] GetProperties(Type type, string[] columns = null) { PropertyInfo[] properties = null; properties = type.GetProperties(); if (columns != null && columns.Length > 0) { // 按columns顺序返回属性 var columnPropertyList = new List(); foreach (var column in columns) { var columnProperty = properties.Where(p => p.Name == column).FirstOrDefault(); if (columnProperty != null) { columnPropertyList.Add(columnProperty); } } return columnPropertyList.ToArray(); } else { return properties; } } } }