精通 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()返回選定儲存格範圍中的最大數字。
為了更靈活,上述功能也可以應用於單個/多個行或列。 了解更多關於選擇行和列。