Home > Enterprise >  Insert row at the end of an excel C# .NET
Insert row at the end of an excel C# .NET

Time:09-17

Good morning everyone!

I am trying to add a new row to an excel at the end of all data.

As you can see, the excel has a header and then all the records from the "billingSummary". At the end of the latter, I would like to add a row to calculate the totals.

var excelDataRows = new List<ExcelDataTableRow>
        {
            new ExcelDataTableRow
            {
                CellValues = new List<string>
                {
                    "Fecha",
                    "Contrato",
                    "Consorcio",
                    "Unidad",
                    "Importe Cobrado",
                    "Comisión Plataforma",
                    "Neto",
                    "Factura"
                }
            }
        };

        excelDataRows.AddRange(billingSummary.OrderBy(x => x.PaymentDate)
            .ThenBy(x => x.OwnersAssociationCode)
            .ThenBy(x => x.FunctionalUnitCode)
            .Select(item => new ExcelDataTableRow
            {
                CellValues = new List<string>
                {
                    item.PaymentDate.Day.ToString()   '/'   item.PaymentDate.Month.ToString()   '/'   item.PaymentDate.Year.ToString(),
                    item.ContractCode.ToString(),
                    item.OwnersAssociationCode.ToString(),
                    item.FunctionalUnitCode.ToString(),
                    item.TotalAmount.ToStringCurrency(),
                    item.PlapsaComission.ToStringCurrency(),
                    item.NetAmount.ToStringCurrency(),
                    item.Type   ' '   item.PointOfSale   '-'   item.Number,
                }
            }));

        //excelDataRows = new List<ExcelDataTableRow>
        //{
        //    new ExcelDataTableRow
        //    {
        //        CellValues = new List<string>
        //        {
        //            "HOLA",
        //            "CHAU"
        //        }
        //    }
        //};

        return excelDataRows;

I hope you can help me! Thank you!

CodePudding user response:

With respect to the code that has commented when setting a new value to excelDataRows it is resetting the value of the list, what you have to use is the Add method that has the list.

Here I leave you an example of how you should do it based on the code that you have commented.

excelDataRows.Add(new ExcelDataTableRow
    {
        CellValues = new List<string>
        {
            "HOLA",
            "CHAU"
        }
    }
); 

return excelDataRows;
  • Related