【问题标题】:Insert row at the end of an excel C# .NET在 excel C# .NET 的末尾插入行
【发布时间】:2021-11-01 00:50:02
【问题描述】:

大家早上好!

我正在尝试在所有数据的末尾向 Excel 添加一个新行。

如您所见,excel 有一个标题,然后是“billingSummary”中的所有记录。在后者的末尾,我想添加一行来计算总数。

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;

我希望你能帮助我!谢谢!

【问题讨论】:

  • 你已经设定了一个目标——是什么阻止你完成它?您尝试过哪些尝试,哪些尝试没有奏效?
  • @mason 我尝试将它与您在此处看到的注释代码一起插入。但它不起作用,因为它重叠了该行的所有内容(“HOLA”和“CHAU”)

标签: c# .net excel asp.net-mvc linq


【解决方案1】:

关于给excelDataRows设置新值时注释的代码,它正在重置列表的值,您必须使用具有列表的Add方法。

在这里,我将根据您注释的代码为您提供一个示例,说明您应该如何做。

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

return excelDataRows;

【讨论】:

  • 这对我有用!谢谢!我没有这样尝试过。
猜你喜欢
  • 2012-05-31
  • 1970-01-01
  • 2015-11-21
  • 1970-01-01
  • 2011-08-13
  • 1970-01-01
  • 2021-07-21
  • 2021-07-20
  • 2011-12-16
相关资源
最近更新 更多