MSExcelClassMapping.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. using System.Text;
  6. namespace XYY.Core.Standard.ExcelHelper.MSExcelHelper
  7. {
  8. public class MSExcelTableMapping : MSExcelClassMapping<System.Data.DataTable>
  9. {
  10. }
  11. public class MSExcelDefaultMapping<T> : MSExcelClassMapping<T>
  12. {
  13. }
  14. public abstract class MSExcelClassMapping<TClass>
  15. {
  16. public virtual int[] SheetIndexs(List<string> sheetNames)
  17. {
  18. return new int[] { 1 };
  19. }
  20. public virtual int RemarkIndex()
  21. {
  22. return 0;
  23. }
  24. public List<ExcelMappingItem> Items { get; set; } = new List<ExcelMappingItem>();
  25. public List<ExcelMappingAttrItem> Attrs { get; set; } = new List<ExcelMappingAttrItem>();
  26. //public List<ExcelMappingItem<TClass> items => new List<ExcelMappingItem>();
  27. public ExcelMappingItem Map<TMember>(Expression<Func<TClass, TMember>> expression)
  28. {
  29. var MappingItem = new ExcelMappingItem();
  30. if (expression.Body.NodeType == ExpressionType.MemberAccess)
  31. {
  32. MemberExpression memberExpression = expression.Body as MemberExpression;
  33. MappingItem = Map(memberExpression.Member);
  34. MappingItem.LambdaExpression = expression;
  35. }
  36. return MappingItem;
  37. }
  38. public ExcelMappingItem Map(MemberInfo memberInfo)
  39. {
  40. var MappingItem = new ExcelMappingItem();
  41. if (memberInfo.MemberType == MemberTypes.Property)
  42. {
  43. PropertyInfo propertyInfo = memberInfo as PropertyInfo;
  44. MappingItem.PropertyInfo = propertyInfo;
  45. var pt = MappingItem.PropertyInfo.PropertyType;
  46. MappingItem.ColumnFormattext = ExcelFormattextHelper.GetColumnFormattext(pt);
  47. Items.Add(MappingItem);
  48. return MappingItem;
  49. }
  50. else
  51. {
  52. return MappingItem;
  53. }
  54. }
  55. }
  56. }