Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
Today's software applications require the ability to create databases and generate reports using Excel files. There are many libraries available today that allow users to do this without the need for Microsoft Excel.
In this article, we are going to discuss and compare how to work with Microsoft Excel documents programmatically in C# .NET technologies using two of the most popular libraries, IronXL and ClosedXML, for reading, manipulating, and writing Excel reports.
IronXL and ClosedXML both provide methods to create, edit, and read Excel documents in the .NET framework. The next question is to decide which C# Excel library is best suited to your project. This article will help you decide on the best option for your applications.
Let's look first at what both libraries have to offer, and then move on to the comparison itself.
IronXL is a .NET library that facilitates reading and editing Microsoft Excel documents with C#. IronXL.Excel is a standalone .NET software library for reading a wide range of spreadsheet formats. It does not require Microsoft Excel to be installed, nor does it depend on Interop.
IronXL is an intuitive C# API that allows you to read, edit, and create Excel spreadsheet files in .NET with lightning-fast performance. IronXL fully supports .NET Core, .NET Framework, Xamarin, Mobile, Linux, macOS, and Azure.
IronXL is a leading .NET Core and .NET Framework Excel spreadsheet library for C#.
More Features of IronXL can be explored using this link.
ClosedXML is a .NET library for reading, manipulating, and writing Excel 2007+ (.xlsx, .xlsm) files. It aims to provide an intuitive and user-friendly interface for dealing with the underlying OpenXML Library. The .xlsx is a file extension for an OpenXML underlying API spreadsheet file format used by Microsoft Excel. The .xlsm files support macros. The .xltm are macro-enabled template files. The .xls format is a proprietary binary format, while .xlsx is based on Office OpenXML format.
ClosedXML is a .NET library for report generation in Microsoft Excel without requiring Excel to be installed on the machine that's running the code.
ClosedXML has a user-friendly interface with extensive API functions to handle creation and extraction of content in Excel. With these API calls and pull requests, you can modify every little detail in an Excel sheet or workbook. All the features are listed below:
The rest of this article continues as follows:
Use the following steps to create a Console Application:
Create a Project
DemoApp Project
.NET Framework 6.0
Now, the project is created and we are almost ready to test the libraries. However, we still need to install and integrate them into our project. Let's install IronXL first.
You can download and install the IronXL library using the following methods:
Let's take a closer look at each one.
Visual Studio provides the NuGet Package Manager to install NuGet packages in your projects. You can access it through the Project Menu, or by right-clicking your project in the Solution Explorer.
Package Manager
IronXL NuGet Install
Another way to download and install the IronXL C# Library is to make use of the following steps to install the IronXL NuGet package through the Developer Command Prompt.
Type the following command:
Install-Package IronXL.Excel
using IronXL;
using IronXL;
Imports IronXL
All done! IronXL is downloaded, installed, and ready to use. However, before that, we should install ClosedXML.
To install the ClosedXML package, you can directly download it from NuGet or from NuGet Package Manager/Console in the project.
For directly installing ClosedXML, click on this link.
Open the NuGet Package Manager from project solution explorer, browse for ClosedXML, and click install.
ClosedXML NuGet Install
Or:
Type the following command:
Install-Package ClosedXML
using ClosedXML.Excel;
using ClosedXML.Excel;
Imports ClosedXML.Excel
A workbook is an Excel file containing multiple worksheets with rows and columns. Both libraries provide the facility to create a new Excel workbook and sheets. Let's have a look at the code step-by-step.
It could not be any easier to create a new Excel Workbook using IronXL. It takes just one line of code. Yes, really. Add the following code to your static void main function in the Program.cs file:
// Create a new Excel workbook using IronXL
WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
// Create a new Excel workbook using IronXL
WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
' Create a new Excel workbook using IronXL
Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
Both XLS (older Excel file version) and XLSX (current and newer file version) file formats can be created with IronXL.
And, it's even simpler to create a default Worksheet:
// Create a new worksheet within the workbook
var worksheet = workbook.CreateWorkSheet("IronXL Features");
// Create a new worksheet within the workbook
var worksheet = workbook.CreateWorkSheet("IronXL Features");
' Create a new worksheet within the workbook
Dim worksheet = workbook.CreateWorkSheet("IronXL Features")
You can now use the worksheet variable to set cell values and do almost everything an Excel file can do.
// Add data and styles to the new worksheet
worksheet["A1"].Value = "Hello World";
worksheet["A2"].Style.BottomBorder.SetColor("#ff6600");
worksheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double;
// Add data and styles to the new worksheet
worksheet["A1"].Value = "Hello World";
worksheet["A2"].Style.BottomBorder.SetColor("#ff6600");
worksheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double;
' Add data and styles to the new worksheet
worksheet("A1").Value = "Hello World"
worksheet("A2").Style.BottomBorder.SetColor("#ff6600")
worksheet("A2").Style.BottomBorder.Type = IronXL.Styles.BorderType.Double
Save the Excel file:
// Save the spreadsheet
workbook.SaveAs("NewExcelFile.xlsx");
// Save the spreadsheet
workbook.SaveAs("NewExcelFile.xlsx");
' Save the spreadsheet
workbook.SaveAs("NewExcelFile.xlsx")
The output file looks as follows:
NewExcelFile
output
You can also create excel files (.xlsx, .xlsm) easily using ClosedXML. The following code is a typical example which serves to generate a simple Excel file and save it. You can add a new sample sheet to your workbook and assign values to cells in your Excel application.
// Create a new empty Excel file
var workbook = new XLWorkbook();
// Create a new worksheet and set cell A1 value to 'Hello world!'
var worksheet = workbook.Worksheets.Add("ClosedXML Features");
worksheet.Cell("A1").Value = "Hello world!";
// Save to XLSX file
workbook.SaveAs("Spreadsheet.xlsx");
// Create a new empty Excel file
var workbook = new XLWorkbook();
// Create a new worksheet and set cell A1 value to 'Hello world!'
var worksheet = workbook.Worksheets.Add("ClosedXML Features");
worksheet.Cell("A1").Value = "Hello world!";
// Save to XLSX file
workbook.SaveAs("Spreadsheet.xlsx");
' Create a new empty Excel file
Dim workbook = New XLWorkbook()
' Create a new worksheet and set cell A1 value to 'Hello world!'
Dim worksheet = workbook.Worksheets.Add("ClosedXML Features")
worksheet.Cell("A1").Value = "Hello world!"
' Save to XLSX file
workbook.SaveAs("Spreadsheet.xlsx")
The output file looks as follows:
Spreadsheet File Output
Both libraries can open and read existing Excel documents. Let's have a look at the sample code.
The IronXL WorkBook
class represents an Excel sheet. To open an Excel file using C#, we use WorkBook.Load
and specify the path of the file (.xlsx). The following one-line code is used to open the file for reading:
// Load WorkBook
var workbook = WorkBook.Load(@"Spreadsheets\\sample.xlsx");
// Load WorkBook
var workbook = WorkBook.Load(@"Spreadsheets\\sample.xlsx");
' Load WorkBook
Dim workbook = WorkBook.Load("Spreadsheets\\sample.xlsx")
Each WorkBook
can have multiple worksheets in the Excel document. If the workbook contains multiple worksheets, retrieve them by name as follows:
// Open Sheet for reading
var worksheet = workbook.GetWorkSheet("sheetnamegoeshere");
// Open Sheet for reading
var worksheet = workbook.GetWorkSheet("sheetnamegoeshere");
' Open Sheet for reading
Dim worksheet = workbook.GetWorkSheet("sheetnamegoeshere")
Code for reading the cell values:
// Read from ranges of cells elegantly
foreach (var cell in worksheet["A2:A10"])
{
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
// Read from ranges of cells elegantly
foreach (var cell in worksheet["A2:A10"])
{
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
' Read from ranges of cells elegantly
For Each cell In worksheet("A2:A10")
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)
Next cell
ClosedXML allows reading of previously created files from your C# application. You can import an excel file using the following code.
// Import Excel file from file's path
var workbook = new XLWorkbook("SimpleTemplate.xlsx");
// Read worksheet from workbook using sheet number
var worksheet1 = workbook.Worksheet(1);
// Read cell value from the first sheet
var data = worksheet1.Cell("A1").GetValue<string>();
// Display on screen
Console.WriteLine(data);
// Import Excel file from file's path
var workbook = new XLWorkbook("SimpleTemplate.xlsx");
// Read worksheet from workbook using sheet number
var worksheet1 = workbook.Worksheet(1);
// Read cell value from the first sheet
var data = worksheet1.Cell("A1").GetValue<string>();
// Display on screen
Console.WriteLine(data);
' Import Excel file from file's path
Dim workbook = New XLWorkbook("SimpleTemplate.xlsx")
' Read worksheet from workbook using sheet number
Dim worksheet1 = workbook.Worksheet(1)
' Read cell value from the first sheet
Dim data = worksheet1.Cell("A1").GetValue(Of String)()
' Display on screen
Console.WriteLine(data)
Excel formulas are one of the most important features of working with Excel. Both libraries have a powerful formula calculation engine. They provide the facility to work with formulas and easily apply them to cells.
After loading the workbook and worksheet, the following code sample can be used to either make changes to formulas, or be applied to specific cells. The code is as follows:
// Set formulas
worksheet["A1"].Formula = "=Sum(B8:C12)";
worksheet["B8"].Formula = "=C9/C11";
worksheet["G30"].Formula = "=Max(C3:C7)";
// Force recalculating all formula values in all sheets
workbook.EvaluateAll();
// Set formulas
worksheet["A1"].Formula = "=Sum(B8:C12)";
worksheet["B8"].Formula = "=C9/C11";
worksheet["G30"].Formula = "=Max(C3:C7)";
// Force recalculating all formula values in all sheets
workbook.EvaluateAll();
' Set formulas
worksheet("A1").Formula = "=Sum(B8:C12)"
worksheet("B8").Formula = "=C9/C11"
worksheet("G30").Formula = "=Max(C3:C7)"
' Force recalculating all formula values in all sheets
workbook.EvaluateAll()
You can also retrieve formulas and their values:
// Get the formula's calculated value e.g. "52"
string formulaValue = worksheet["G30"].Value;
// Get the formula as a string e.g. "Max(C3:C7)"
string formulaString = worksheet["G30"].Formula;
// Save your changes with updated formulas and calculated values
workbook.Save();
// Get the formula's calculated value e.g. "52"
string formulaValue = worksheet["G30"].Value;
// Get the formula as a string e.g. "Max(C3:C7)"
string formulaString = worksheet["G30"].Formula;
// Save your changes with updated formulas and calculated values
workbook.Save();
' Get the formula's calculated value e.g. "52"
Dim formulaValue As String = worksheet("G30").Value
' Get the formula as a string e.g. "Max(C3:C7)"
Dim formulaString As String = worksheet("G30").Formula
' Save your changes with updated formulas and calculated values
workbook.Save()
ClosedXML does not support all formulas and you'll probably get a nasty error if the formula isn't supported or if there's an error in the formula. Please test your formulas before going to production. Let's have a look at how to use them.
// Set formulas
worksheet.Cell("A1").Value = "Hello World!";
worksheet.Cell("A2").FormulaA1 = "=MID(A1, 7, 5)";
// You can use Evaluate function to directly calculate formula
var sum = worksheet.Evaluate("SUM(B1:B7)");
// Force recalculation
worksheet.RecalculateAllFormulas();
// Save Excel file
workbook.SaveAs("Formula Calculation.xlsx");
// Set formulas
worksheet.Cell("A1").Value = "Hello World!";
worksheet.Cell("A2").FormulaA1 = "=MID(A1, 7, 5)";
// You can use Evaluate function to directly calculate formula
var sum = worksheet.Evaluate("SUM(B1:B7)");
// Force recalculation
worksheet.RecalculateAllFormulas();
// Save Excel file
workbook.SaveAs("Formula Calculation.xlsx");
' Set formulas
worksheet.Cell("A1").Value = "Hello World!"
worksheet.Cell("A2").FormulaA1 = "=MID(A1, 7, 5)"
' You can use Evaluate function to directly calculate formula
Dim sum = worksheet.Evaluate("SUM(B1:B7)")
' Force recalculation
worksheet.RecalculateAllFormulas()
' Save Excel file
workbook.SaveAs("Formula Calculation.xlsx")
The property FormulaA1
is with reference to A1 cell. If you are referencing another cell, you must add it to the Formula
property. E.g., FormulaC2
.
IronXL is an openly commercial C# Excel library. It is free for development and can always be licensed for commercial deployment. Licenses are available for single-project use, single developers, agencies, and global corporations as well as SaaS and OEM redistribution. All licenses include a 30-day money-back guarantee, one year of product support and updates, validity for dev/staging/production, and also a permanent license (one-time purchase). The lite package starts from $749.
IronXL License Packages
For ClosedXML, applications using this DLL file do not require a separate license either for single-use or commercial use. In order for any solution to work with ClosedXML, just install the publicly available NuGet package "ClosedXML". ClosedXML is licensed under the MIT License. MIT License is a short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code.
ClosedXML License
IronXL is a complete library offering everything you need to manipulate an Excel file. IronXL allows developers to read, generate, and edit Excel (and other Spreadsheet files) in .NET applications & websites. It provides a fast and natural approach to work with Excel and other Spreadsheet files in C#.
ClosedXML is a .NET library for reading and writing Excel files, which makes it easier for developers to create Excel 2007+ files. It provides a nice object-oriented way to manipulate the files (similar to VBA) without dealing with the hassles of XML Documents. It can be used by any .NET language like C# and Visual Basic (VB). It provides features of OpenXML but still some are not implemented. E.g., Macros, Embedding, and Charts.
IronXL is also helpful in other Excel operations like cell data formats, sorting, cell styling. It can also work with Excel Spreadsheets as System.Data.DataSet and System.Data.DataTable. It supports console, web server, and desktop-based applications. It is also supported on all OS platforms.
ClosedXML is a lightweight library for reading, manipulating, and writing Excel applications using OpenXML API which is fast and easier to implement. It helps in all major Excel operations like Page Setup, Freeze Panes, Hyperlinks, Tables, and Conditional Formatting. It has no overhead or low performance issues.
In comparison, IronXL and ClosedXML both are fast and accurate when it comes to Excel spreadsheets. They do not need Microsoft Office installation or license when it comes to creating Excel reports. However, IronXL has an advantage over ClosedXML as ClosedXML is not thread-safe. IronXL is easy to use and provides the facility to convert other formats to XLSX, and also from XLSX to other formats such as CSV and HTML. ClosedXML doesn't allow this conversion. This interconversion allows users the flexibility to work with other file formats with ease.
Now you can get five Iron products for the price of just two in the lite package for 1 developer($1498) and unlimited for $5,998 and save up to 60%.
IronXL and ClosedXML are .NET libraries for creating, editing, and reading Excel documents. IronXL offers a standalone solution compatible with various platforms, while ClosedXML uses OpenXML to handle Excel 2007+ (.xlsx, .xlsm) files.
No, both IronXL and ClosedXML do not require Microsoft Excel to be installed to work with Excel files.
IronXL can load, read, and edit XLS/XLSX/CSV/TSV files and save and export to the same formats, including JSON.
ClosedXML offers features such as formulas, validation, hyperlinks, protection, conditional formatting, freeze panes, tables, ranges, styling, page setup, auto-filters, and comments.
IronXL can be installed using Visual Studio with NuGet packages or by manually installing the DLL via the Developer Command Prompt with the command 'Install-Package IronXL.Excel'.
You can install ClosedXML by downloading it from NuGet or by using the NuGet Package Manager/Console with the command 'Install-Package ClosedXML'.
IronXL requires a commercial license for deployment, with options for single-project use, single developers, agencies, and corporations. It offers a 30-day money-back guarantee and includes one year of product support and updates.
Yes, ClosedXML is licensed under the MIT License, which is a permissive open-source license allowing free use, modification, and distribution.
Yes, IronXL can convert Excel files to other formats such as CSV and HTML, providing flexibility in handling different file formats.
One key advantage of IronXL is its thread-safety and ability to convert between various file formats, which ClosedXML does not support.