Mohamed Maddouri
Mohamed Maddouri

Reputation: 31

Epplus loadfromCollection list of int didn't work

I'm trying to load data from a list of int and a list of string to display chart with excel using epplus library with asp.net, it works fine for list of string however when I try to load from list of int an exception appear :

System.IndexOutOfRangeException : 'Index was outside the bounds of the array.

var workSheet = ep.Workbook.Worksheets.Add("Activité");

                // specify cell values to be used for generating chart.
               // CountList : list of int
                workSheet.Cells[2, 1].LoadFromCollection(CountList); 
                // add chart of type Pie.
                var myChartt = workSheet.Drawings.AddChart("chart", eChartType.CylinderCol);

                // Define series for the chart
var seriess = myChartt.Series.Add("A2: A5", "B2: B5");
myChartt.Border.Fill.Color = System.Drawing.Color.Green;
myChartt.Title.Text = "My Chart";
myChartt.SetSize(400, 400);

Upvotes: 2

Views: 872

Answers (1)

Yahya Hussein
Yahya Hussein

Reputation: 9121

The issue is caused by the type int, to override it you can pass a list of string instead:

            var workSheet = ep.Workbook.Worksheets.Add("Activité");

            List<int> CountList= new List<int>();
            List<string> stringList= new List<string>();
            foreach (var num in CountList)
            {
                stringList.Add(num.ToString());
            }
            // specify cell values to be used for generating chart.
            // CountList : list of int
            workSheet.Cells[2, 1].LoadFromCollection(stringList);
            // add chart of type Pie.
            var myChartt = workSheet.Drawings.AddChart("chart", eChartType.CylinderCol);

            // Define series for the chart
            var seriess = myChartt.Series.Add("A2: A5", "B2: B5");
            myChartt.Border.Fill.Color = System.Drawing.Color.Green;
            myChartt.Title.Text = "My Chart";
            myChartt.SetSize(400, 400);

Seems like LoadFromCollection also fails for a list of chars, and a list of doubles so I guess that LoadFromCollection is expecting a reference value collection to be passed.

Upvotes: 3

Related Questions