ClosedXML
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 to dealing with the underlying OpenXML API.
Top Related Projects
Lightweight and fast library written in C# for reading Microsoft Excel files
Create advanced Excel spreadsheets using .NET
Quick Overview
ClosedXML is an open-source .NET library for reading, manipulating, and writing Excel 2007+ (.xlsx, .xlsm) files. It provides a user-friendly and intuitive API for working with Excel spreadsheets without requiring Microsoft Office to be installed on the machine.
Pros
- Easy to use API with a fluent interface for creating and modifying Excel files
- No dependency on Microsoft Office or COM Interop
- Supports a wide range of Excel features, including formulas, cell styles, and charts
- Active development and community support
Cons
- Performance can be slower compared to other Excel libraries for very large datasets
- Limited support for older Excel file formats (.xls)
- Some advanced Excel features may not be fully supported
- Learning curve for complex operations and advanced features
Code Examples
- Creating a new workbook and adding data:
using ClosedXML.Excel;
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add("Sample Sheet");
worksheet.Cell("A1").Value = "Hello World!";
worksheet.Cell("A2").FormulaA1 = "=UPPER(A1)";
workbook.SaveAs("HelloWorld.xlsx");
}
- Reading data from an existing Excel file:
using ClosedXML.Excel;
using (var workbook = new XLWorkbook("existing_file.xlsx"))
{
var worksheet = workbook.Worksheet(1);
var cellValue = worksheet.Cell("A1").Value;
var rangeValues = worksheet.Range("A1:C10").Values;
}
- Applying styles to cells:
using ClosedXML.Excel;
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add("Styled Sheet");
var cell = worksheet.Cell("A1");
cell.Value = "Styled Cell";
cell.Style.Font.Bold = true;
cell.Style.Fill.BackgroundColor = XLColor.LightBlue;
cell.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
workbook.SaveAs("StyledSheet.xlsx");
}
Getting Started
To get started with ClosedXML, follow these steps:
-
Install the NuGet package in your .NET project:
Install-Package ClosedXML
-
Add the following using statement to your C# file:
using ClosedXML.Excel;
-
Create a new workbook or open an existing one:
var workbook = new XLWorkbook(); // New workbook // OR var workbook = new XLWorkbook("existing_file.xlsx"); // Open existing file
-
Work with worksheets and cells:
var worksheet = workbook.Worksheets.Add("My Sheet"); worksheet.Cell("A1").Value = "Hello, ClosedXML!";
-
Save the workbook:
workbook.SaveAs("MyWorkbook.xlsx");
Competitor Comparisons
Lightweight and fast library written in C# for reading Microsoft Excel files
Pros of ExcelDataReader
- Lightweight and focused on reading Excel files
- Supports both .xls and .xlsx formats
- Faster performance for large file reading
Cons of ExcelDataReader
- Limited to reading Excel files, no writing capabilities
- Less feature-rich compared to ClosedXML
- May require additional libraries for advanced operations
Code Comparison
ExcelDataReader:
using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
var result = reader.AsDataSet();
// Process data
}
ClosedXML:
using (var workbook = new XLWorkbook(filePath))
{
var worksheet = workbook.Worksheet(1);
var range = worksheet.RangeUsed();
// Process data
}
ExcelDataReader is more focused on reading Excel files efficiently, while ClosedXML offers a broader range of features for both reading and writing Excel files. ExcelDataReader may be preferred for simple reading tasks or when dealing with large files, while ClosedXML is better suited for more complex Excel operations and file creation.
Create advanced Excel spreadsheets using .NET
Pros of EPPlus
- Better performance for large datasets
- More comprehensive charting capabilities
- Supports password protection and encryption
Cons of EPPlus
- Licensing changes: now requires a commercial license for some use cases
- Less active community and fewer contributors
- Steeper learning curve for beginners
Code Comparison
EPPlus:
using (var package = new ExcelPackage())
{
var worksheet = package.Workbook.Worksheets.Add("Sheet1");
worksheet.Cells["A1"].Value = "Hello, EPPlus!";
package.SaveAs(new FileInfo("output.xlsx"));
}
ClosedXML:
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add("Sheet1");
worksheet.Cell("A1").Value = "Hello, ClosedXML!";
workbook.SaveAs("output.xlsx");
}
Both libraries offer similar functionality for basic Excel operations. EPPlus tends to have more advanced features, while ClosedXML focuses on simplicity and ease of use. The choice between them often depends on specific project requirements, licensing considerations, and personal preference.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
ð¾ Download unstable CI build
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 to dealing with the underlying OpenXML API.
For more information see the documentation or the wiki.
Release notes & migration guide
The public API is still not stable and it is a very good idea to read release notes and migration guide before each update.
Performance
Performance matters mostly for large files. For small files, few ms here or there doesn't matter. The presented data are from generally develop branch (currently 0.103-beta).
Runtime details
``` BenchmarkDotNet v0.13.8, Windows 11 (10.0.22621.2283/22H2/2022Update/SunValley2) AMD Ryzen 5 5500U with Radeon Graphics, 1 CPU, 12 logical and 6 physical cores .NET SDK 8.0.100-rc.1.23463.5 [Host] : .NET 7.0.11 (7.0.1123.42427), X64 RyuJIT AVX2 ```Save
Description | Rows | Columns | Time/Memory to insert data | Save workbook | Total time/memory |
---|---|---|---|---|---|
Mixed (text/number) workbook. Gist | 250 000 | 15 | 1.619 sec / 117 MiB | 6.343 sec | 7.962 sec / 477 MiB |
Text only workbook. Gist | 1 000 000 | 10 | 6.302 sec / 402 MiB | 17.134 sec | 23.436 sec / 1880 MiB |
Load
Description | Rows | Columns | Time to load data | Used memory |
---|---|---|---|---|
Load mixed workbook (10 text/5 number columns). | 250 000 | 15 | 15.648 sec | 236 MiB |
Text only workbook. | 1 000 000 | 10 | 49.046 sec | 801 MiB |
Load tests used files created during save test.
Frequent answers
- If you get an exception
Unable to find font font name or fallback font fallback font name. Install missing fonts or specify a different fallback font through âLoadOptions.DefaultGraphicEngine = new DefaultGraphicEngine(âFallback font nameâ)â
, see help page about missing fonts. - ClosedXML is not thread-safe. There is no guarantee that parallel operations will work. The underlying OpenXML library is also not thread-safe.
- If you get an exception
The type initializer for 'Gdip' threw an exception.
on Linux, you have to upgrade to 0.97+.
Install ClosedXML via NuGet
If you want to include ClosedXML in your project, you can install it directly from NuGet
To install ClosedXML, run the following command in the Package Manager Console
PM> Install-Package ClosedXML
What can you do with this?
ClosedXML allows you to create Excel files without the Excel application. The typical example is creating Excel reports on a web server.
Example:
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add("Sample Sheet");
worksheet.Cell("A1").Value = "Hello World!";
worksheet.Cell("A2").FormulaA1 = "=MID(A1, 7, 5)";
workbook.SaveAs("HelloWorld.xlsx");
}
Extensions
Be sure to check out our ClosedXML
extension projects
- https://github.com/ClosedXML/ClosedXML.Report
- https://github.com/ClosedXML/ClosedXML.Extensions.AspNet
- https://github.com/ClosedXML/ClosedXML.Extensions.Mvc
- https://github.com/ClosedXML/ClosedXML.Extensions.WebApi
Developer guidelines
The OpenXML specification is a large and complicated beast. In order for ClosedXML, the wrapper around OpenXML, to support all the features, we rely on community contributions. Before opening an issue to request a new feature, we'd like to urge you to try to implement it yourself and log a pull request.
Please read the full developer guidelines.
Credits
- Project originally created by Manuel de Leon
- Current maintainer: Jan HavlÃÄek
- Former maintainer and lead developer: Francois Botha
- Master of Computing Patterns: Aleksei Pankratev
- Logo design by @Tobaloidee
Thanks to JetBrains for providing development tools through their Open Source Program
Top Related Projects
Lightweight and fast library written in C# for reading Microsoft Excel files
Create advanced Excel spreadsheets using .NET
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot