BitBindle.ClosedXML.Parser 3.0.1

dotnet add package BitBindle.ClosedXML.Parser --version 3.0.1
                    
NuGet\Install-Package BitBindle.ClosedXML.Parser -Version 3.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BitBindle.ClosedXML.Parser" Version="3.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BitBindle.ClosedXML.Parser" Version="3.0.1" />
                    
Directory.Packages.props
<PackageReference Include="BitBindle.ClosedXML.Parser" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BitBindle.ClosedXML.Parser --version 3.0.1
                    
#r "nuget: BitBindle.ClosedXML.Parser, 3.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BitBindle.ClosedXML.Parser@3.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BitBindle.ClosedXML.Parser&version=3.0.1
                    
Install as a Cake Addin
#tool nuget:?package=BitBindle.ClosedXML.Parser&version=3.0.1
                    
Install as a Cake Tool

ClosedXML.Parser

ClosedXML.Parser is a .NET library that parses Excel formulas into an abstract syntax tree (AST). ClosedXML uses it to evaluate formulas and to change references in formulas.

This repository is a fork of ClosedXML.Parser by Jan Havlíček. The fork has its own releases, issues and changes. The license is MIT (see LICENSE.txt).

Package

The BitBindle.ClosedXML.Parser package is on nuget.org. The package targets net10.0.

dotnet add package BitBindle.ClosedXML.Parser

The package ID differs from the ID of the upstream package (ClosedXML.Parser). The assembly name and the namespaces stay ClosedXML.Parser. A project cannot reference both packages, because they contain the same assembly name. The upstream ClosedXML package depends on the upstream ClosedXML.Parser package. The fork does not support a project that uses both ClosedXML and this package.

How to use

  1. Implement the IAstFactory<TScalarValue, TNode, TContext> interface. The parser calls the factory for each part of the formula, and the factory creates your nodes.
  2. Call a parse method:
    • FormulaParser<TScalarValue, TNode, TContext>.CellFormulaA1("SUM(A1, 2)", context, factory)
    • FormulaParser<TScalarValue, TNode, TContext>.CellFormulaR1C1("SUM(R1C1, 2)", context, factory)

The library also has these helpers:

  • FormulaConverter changes a formula from A1 to R1C1 and from R1C1 to A1. Its ModifyA1 method changes a formula with a factory that you give, for example a subclass of RefModVisitor.
  • ReferenceParser parses a reference, for example Sheet1!A1:B2.
  • NameUtils finds when a sheet name needs quotes in a formula.

The ClosedXML.Parser.Ast project has a simple AST and a factory. The tests use it. It shows how to implement IAstFactory.

Goals

  • Performance: ClosedXML must parse formulas fast and with few allocations.
  • Evaluation: The parser creates an abstract syntax tree, not a concrete syntax tree. The goal is the evaluation of formulas.
  • Multi-use: Formulas are mostly in cells, but other parts of a workbook have formulas with different grammar rules, for example sparklines and data validation.
  • A1 and R1C1 notation: The parser reads formulas in A1 and R1C1 notation. For example, SUM(R5) is the sum of cell R5 in A1 notation, but the sum of row 5 in R1C1 notation.

Design

The official source of the grammar is MS-XLSX, chapter 2.2.2 Formulas. A copy is in MS-XLSX-parser-grammar.abnf. A parser generator can not use this grammar directly: it has ambiguities, and its rules do not show the operator precedence.

The ANTLR4 grammar in src/ClosedXML.ANTLR is the source of truth for the lexer and the parser:

  • Lexer: Rolex generates a DFA lexer from the ANTLR lexer grammar (see Generate the lexer). It is approximately two times faster than the ANTLR lexer.
  • Parser: FormulaParser is a recursive descent parser. It follows the rules of the ANTLR parser grammar, but it does not use ANTLR at runtime.

Performance

The ENRON data set has 946320 formulas. The recursive descent parser with the DFA lexer parses them in approximately 1.8 seconds in Release mode. That is approximately 2 μs for each formula.

