The following code write to excel the right count of rows, but the data inside the file contains only the last data of the list, why?
(in my case 84 rows with repeate data that inside arr[list.Capacity -1])
Thanks!!
List<string> titles = new List<string>() { "מספר רשיון", "אזור" ,"ישוב", "תאריך אחרון להגשת השגה","מין העץ", "ערך מין העץ", "סה'כ להעתקה" , "סה'כ לכריתה"
,"סה'כ לשימור", "מבקש" , "רחוב" , "מספר", "גוש" , "חלקה"};
List<string[]> list = new List<string[]>(pagedList.Capacity);
foreach (var result in pagedList)
{
list.Add(new[] {result.LicenseId?.ToString(),result.ZoneName, result.CityName?.ToString(),
result.AppealLastDate?.ToString(), result.TreeName?.ToString(), result.TreeType.HasValue ? result.TreeType.Value.ToString() :"", result.Copying?.ToString(),
result.Unproot?.ToString(), result.Conservation?.ToString(), result.ExpandRows.Select(p => p.CustomerName).FirstOrDefault(), result.ExpandRows.Select(p => p.Street).FirstOrDefault(),
result.ExpandRows.Select(p => p.HomeNumber).FirstOrDefault(),result.ExpandRows.Select(p => p.Block).FirstOrDefault(),result.ExpandRows.Select(p => p.Parcel).FirstOrDefault(),
}); ;
}
using (var pck = new ExcelPackage())
{
var workSheet = pck.Workbook.Worksheets.Add("Sheet1");
foreach (var arr in list)
{
for (int i = 1; i <= list.Capacity; i++)
{
for (int j = 1; j <= titles.Count(); j++)
{
workSheet.Cells[i, j].Value = arr[j-1];
}
}
}
}
From my comments, the 2 inner
forloops are always writing to the exact same cells, effectively overwriting the prior values which is why only the last row of data is being added to the worksheet.Only one list is needed to hold all the rows, including the titles. The following commented code should be close enough to help resolve the issue: