Get started with IronXL

Start using IronXL in your project today with a free trial.

First Step:
green arrow pointer


Aggregate Functions Example

When working with ranges of cells in an Excel spreadsheet, you can utilize various aggregate functions to perform calculations. Here are some essential methods:

  • The Sum() method calculates the total sum of the selected range of cells.
  • The Avg() method determines the average value of the selected range of cells.
  • The Min() method identifies the minimum number within the selected range of cells.
  • The Max() method finds the maximum number within the selected range of cells.

These functions are valuable tools for analyzing data and deriving meaningful insights from your Excel spreadsheets.

Please note
Non-numerical values will not be included in the calculation.

:path=/static-assets/excel/content-code-examples/how-to/math-functions-math-functions.cs
using IronXL;
using System.Linq;

// Load an Excel workbook
// The Load method opens an Excel file with the specified path.
WorkBook workBook = WorkBook.Load("sample.xls");

// Access the first worksheet in the workbook
// The First method retrieves the first WorkSheet from the WorkSheets collection.
WorkSheet workSheet = workBook.WorkSheets.First();

// Get a range of cells from the worksheet (cells A1 to A8)
// This specifies a range in the Excel sheet using the indexer method.
var range = workSheet["A1:A8"];

// Calculate the sum of all numeric cells within the specified range
// The Sum extension method iterates over all cells in the range and calculates the sum of numeric values.
double sum = range.Sum(cell => cell.DoubleValue);

// Calculate the average value of all numeric cells within the specified range
// The Average extension method calculates the mean of all numeric values.
double avg = range.Average(cell => cell.DoubleValue);

// Identify the maximum value among all numeric cells within the specified range
// The Max extension method returns the largest value among the numeric cells.
double max = range.Max(cell => cell.DoubleValue);

// Identify the minimum value among all numeric cells within the specified range
// The Min extension method returns the smallest value among the numeric cells.
double min = range.Min(cell => cell.DoubleValue);
Imports IronXL

Imports System.Linq



' Load an Excel workbook

' The Load method opens an Excel file with the specified path.

Private workBook As WorkBook = WorkBook.Load("sample.xls")



' Access the first worksheet in the workbook

' The First method retrieves the first WorkSheet from the WorkSheets collection.

Private workSheet As WorkSheet = workBook.WorkSheets.First()



' Get a range of cells from the worksheet (cells A1 to A8)

' This specifies a range in the Excel sheet using the indexer method.

Private range = workSheet("A1:A8")



' Calculate the sum of all numeric cells within the specified range

' The Sum extension method iterates over all cells in the range and calculates the sum of numeric values.

Private sum As Double = range.Sum(Function(cell) cell.DoubleValue)



' Calculate the average value of all numeric cells within the specified range

' The Average extension method calculates the mean of all numeric values.

Private avg As Double = range.Average(Function(cell) cell.DoubleValue)



' Identify the maximum value among all numeric cells within the specified range

' The Max extension method returns the largest value among the numeric cells.

Private max As Double = range.Max(Function(cell) cell.DoubleValue)



' Identify the minimum value among all numeric cells within the specified range

' The Min extension method returns the smallest value among the numeric cells.

Private min As Double = range.Min(Function(cell) cell.DoubleValue)
$vbLabelText   $csharpLabel

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

Frequently Asked Questions

What is IronXL?

IronXL is a powerful tool in Excel that facilitates math aggregation functions such as Average, Sum, Min, and Max. It allows users to perform calculations and analyze data effectively without using Interop.

How do I start using math functions with IronXL?

To start using math functions with IronXL, first download the C# library from NuGet, load an existing Excel file, select the desired range, and invoke the available math functions like Sum, Avg, Min, or Max.

What aggregate functions can be used with IronXL?

IronXL supports several aggregate functions including Sum, Avg, Min, and Max for analyzing ranges of cells in an Excel spreadsheet.

Can I use IronXL to calculate the sum of a range of cells?

Yes, IronXL allows you to calculate the total sum of a selected range of cells using the Sum() method.

How can I calculate the average value of a range using IronXL?

You can calculate the average value of a selected range of cells in IronXL using the Avg() method.

Is it possible to find the minimum value in a range with IronXL?

Yes, you can use the Min() method in IronXL to identify the minimum number within a selected range of cells.

How do I determine the maximum value in a range using IronXL?

The Max() method in IronXL allows you to find the maximum number within a selected range of cells.

Are non-numerical values included in IronXL calculations?

Non-numerical values are not included in the calculations performed by IronXL's aggregate functions.

What programming language is used with IronXL for math functions?

IronXL uses C# programming language to perform math functions and other operations on Excel files.

Chaknith related to Aggregate Functions Example
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.