Epplus中利用对象集合来生成Excel内容,共有四种方式:
public ExcelRangeBase LoadFromCollection<T>(IEnumerable<T> Collection); public ExcelRangeBase LoadFromCollection<T>(IEnumerable<T> Collection, bool PrintHeaders); public ExcelRangeBase LoadFromCollection<T>(IEnumerable<T> Collection, bool PrintHeaders, TableStyles TableStyle); public ExcelRangeBase LoadFromCollection<T>(IEnumerable<T> Collection, bool PrintHeaders, TableStyles TableStyle, BindingFlags memberFlags, MemberInfo[] Members);
LoadFromCollection 默认是在Excel中生成对象中所有的属性的数据。 当想只生成对象中部分属性的内容,需要调用 LoadFromCollection<T>(IEnumerable<T> Collection, bool PrintHeaders, TableStyles TableStyle, BindingFlags memberFlags, MemberInfo[] Members)
1 Type typeFromHandle = typeof(TargetObj); //TargetObj 是 IEnumerable中T的类型 2 MemberInfo[] properties = typeFromHandle.GetProperties(memberFlags); 3 //过滤出需要导出Excel中的属性 4 var members = properties.FindAll(filterFunction); 5 workSheet.Cells.LoadFromCollection(collections, true, OfficeOpenXml.Table.TableStyles.None, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, members)