How can I create Excel files (.XLS and .XLSX) in C# without requiring Microsoft Office to be installed on the target machine?
Creating Excel files (.XLS and .XLSX) in C# without Microsoft Office installed is possible through several dedicated libraries that handle the Excel file formats directly without requiring Office interop. These libraries work with the underlying file format specifications rather than automation, making them ideal for server environments, deployment, and applications where Office cannot be installed.
Contents
- Popular Libraries for Excel File Creation
- Installation Instructions
- EPPlus Tutorial
- NPOI Tutorial
- ClosedXML Tutorial
- Library Comparison and Selection
- Advanced Features and Considerations
Popular Libraries for Excel File Creation
Several libraries excel at creating Excel files without requiring Microsoft Office:
- EPPlus - Excellent for .XLSX files, no dependencies, good performance
- NPOI - Supports both .XLS and .XLSX formats, open-source
- ClosedXML - User-friendly wrapper around OpenXML, supports .XLSX and .XLSM
- GemBox.Spreadsheet - Commercial library with free tier, supports multiple formats
- IronXL - Commercial library with simple API, supports .XLSX, .XLS, and .CSV
Each library has its strengths depending on your specific requirements for file format support, licensing, and features.
Installation Instructions
EPPlus Installation
Install-Package EPPlus
NPOI Installation
Install-Package NPOI
ClosedXML Installation
Install-Package ClosedXML
GemBox.Spreadsheet Installation
Install-Package GemBox.Spreadsheet
IronXL Installation
Install-Package IronXL
All libraries are available via NuGet Package Manager and can be installed directly from Visual Studio Package Manager Console or through the NuGet package manager interface.
EPPlus Tutorial
EPPlus is a popular library for working with .XLSX files that doesn’t require any dependencies and works well for server environments.
Key Features:
- Only supports .XLSX format (Excel 2007+)
- No dependencies in NuGet package
- Good performance for large datasets
- Free for commercial use (version 5+ requires license for commercial use)
Basic Example:
using OfficeOpenXml;
using System;
using System.IO;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// Set license context (required for EPPlus 5+)
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
// Create a list of data
List<string[]> data = new List<string[]>
{
new[] { "Name", "Age", "City" },
new[] { "John Doe", "25", "New York" },
new[] { "Jane Smith", "30", "London" },
new[] { "Bob Johnson", "35", "Paris" }
};
// Create Excel file
FileInfo fileInfo = new FileInfo("C:\\Temp\\output.xlsx");
using (ExcelPackage excelPackage = new ExcelPackage(fileInfo))
{
var worksheet = excelPackage.Workbook.Worksheets.Add("Sheet1");
// Write data to worksheet
for (int i = 0; i < data.Count; i++)
{
for (int j = 0; j < data[i].Length; j++)
{
worksheet.Cells[i + 1, j + 1].Value = data[i][j];
}
}
// Auto-fit columns
worksheet.Columns.AutoFit();
// Save the file
excelPackage.Save();
}
}
}
Advanced Features:
- Formulas and calculations
- Cell styling and formatting
- Charts and pivot tables
- Conditional formatting
- Merged cells
Note: EPPlus version 5 and later require a license for commercial use, though it has a free community edition.
NPOI Tutorial
NPOI is the .NET port of the Apache POI Java library and is the best choice when you need to support both .XLS and .XLSX formats.
Key Features:
- Supports both .XLS and .XLSX formats
- Open-source and free for commercial use
- Mature and stable
- Good performance
Basic Example (XLSX):
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.IO;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// Create a list of data
List<string[]> data = new List<string[]>
{
new[] { "Name", "Age", "City" },
new[] { "John Doe", "25", "New York" },
new[] { "Jane Smith", "30", "London" },
new[] { "Bob Johnson", "35", "Paris" }
};
// Create Excel file (XLSX format)
using (var workbook = new XSSFWorkbook())
{
var sheet = workbook.CreateSheet("Sheet1");
// Write data to worksheet
for (int i = 0; i < data.Count; i++)
{
var row = sheet.CreateRow(i);
for (int j = 0; j < data[i].Length; j++)
{
row.CreateCell(j).SetCellValue(data[i][j]);
}
}
// Auto-size columns
for (int i = 0; i < data[0].Length; i++)
{
sheet.AutoSizeColumn(i);
}
// Save the file
using (var fileStream = new FileStream("C:\\Temp\\output.xlsx", FileMode.Create, FileAccess.Write))
{
workbook.Write(fileStream);
}
}
}
}
Basic Example (XLS format):
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.IO;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// Create a list of data
List<string[]> data = new List<string[]>
{
new[] { "Name", "Age", "City" },
new[] { "John Doe", "25", "New York" },
new[] { "Jane Smith", "30", "London" },
new[] { "Bob Johnson", "35", "Paris" }
};
// Create Excel file (XLS format)
using (var workbook = new HSSFWorkbook())
{
var sheet = workbook.CreateSheet("Sheet1");
// Write data to worksheet
for (int i = 0; i < data.Count; i++)
{
var row = sheet.CreateRow(i);
for (int j = 0; j < data[i].Length; j++)
{
row.CreateCell(j).SetCellValue(data[i][j]);
}
}
// Auto-size columns
for (int i = 0; i < data[0].Length; i++)
{
sheet.AutoSizeColumn(i);
}
// Save the file
using (var fileStream = new FileStream("C:\\Temp\\output.xls", FileMode.Create, FileAccess.Write))
{
workbook.Write(fileStream);
}
}
}
}
ClosedXML Tutorial
ClosedXML is a user-friendly wrapper around the OpenXML SDK that provides an intuitive API for working with Excel files.
Key Features:
- Supports .XLSX and .XLSM formats (Excel 2007+)
- Intuitive and easy-to-use API
- Open-source and free for commercial use
- Good performance
Basic Example:
using ClosedXML.Excel;
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// Create a list of data
var data = new List<string[]>
{
new[] { "Name", "Age", "City" },
new[] { "John Doe", "25", "New York" },
new[] { "Jane Smith", "30", "London" },
new[] { "Bob Johnson", "35", "Paris" }
};
// Create Excel file
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add("Sheet1");
// Write data to worksheet
for (int i = 0; i < data.Count; i++)
{
for (int j = 0; j < data[i].Length; j++)
{
worksheet.Cell(i + 1, j + 1).Value = data[i][j];
}
}
// Add some styling
var headerRange = worksheet.Range("1:1");
headerRange.Style.Font.Bold = true;
headerRange.Style.Fill.BackgroundColor = XLColor.LightGray;
// Auto-fit columns
worksheet.Columns().AdjustToContents();
// Save the file
workbook.SaveAs("C:\\Temp\\output.xlsx");
}
}
}
Advanced Features:
- Rich styling capabilities
- Charts and conditional formatting
- Pivot tables
- Formulas and calculations
- Data validation
Library Comparison and Selection
Feature Comparison Table
| Library | .XLS Support | .XLSX Support | License | Dependencies | Ease of Use |
|---|---|---|---|---|---|
| EPPlus | ❌ | ✅ | Free (v5+ requires commercial license) | None | Medium |
| NPOI | ✅ | ✅ | Open-source (Apache 2.0) | None | Medium |
| ClosedXML | ❌ | ✅ | Open-source (LGPL) | OpenXML SDK | High |
| GemBox.Spreadsheet | ✅ | ✅ | Free/Commercial | None | High |
| IronXL | ✅ | ✅ | Free/Commercial | None | High |
When to Choose Each Library
Choose EPPlus when:
- You only need .XLSX format
- You want minimal dependencies
- You need good performance for large datasets
- You’re okay with the licensing requirements for commercial use
Choose NPOI when:
- You need both .XLS and .XLSX support
- You prefer open-source with permissive licensing
- You need compatibility with existing Java-based POI systems
Choose ClosedXML when:
- You only need .XLSX format
- You want an intuitive, developer-friendly API
- You prefer modern C# syntax and LINQ-style operations
- You need good performance without complex dependencies
Choose GemBox.Spreadsheet or IronXL when:
- You need comprehensive .XLS support
- You want the easiest API
- You’re willing to consider commercial licensing for advanced features
- You need enterprise-level support
Advanced Features and Considerations
Performance Considerations
For large datasets, performance can vary between libraries:
- EPPlus generally performs well with large datasets and has good memory management
- NPOI is efficient but may require more manual optimization for very large files
- ClosedXML is user-friendly but can be slower with extremely large datasets
File Size Optimization
All libraries handle compression differently:
// EPPlus - can control compression
excelPackage.CompressionLevel = CompressionLevel.BestCompression;
// ClosedXML - automatic compression
workbook.SaveAs("output.xlsx");
Error Handling
Always implement proper error handling when working with file operations:
try
{
// Excel file creation code
}
catch (Exception ex)
{
// Handle file access errors, formatting errors, etc.
Console.WriteLine($"Error creating Excel file: {ex.Message}");
}
Security Considerations
When creating Excel files on web servers:
- Validate input data to prevent Excel injection attacks
- Use proper file permissions
- Sanitize file names to prevent directory traversal
- Consider using temporary files with proper cleanup
Conclusion
Creating Excel files in C# without Microsoft Office is straightforward using specialized libraries that work directly with the file format specifications. The key takeaways are:
- For .XLSX-only needs, EPPlus and ClosedXML are excellent choices with good performance and ease of use
- For legacy .XLS support, NPOI is the best open-source option that handles both formats
- For the easiest API, consider commercial libraries like GemBox.Spreadsheet or IronXL
- All libraries are available via NuGet and require no Office installation on the target machine
- Performance and licensing should be your main considerations when selecting a library
Choose the library that best matches your specific requirements for file format support, budget, and development timeline. For most modern applications, ClosedXML provides the best balance of ease of use and performance, while NPOI remains the go-to solution when legacy .XLS format support is required.
Sources
- Excel File Creation in C# Without Microsoft Office
- How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office? - Stack Overflow
- Create Excel files from C# without office - Stack Overflow
- Creating Excel Files in C# Without Installing Microsoft Office | End Your If
- Create Excel file in C# without Interop (MVC (EPPlus) and Console (NPOI ) example) - QA With Experts
- 5 Excel Libraries Every .NET Developer Mostly use - DEV Community
- GitHub - ClosedXML/ClosedXML
- Which library to generate Excel in C#? OpenXmlSdk or ClosedXml? | Medium
- C# Create Excel File Without MS Office | Openize.OpenXML-SDK
- Writing Large Datasets to Excel Files Using EPPlus in C# - DZone
- Excel spreadsheet library for .NET Framework/Core - EPPlus Software
- Create Excels with C# and EPPlus: A tutorial
- What are the differences between the EPPlus and ClosedXML libraries for working with OpenXML? - Stack Overflow
- Reading an Excel File Using C# with Code Examples
- Compare to Other Components - IronXL