Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EPPlus: "Column out of range" error with LoadFromCollection

Tags:

epplus

I am getting a "Column out of range" error with LoadFromCollection - code follows. Insert into the SampleApp that comes with EPPlus to replicate.

Did I do something screwy or is it a bug? Or a config setting that I haven't set?

public class tst
    {
        public string Name;
        [Description("Created Time")]
        public DateTime Created_time;
    }


     var pck = new ExcelPackage();

            var kpcollection = new List<tst>();
            for (var i = 1; i <= 10; i++)
            {
                kpcollection.Add(new tst
                {
                    Name = "line" + i.ToString(),
                    Created_time = DateTime.Now
                });
            }

            var wsenum = pck.Workbook.Worksheets.Add("KPTest");
            //Load the collection starting from cell A1...
            wsenum.Cells["A1"].LoadFromCollection(kpcollection, true, TableStyles.Medium9);
like image 351
kpollock Avatar asked Jul 04 '18 09:07

kpollock


2 Answers

Add Properties like { get; set;} for each variable, because the way epplus works is on properties of class.

For example, instead of using:

class student
{
    int num;
   string name;
}

use this:

class student
{
     int num { get; set; }
     string name { get; set; }
}
like image 98
Harish Patil Avatar answered Oct 23 '22 03:10

Harish Patil


This is a bug in EPPlus library and it has been reported on September 25, 2017.

like image 43
wha7ever Avatar answered Oct 23 '22 05:10

wha7ever