Master Excel Aggregate Functions in C#

The code example above shows how to easily get aggregate values from a range of cells in an Excel spreadsheet. However, please note that any non-numerical cell will be left out.

Sum

The method Sum() returns the total sum of the selected range of cells.

// This C# code demonstrates how to calculate the sum of a range of cells in an Excel spreadsheet.
ExcelWorksheet worksheet = package.Workbook.Worksheets["Sheet1"];

// Define the range of cells you want to sum.
var cellRange = worksheet.Cells["A1:A10"];

// Calculate the sum of the numbers in the specified range.
double sum = cellRange.Sum();

// Output the result to the console.
Console.WriteLine($"The sum of the selected cells is: {sum}");
// This C# code demonstrates how to calculate the sum of a range of cells in an Excel spreadsheet.
ExcelWorksheet worksheet = package.Workbook.Worksheets["Sheet1"];

// Define the range of cells you want to sum.
var cellRange = worksheet.Cells["A1:A10"];

// Calculate the sum of the numbers in the specified range.
double sum = cellRange.Sum();

// Output the result to the console.
Console.WriteLine($"The sum of the selected cells is: {sum}");
' This C# code demonstrates how to calculate the sum of a range of cells in an Excel spreadsheet.
Dim worksheet As ExcelWorksheet = package.Workbook.Worksheets("Sheet1")

' Define the range of cells you want to sum.
Dim cellRange = worksheet.Cells("A1:A10")

' Calculate the sum of the numbers in the specified range.
Dim sum As Double = cellRange.Sum()

' Output the result to the console.
Console.WriteLine($"The sum of the selected cells is: {sum}")
$vbLabelText   $csharpLabel

Average

The method Average() returns the average of the selected range of cells.

// This C# code demonstrates how to calculate the average of a range of cells in an Excel spreadsheet.
ExcelWorksheet worksheet = package.Workbook.Worksheets["Sheet1"];

// Define the range of cells you want to average.
var cellRange = worksheet.Cells["A1:A10"];

// Calculate the average of the numbers in the specified range.
double average = cellRange.Average();

// Output the result to the console.
Console.WriteLine($"The average of the selected cells is: {average}");
// This C# code demonstrates how to calculate the average of a range of cells in an Excel spreadsheet.
ExcelWorksheet worksheet = package.Workbook.Worksheets["Sheet1"];

// Define the range of cells you want to average.
var cellRange = worksheet.Cells["A1:A10"];

// Calculate the average of the numbers in the specified range.
double average = cellRange.Average();

// Output the result to the console.
Console.WriteLine($"The average of the selected cells is: {average}");
' This C# code demonstrates how to calculate the average of a range of cells in an Excel spreadsheet.
Dim worksheet As ExcelWorksheet = package.Workbook.Worksheets("Sheet1")

' Define the range of cells you want to average.
Dim cellRange = worksheet.Cells("A1:A10")

' Calculate the average of the numbers in the specified range.
Dim average As Double = cellRange.Average()

' Output the result to the console.
Console.WriteLine($"The average of the selected cells is: {average}")
$vbLabelText   $csharpLabel

Minimum

The method Min() returns the minimum number among the selected range of cells.

// This C# code demonstrates how to find the minimum value in a range of cells in an Excel spreadsheet.
ExcelWorksheet worksheet = package.Workbook.Worksheets["Sheet1"];

// Define the range of cells you want to find the minimum value in.
var cellRange = worksheet.Cells["A1:A10"];

// Find and store the minimum value of the specified range.
double minValue = cellRange.Min();

// Output the result to the console.
Console.WriteLine($"The minimum value among the selected cells is: {minValue}");
// This C# code demonstrates how to find the minimum value in a range of cells in an Excel spreadsheet.
ExcelWorksheet worksheet = package.Workbook.Worksheets["Sheet1"];

// Define the range of cells you want to find the minimum value in.
var cellRange = worksheet.Cells["A1:A10"];

// Find and store the minimum value of the specified range.
double minValue = cellRange.Min();

// Output the result to the console.
Console.WriteLine($"The minimum value among the selected cells is: {minValue}");
' This C# code demonstrates how to find the minimum value in a range of cells in an Excel spreadsheet.
Dim worksheet As ExcelWorksheet = package.Workbook.Worksheets("Sheet1")

' Define the range of cells you want to find the minimum value in.
Dim cellRange = worksheet.Cells("A1:A10")

' Find and store the minimum value of the specified range.
Dim minValue As Double = cellRange.Min()

' Output the result to the console.
Console.WriteLine($"The minimum value among the selected cells is: {minValue}")
$vbLabelText   $csharpLabel

Maximum

The method Max() returns the maximum number among the selected range of cells.

// This C# code demonstrates how to find the maximum value in a range of cells in an Excel spreadsheet.
ExcelWorksheet worksheet = package.Workbook.Worksheets["Sheet1"];

// Define the range of cells you want to find the maximum value in.
var cellRange = worksheet.Cells["A1:A10"];

// Find and store the maximum value of the specified range.
double maxValue = cellRange.Max();

// Output the result to the console.
Console.WriteLine($"The maximum value among the selected cells is: {maxValue}");
// This C# code demonstrates how to find the maximum value in a range of cells in an Excel spreadsheet.
ExcelWorksheet worksheet = package.Workbook.Worksheets["Sheet1"];

// Define the range of cells you want to find the maximum value in.
var cellRange = worksheet.Cells["A1:A10"];

// Find and store the maximum value of the specified range.
double maxValue = cellRange.Max();

// Output the result to the console.
Console.WriteLine($"The maximum value among the selected cells is: {maxValue}");
' This C# code demonstrates how to find the maximum value in a range of cells in an Excel spreadsheet.
Dim worksheet As ExcelWorksheet = package.Workbook.Worksheets("Sheet1")

' Define the range of cells you want to find the maximum value in.
Dim cellRange = worksheet.Cells("A1:A10")

' Find and store the maximum value of the specified range.
Dim maxValue As Double = cellRange.Max()

' Output the result to the console.
Console.WriteLine($"The maximum value among the selected cells is: {maxValue}")
$vbLabelText   $csharpLabel

For more flexibility, the above functions can also be applied to single/multiple rows or columns. Learn more about selecting rows and columns.