Creating Excel in C# without Microsoft Office: Magic Under the Hood

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why "without Office" and What's Inside?

\n\n

If you've ever looked for how to generate an Excel file in C#, you've probably come across the iconic Stack Overflow question with tens of thousands of views. And the main pain point there is the phrase "without installing Microsoft Office". Why? Because dragging along COM objects, depending on the Excel version on the server, and catching errors like "Retrieving the COM class factory for component... failed" is a path to developer hell.

\n\n

But how then do you create .XLSX and .XLS files? The answer lies in how the format itself is structured. Modern Excel (since 2007) is simply a ZIP archive containing XML files inside. Yes, you heard that right: take any .xlsx, rename it to .zip, open it — and you'll see folders like xl, _rels, docProps and a bunch of XML documents. Each sheet, each cell, each style is neatly structured XML. The older .XLS format is a binary format (BIFF), but there are open-source solutions for it too.

\n\n

In this article, we'll look under the hood of this process: we'll break down popular libraries, write live code, and understand what actually happens when we call Save(). Let's go!

\n\n

1. The Three Pillars of C# for Working with Excel without Office

\n\n

Today, there are three main contenders that solve the task "out of the box." Each works directly with the format, bypassing an installed Office.

\n\n

1.1. EPPlus (LGPL / Proprietary License)

\n

The most popular library for .NET. It was originally free, but from version 5.0 it switched to a paid license for commercial use. However, for educational purposes and small projects, it can still be used (version 4.5.3.3 is the last free one). EPPlus only works with .XLSX.

\n\n

How it works under the hood: EPPlus uses System.IO.Packaging (that same ZIP archive) and generates XML files according to the Office Open XML (OOXML) specification. It can create styles, formulas, charts — everything you see in Excel.

\n\n

1.2. ClosedXML

\n

Free, open-source (MIT license). Also works only with .XLSX. It features a very friendly API that resembles working with Excel itself: you work with Workbook, Worksheet, Cell. Under the hood — the same Open XML, but ClosedXML handles all the routine of creating the XML structure.

\n\n

1.3. NPOI

\n

A port of the famous Java library Apache POI. Its main advantage is that it can work with both .XLSX (via XSSFWorkbook) and the older .XLS (via HSSFWorkbook). Free, open-source (Apache 2.0). Ideal if you need support for legacy formats.

\n\n

Under the hood for .XLS: NPOI directly writes binary data in the BIFF8 format. This is more complex than XML, but it doesn't require compression.

\n\n

2. Writing Code: Creating an Excel File Three Ways

\n\n

Let's create a simple report with headers and data. Below are examples for each library.

\n\n

2.1. Example with ClosedXML (Simplest)

\n\n
using ClosedXML.Excel;\n\n// Create a workbook\nusing (var workbook = new XLWorkbook())\n{\n    // Add a worksheet\n    var worksheet = workbook.Worksheets.Add("Report");\n\n    // Headers\n    worksheet.Cell(1, 1).Value = "Name";\n    worksheet.Cell(1, 2).Value = "Age";\n    worksheet.Cell(1, 3).Value = "City";\n\n    // Data\n    worksheet.Cell(2, 1).Value = "Anna";\n    worksheet.Cell(2, 2).Value = 28;\n    worksheet.Cell(2, 3).Value = "Moscow";\n\n    worksheet.Cell(3, 1).Value = "Ivan";\n    worksheet.Cell(3, 2).Value = 35;\n    worksheet.Cell(3, 3).Value = "Saint Petersburg";\n\n    // Style for headers\n    var headerRange = worksheet.Range("A1:C1");\n    headerRange.Style.Font.Bold = true;\n    headerRange.Style.Fill.BackgroundColor = XLColor.LightGray;\n\n    // Auto-fit columns\n    worksheet.Columns().AdjustToC                

Blogs

Book Recommendations