Introduction to ClosedXML: What It Is and Why You Need It
ClosedXML is a powerful open-source library for C# (.NET) designed to create, read, and edit Microsoft Excel files (formats .xlsx, .xlsm, .xltx). It provides a clean, object-oriented interface for working with spreadsheets without requiring Microsoft Office installation or COM objects.
This library is especially useful for:
- Report automation — generating complex Excel reports from databases, APIs, or other sources
- Data import/export — bulk processing of tabular data between systems
- Template filling — populating pre-designed Excel templates with data
- Financial and analytical calculations — working with formulas, pivot tables, and charts
- VBA migration — moving macros to modern C#
ClosedXML supports all major Excel features: cell formatting, merging, conditional formatting, charts, pivot tables, data validation, sheet protection, and more. The library works entirely in memory and does not require Excel to be installed on the server.
Installing ClosedXML
Install ClosedXML via NuGet Package Manager. Below are all installation methods:
Via .NET CLI
dotnet add package ClosedXML
Via Package Manager Console (Visual Studio)
Install-Package ClosedXML
Via NuGet Package Manager UI
- Open your project in Visual Studio
- Right-click the project → "Manage NuGet Packages"
- Search for "ClosedXML" and click "Install"
Verify Installation
After installation, add the following using directive to your code:
using ClosedXML.Excel;
Quick Start: Create Your First Excel Table
Let's create a simple employee data table and save it to a file:
using ClosedXML.Excel;
class Program
{
static void Main(string[] args)
{
// Create a new workbook
using (var workbook = new XLWorkbook())
{
// Add a worksheet
var worksheet = workbook.Worksheets.Add("Employees");
// Fill in headers
worksheet.Cell("A1").Value = "ID";
worksheet.Cell("B1").Value = "Name";
worksheet.Cell("C1").Value = "Position";
worksheet.Cell("D1").Value = "Salary";
// Fill in data
worksheet.Cell("A2").Value = 1;
worksheet.Cell("B2").Value = "John Smith";
worksheet.Cell("C2").Value = "Developer";
worksheet.Cell("D2").Value = 150000;
worksheet.Cell("A3").Value = 2;
worksheet.Cell("B3").Value = "Jane Doe";
worksheet.Cell("C3").Value = "Analyst";
worksheet.Cell("D3").Value = 120000;
// Format headers
var headerRange = worksheet.Range("A1:D1");
headerRange.Style.Font.Bold = true;
headerRange.Style.Fill.BackgroundColor = XLColor.LightBlue;
// Auto-fit column widths
worksheet.Columns().AdjustToContents();
// Save the file
workbook.SaveAs("employees.xlsx");
}
Console.WriteLine("File created successfully!");
}
}
This example creates an employees.xlsx file with three rows of data and formatted headers.
Key ClosedXML Methods and Functions
1. Creating Workbooks and Worksheets
Method: new XLWorkbook() and workbook.Worksheets.Add()
Description: Creates a new workbook and adds worksheets to it.
Parameters:
Also in library
Monitoring Go Applications: Libraries and Tools
Kotlin WebSocket Libraries for Creating Real-Time in Web Applications
Authorization and Authentication in TypeScript: A Complete Guide