Search code examples
c#excelepplus

How to get all elements from a List


I add each day of the current month to a list in a specific format.

public static List<string> GetMonthDays()
{
    DateTime startTime = DateTime.Now;
    DateTime startOfMonth = new DateTime(startTime.Year, startTime.Month, 1);
    int daysInMonth = DateTime.DaysInMonth(startTime.Year, startTime.Month);
    List<string> currentMonth = new List<string>();
    //string[] currentMonth = new string[daysInMonth];
    for (int i = 0; i < daysInMonth; ++i) {

        DateTime currentDate = startOfMonth.AddDays(i);
        currentMonth.Add(currentDate.ToString("dddd - dd.MM.yy", 
        CultureInfo.InvariantCulture));              
    }
    return currentMonth;
}

I am currently calling the method as follows, but I can only call a single index. How can I retrieve all the elements from the list?

var monthCurrent = new List<string> { GetMonthDays()[0] };
ws.Cells["A4:A34"].LoadFromArrays(new List<string[]>(new[] { monthCurrent.ToArray() }));

Solution

  • Why not simply:

    ws.Cells["A4:A34"].LoadFromArrays(GetMonthDays().ToArray());
    

    Edit: I see, LoadFromArrays takes multiple. Then use LoadFromCollection:

    ws.Cells["A4:A34"].LoadFromCollection(GetMonthDays());