Limitations

The primary goal is to parse the formulas in a file, not the formulas that a user types. The formula in the file is not always the same as the formula that Excel shows. For example:

  • The IFS function is a future function. The file has _xlfn.IFS, but the user sees IFS.
  • In a structured reference, the user sees @ for the current row. The file has the [#This Row] specifier.

Thus:

  • An external reference must use the index of an external file, for example [5].
  • Other formula grammars, for example [1]!'Some name in external wb', are out of scope.

The maximum of nesting levels depends on the stack of the thread. If the stack does not have sufficient space for the next level, the parser throws ParsingException. The runtime keeps the last 128 KB of the stack free, so a thread with a stack of 1 MB or more parses much more than the 64 nesting levels of Excel.

Development

AGENTS.md gives the rules for each change, and the commands to build, test and check the code. The short form:

dotnet build ClosedXML.Parser.slnx --configuration Release
dotnet test src/ClosedXML.Parser.Tests/ClosedXML.Parser.Tests.csproj --configuration Release --no-build

The base branch is develop. The CI workflow builds and tests each pull request to develop. A person starts the release workflow on develop.

Testing strategy

  • Some tokens have data for a node, for example A1_CELL C5 gives row 5 and column 3. The Lexers folder has test classes for most of these tokens, for example A1CellTokenTests.cs.
  • The Rules folder has test classes for the parser rules, for example CellReferenceRuleTests.cs. Each test class has combinations of the rule, and it compares the result with the AST nodes.
  • DataSetTests.cs has the data set tests. Each test parses each formula in data/<set>/formulas.csv. The parse must fail for the formulas in the known-fails.csv and invalid-external-cell-reference.csv files of the set, if the set has them. The parse must not fail for the other formulas. The tests do not check the AST. The CSV files have one column.
  • AntlrCompatibilityTests.cs makes sure that the Rolex lexer gives the same tokens as the ANTLR lexer.

Use the vscode-antlr4 extension to debug the ANTLR grammar.

Generate the lexer

Rolex is a DFA lexer generator with the MIT license. Antlr2Rolex makes a Rolex grammar from the ANTLR grammar, and Rolex makes the DFA.

Prepare the Rolex grammars:

  1. Run Antlr2Rolex on src/ClosedXML.ANTLR/FormulaLexer.g4 with the A1 version. Write the result to src/ClosedXML.Parser/Rolex/LexerA1.rl.
  2. Add /* at the start of the Local A1 References section. This removes A1_CELL, A1_SPAN_REFERENCE and their fragments.
  3. Remove /* at the start of the Local R1C1 References section. This section has different tokens for A1_CELL, A1_SPAN_REFERENCE and their fragments.
  4. Run Antlr2Rolex on src/ClosedXML.ANTLR/FormulaLexer.g4 with the R1C1 version. Write the result to src/ClosedXML.Parser/Rolex/LexerR1C1.rl.

Fix the Rolex generator: Rolex does not read the \u1234 escape (a Unicode escape with four hex digits) correctly. Add pc.Advance() to _ParseEscapePart and _ParseRangeEscapePart in FFA.cs.

Generate the DFA with Rolex:

Rolex.exe src\ClosedXML.Parser\Rolex\LexerA1.rl /noshared /output src\ClosedXML.Parser\Rolex\RolexA1Dfa.cs /namespace ClosedXML.Parser.Rolex
Rolex.exe src\ClosedXML.Parser\Rolex\LexerR1C1.rl /noshared /output src\ClosedXML.Parser\Rolex\RolexR1C1Dfa.cs /namespace ClosedXML.Parser.Rolex

Resources

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on BitBindle.ClosedXML.Parser:

Package Downloads
BitBindle.ClosedXML

See release notes https://github.com/BitBindle/ClosedXML/releases/tag/0.107.7 ClosedXML is a .NET library that reads, changes and writes Excel 2007+ files (.xlsx, .xlsm). It gives an easy interface to the Open XML SDK.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.1 250 11/25/2026
3.0.0 96 11/24/2026