在 C# 中掌握 Excel 汇总函数
上面的代码示例展示了如何轻松地从Excel电子表格中的单元格范围获取汇总值。 但请注意,任何非数字单元格都将被省略。
总和
Sum()
方法返回选定单元格范围的总和。
平均
方法Avg()
返回所选单元格范围的平均值。
最低限度
方法Min()
返回所选单元格范围中的最小数。
最大
Max()
方法返回选定单元格范围中的最大数值。
为了增加灵活性,上述功能也可以应用于单个/多个行或列。 了解更多关于选择行和列的信息。
using IronXL; using System.Linq; WorkBook workBook = WorkBook.Load("sample.xls"); WorkSheet workSheet = workBook.WorkSheets.First(); // Get range from worksheet var range = workSheet["A1:A8"]; // Apply sum of all numeric cells within the range decimal sum = range.Sum(); // Apply average value of all numeric cells within the range decimal avg = range.Avg(); // Identify maximum value of all numeric cells within the range decimal max = range.Max(); // Identify minimum value of all numeric cells within the range decimal min = range.Min();
Imports IronXL Imports System.Linq Private workBook As WorkBook = WorkBook.Load("sample.xls") Private workSheet As WorkSheet = workBook.WorkSheets.First() ' Get range from worksheet Private range = workSheet("A1:A8") ' Apply sum of all numeric cells within the range Private sum As Decimal = range.Sum() ' Apply average value of all numeric cells within the range Private avg As Decimal = range.Avg() ' Identify maximum value of all numeric cells within the range Private max As Decimal = range.Max() ' Identify minimum value of all numeric cells within the range Private min As Decimal = range.Min()
Install-Package IronXL.Excel
上面的代码示例展示了如何轻松地从Excel电子表格中的单元格范围获取汇总值。 但请注意,任何非数字单元格都将被省略。
Sum()
方法返回选定单元格范围的总和。
方法Avg()
返回所选单元格范围的平均值。
方法Min()
返回所选单元格范围中的最小数。
Max()
方法返回选定单元格范围中的最大数值。
为了增加灵活性,上述功能也可以应用于单个/多个行或列。 了解更多关于选择行和列的信息。