---
title: "Recalculate Excel formulas in C#"
url: https://recursiveai.org/onedocument/cookbook/recalculate-excel-formulas/
description: "Recalculate Excel formulas in C# with OneDocument for .NET 10. Runnable code, sample input and a downloadable example with real output."
version: "1.0.0"
---

# Recalculate Excel formulas in C#

**OneDocument** is a free, open-source Word, Excel and PowerPoint document library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneDocument.Spreadsheets) · [GitHub](https://github.com/recursive-ai-oss/onedocument-csharp) · [Docs](https://recursiveai.org/onedocument/docs/) · [Download example (.zip)](https://recursiveai.org/onedocument/cookbook/recalculate-excel-formulas/recalculate-excel-formulas.zip)

```shell
dotnet add package RecursiveAI.OneDocument.Spreadsheets
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneDocument.Spreadsheets;
using var book = Workbook.Open("input.xlsx");
book.Sheets[0]["A1"].SetNumber(4);
book.Recalculate();
book.SaveAs("recalculated.xlsx");
```

Writes recalculated.xlsx. The zip’s input.xlsx has A1 = 2 and B1 = `A1*3+2`; the saved B1 becomes 14. The snippet prints nothing.

Unzip [the example](https://recursiveai.org/onedocument/cookbook/recalculate-excel-formulas/recalculate-excel-formulas.zip), then `dotnet run`.

## The full program

The program creates that input workbook, changes A1, calculates, and reopens the saved file to read the cached result—the value stored alongside a formula.

```csharp
using RecursiveAI.OneDocument.Spreadsheets;

using (Workbook seed = Workbook.Create())
{
    var sheet = seed.Sheets[0];
    sheet["A1"].SetNumber(2);
    sheet["B1"].Formula = "A1*3+2";
    seed.Recalculate();
    seed.SaveAs("input.xlsx");
}
using Workbook book = Workbook.Open("input.xlsx");
var cells = book.Sheets[0];
Console.WriteLine($"Stored B1: {cells["B1"].Number}");
cells["A1"].SetNumber(4);
book.Recalculate();
book.SaveAs("recalculated.xlsx");
using Workbook saved = Workbook.Open("recalculated.xlsx");
Console.WriteLine($"Saved B1: {saved.Sheets[0]["B1"].Number}");
```

`dotnet run` prints:

```text
Stored B1: 8
Saved B1: 14
```

A formula calling a missing function evaluates to `#NAME?`; recalculation is not a guarantee that every formula is supported.

## Variations

**Force a full calculation**, regardless of which cells are dirty (need recalculation after an input changes):

```csharp
using RecursiveAI.OneDocument.Spreadsheets;
using var book = Workbook.Open("input.xlsx");
book.Sheets[0]["A1"].SetNumber(4);
book.RecalculateAll();
Console.WriteLine(book.Sheets[0]["B1"].Number);
```

```text
14
```

## Related recipes

- [Use Excel Goal Seek](https://recursiveai.org/onedocument/cookbook/excel-goal-seek/)
- [Create an Excel file](https://recursiveai.org/onedocument/cookbook/create-excel-file/)
- [Read an Excel file](https://recursiveai.org/onedocument/cookbook/read-excel-file/)

---

## The short answer

To recalculate Excel formulas in C#, open the workbook with OneDocument’s Spreadsheets package, change inputs through `Cell`, call `Workbook.Recalculate`, then save. Assigning a formula and calculating its result are separate operations. Saving after calculation stores the updated cached results, so another reader can see them without evaluating formulas itself.

The downloadable workbook is deliberately small: A1 starts at 2, B1 contains `A1*3+2`, and its stored result is 8. Changing A1 to 4 and recalculating gives 14. The full program reads the saved result back, rather than relying on what is still in memory.

## How can I automatically recalculate formulas in Excel?

In OneDocument, make the calculation call part of the edit-and-save operation. Edits through cells are tracked, but they do not trigger immediate evaluation. Opening a workbook never calculates it either. A manual-mode workbook remains in manual mode; an explicit `Recalculate` is a “calculate now” command, not a change to that setting.

This distinction matters in import pipelines. Reading a formula cell immediately after opening returns its stored value, which can be old. Reading it immediately after changing an input can still return the earlier result. Call the calculation method before reading dependent results or exporting the edited workbook. The [workbook calculation docs](https://recursiveai.org/onedocument/docs/spreadsheets-calc/) describe when edits are noticed.

## How to force full recalculation in Excel?

Use `RecalculateAll`, as in the variation. The first ordinary `Recalculate` builds the dependency graph and calculates everything. Later ordinary calls calculate formulas made dirty by edits, plus volatile formulas and those whose references are determined at runtime. A full call calculates everything again instead of relying on that dirty set.

“Dirty” means that a formula needs a fresh answer because an input changed; it does not mean the file is damaged. For repeated edits to the same in-memory workbook, ordinary recalculation is the natural choice. Use full calculation when you explicitly want all formulas evaluated again. Neither call removes the need to save afterward.

## How do I refresh all formulas in Excel?

Formula calculation and data refresh are different tasks. This API calculates from the workbook’s cell values. Pivot refresh is not implemented in this release. Other workbooks are not opened by default: external references use the caches in the file unless you supply a resolver. A formula can therefore be freshly evaluated from old external data.

Check the [function documentation](https://recursiveai.org/onedocument/docs/calc-functions/) against your workbook. Missing functions give `#NAME?`. Circular references without iterative calculation enabled get zero and a `calc.circular-reference` defect. With iteration enabled, calculation uses the workbook’s iteration settings; an unsettled cycle keeps its last values and reports `calc.iteration-limit`. Several detailed Excel-equivalence behaviors in the calculation docs have not yet been checked against Excel; do not assume those behaviors match Excel exactly. If calculation is cancelled, some cells can hold new results and others old ones. Call again to finish before saving a file you intend to distribute.
