Epplus example of `LoadFromCollection(IEnumerable, Boolean, TableStyles, BindingFlags, MemberInfo[])`

前端 未结 1 1293
感情败类
感情败类 2020-12-30 12:28

When using Epplus ExcelPackage, I typically use a basic LoadFromCollection function to populate a Worksheet, like in this manner:

worksheet.Cell         


        
相关标签:
1条回答
  • 2020-12-30 13:04

    LoadFromCollection basically allow you to use Generics as the source Collection that implements IEnumerable<T> and the function will use the T type from that collection. So, say, a List of thingy objects can be consumed by it. Here is a thread that demonstrates it:

    EPPlus - LoadFromCollection - Text converted to number

    The collection datalist is

    var datalist = new List<TestObject>();
    

    is passed into the function

    worksheet.Cells.LoadFromCollection(datalist);
    

    As for the overloads, the longest one (the one you show) takes the MemberInfo's of the T object as the last parameter. If Members is null (via the other overloads) it simply does

    Members = type.GetProperties(memberFlags);
    

    where type is

    var type = typeof(T);
    

    You can supply your own but it is rarely necessary unless you are doing some very specific. The above memberFlags is the typical Public and Instance property types (again not usually necessary to supply your own).

    If you want to get a better idea of this you can checkout the source doe here:

    http://epplus.codeplex.com/SourceControl/latest#EPPlus/ExcelRangeBase.cs

    RESPONSE TO COMMENTS

    Here is a proper example relevant to the question:

    public class TestObject
    {
        public int Col1 { get; set; }
        public int Col2 { get; set; }
        public string Col3 { get; set; }
        public DateTime Col4 { get; set; }
    }
    
    [TestMethod]
    public void LoadFromCollection_MemberList_Test()
    {
        //https://stackoverflow.com/questions/32587834/epplus-loadfromcollection-text-converted-to-number/32590626#32590626
    
        var TestObjectList = new List<TestObject>();
        for (var i = 0; i < 10; i++)
            TestObjectList.Add(new TestObject {Col1 = i, Col2 = i*10, Col3 = (i*10) + "E4"});
    
        //Create a test file
        var fi = new FileInfo(@"c:\temp\LoadFromCollection_MemberList_Test.xlsx");
        if (fi.Exists)
            fi.Delete();
    
        using (var pck = new ExcelPackage(fi))
        {
            //Do NOT include Col1
            var mi = typeof (TestObject)
                .GetProperties()
                .Where(pi => pi.Name != "Col1")
                .Select(pi => (MemberInfo)pi)
                .ToArray();
    
            var worksheet = pck.Workbook.Worksheets.Add("Sheet1");
            worksheet.Cells.LoadFromCollection(
                TestObjectList
                , true
                , TableStyles.Dark1
                , BindingFlags.Public| BindingFlags.Instance
                , mi);
    
            pck.Save();
        }
    }
    

    Notice that Col1 is NOT in the output:

    0 讨论(0)
提交回复
热议问题