在EPPlus中调用LoadFromCollection时忽略属性

我正在尝试使用以下代码生成Excel文件:

public static Stream GenerateFileFromClass<T>(IEnumerable<T> collection, int startrow, int startcolumn, byte[]templateResource) { using (Stream template = new MemoryStream(templateResource))//this is an excel file I am using for a base/template { using (var tmpl = new ExcelPackage(template)) { ExcelWorkbook wb = tmpl.Workbook; if (wb != null) { if (wb.Worksheets.Count > 0) { ExcelWorksheet ws = wb.Worksheets.First(); ws.Cells[startrow, startcolumn].LoadFromCollection<T>(collection, false); } return new MemoryStream(tmpl.GetAsByteArray()); } else { throw new ArgumentException("Unable to load template WorkBook"); } } } } 

这工作像一个款待,但是..我想忽略我的类集合中的几个属性,所以它匹配我的模板。 我知道LoadFromCollection将基于类的公共属性生成Excel文件中的列,但是当我使用entity framework加载类时,如果将该字段标记为私有,那么EF会抱怨 – 主要是因为其中一个字段我不想显示的是密钥。

我试图标记我不想使用[XmlIgnore]的属性,无济于事。 有没有办法做到这一点,加载到一个数据集或一些这样的整个集合和修剪的列? 或者投射到没有我不需要的属性的基类?

是的,EPPlus为要包含的属性提供带有MemberInfo[]参数的.LoadFromCollection<T>()方法的重载。

这给了我们所有我们需要忽略具有某个属性的任何属性。

例如,如果我们想要忽略这个自定义属性的属性:

 public class EpplusIgnore : Attribute { } 

那么我们可以编写一个小扩展方法,首先find没有[EpplusIgnore]属性的所有MemberInfo对象,然后返回EPPlus dll中.LoadFromCollection方法的正确重载结果。

像这样的东西:

 public static class Extensions { public static ExcelRangeBase LoadFromCollectionFiltered<T>(this ExcelRangeBase @this, IEnumerable<T> collection) where T:class { MemberInfo[] membersToInclude = typeof(T) .GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(p=>!Attribute.IsDefined(p,typeof(EpplusIgnore))) .ToArray(); return @this.LoadFromCollection<T>(collection, false, OfficeOpenXml.Table.TableStyles.None, BindingFlags.Instance | BindingFlags.Public, membersToInclude); } } 

所以,举个例子,像这样使用它会在将一个Person集合导出到excel时忽略.Key属性:

 public class Person { [EpplusIgnore] public int Key { get; set; } public string Name { get; set; } public int Age { get; set; } } class Program { static void Main(string[] args) { var demoData = new List<Person> { new Person { Key = 1, Age = 40, Name = "Fred" }, new Person { Key = 2, Name = "Eve", Age = 21 } }; FileInfo fInfo = new FileInfo(@"C:\Temp\Book1.xlsx"); using (var excel = new ExcelPackage()) { var ws = excel.Workbook.Worksheets.Add("People"); ws.Cells[1, 1].LoadFromCollectionFiltered(demoData); excel.SaveAs(fInfo); } } } 

给我们期望的输出:

在这里输入图像说明

谢谢Stewart_R,根据你的工作,我做了一个新的接收属性名称:

 public static ExcelRangeBase LoadFromCollection<T>(this ExcelRangeBase @this, IEnumerable<T> collection, string[] propertyNames, bool printHeaders) where T:class { MemberInfo[] membersToInclude = typeof(T) .GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(p=>propertyNames.Contains(p.Name)) .ToArray(); return @this.LoadFromCollection<T>(collection, printHeaders, OfficeOpenXml.Table.TableStyles.None, BindingFlags.Instance | BindingFlags.Public, membersToInclude); }