在EPPlus中调用LoadFromCollection时忽略属性

nat*_*nat 9 c# excel epplus entity-framework-6

我正在尝试使用以下代码生成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");
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这就像一种享受,但是..我想忽略我的类集合中的一些属性,因此它与我的模板匹配.我知道LoadFromCollection将根据类的公共属性在Excel文件中生成列,但是当我使用Entity Framework加载类时,如果我将该字段标记为私有,那么EF会抱怨 - 主要是因为其中一个字段我不想显示是关键.

我试图标记我不想使用的属性[XmlIgnore],但无济于事.有没有办法做到这一点,没有将整个集合加载到数据集或其他一些数据集,并修剪列中的列?或者在没有我不需要的属性的情况下转换为基类?

Ste*_*t_R 18

是的,EPPlus提供了.LoadFromCollection<T>()方法的重载,其中MemberInfo[]包含您希望包含的属性的参数.

这给了我们所有我们需要忽略具有特定属性的任何属性.

例如,如果我们想要忽略具有此自定义属性的属性:

public class EpplusIgnore : Attribute { }
Run Code Online (Sandbox Code Playgroud)

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

像这样的东西:

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

因此,例如,像这样使用它会.Key在将Person集合导出到excel 时忽略该属性:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

给出我们期望的输出:

在此输入图像描述