2

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);
Yahya Hussein
  • 8,767
  • 15
  • 58
  • 114
  • Possible duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – VDWWD Jan 25 '18 at 08:55
  • what is listTypeActivite? and what do you use lstNew for? – Yahya Hussein Jan 25 '18 at 09:26
  • listTypeActivite is a list of string and loadFromCollection work fine with it. lstNew was just for test, it convert CountList from List of int to List of strings, – Mohamed Maddouri Jan 25 '18 at 09:28

1 Answers1

3

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.

Yahya Hussein
  • 8,767
  • 15
  • 58
  • 114