To create Excel files (.XLS
and .XLSX
) in C# without installing Microsoft Office, you can use open-source libraries or Microsoft’s Open XML SDK. Below are the most common and reliable methods:
1. Using the NPOI
Library
Supports: .XLS
(Excel 97-2003) and .XLSX
(Excel 2007+).
License: Apache License 2.0 (free for commercial use).
Steps:
- Install the NuGet Package:
Install-Package NPOI
- Code Example:
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel; // For XLSX
using NPOI.HSSF.UserModel; // For XLS
using System.IO;
public void CreateExcelFile(string filePath, bool isXlsx = true)
{
IWorkbook workbook;
// Create workbook for XLSX or XLS
if (isXlsx)
workbook = new XSSFWorkbook();
else
workbook = new HSSFWorkbook();
// Create a worksheet
ISheet sheet = workbook.CreateSheet("Sheet1");
// Add data to cells (Row 0, Column 0)
IRow row = sheet.CreateRow(0);
row.CreateCell(0).SetCellValue("Hello");
row.CreateCell(1).SetCellValue("World");
// Save to file
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
workbook.Write(fs);
}
}
2. Using EPPlus
(for .XLSX
only)
Supports: .XLSX
(Excel 2007+).
License: Polyform Noncommercial License (free for non-commercial use; commercial use requires a license).
- Install the NuGet Package:
Install-Package EPPlus
- Code Example:
using OfficeOpenXml;
using System.IO;
public void CreateExcelFile(string filePath)
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // Set license
using (var package = new ExcelPackage())
{
// Add a worksheet
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1");
// Add data to cells (Row 1, Column 1)
worksheet.Cells[1, 1].Value = "Hello";
worksheet.Cells[1, 2].Value = "World";
// Save to file
package.SaveAs(new FileInfo(filePath));
}
}
3. Using ClosedXML
(for .XLSX
only)
Supports: .XLSX
(Excel 2007+).
License: MIT License (free for commercial use).
- Install the NuGet Package:
Install-Package ClosedXML
- Code Example:
using ClosedXML.Excel;
using System.IO;
public void CreateExcelFile(string filePath)
{
using (var workbook = new XLWorkbook())
{
// Add a worksheet
var worksheet = workbook.Worksheets.Add("Sheet1");
// Add data to cells (Row 1, Column 1)
worksheet.Cell(1, 1).Value = "Hello";
worksheet.Cell(1, 2).Value = "World";
// Save to file
workbook.SaveAs(filePath);
}
}
4. Using Microsoft’s Open XML SDK (Low-Level)
Supports: .XLSX
only.
License: Free (Microsoft’s Open Specification Promise).
- Install the NuGet Package:
Install-Package DocumentFormat.OpenXml
- Code Example:
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Linq;
public void CreateExcelFile(string filePath)
{
// Create a new spreadsheet document
using (var document = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook))
{
// Add a workbook and worksheet
WorkbookPart workbookPart = document.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Add sheets to the workbook
Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
Sheet sheet = new Sheet()
{
Id = workbookPart.GetIdOfPart(worksheetPart),
SheetId = 1,
Name = "Sheet1"
};
sheets.Append(sheet);
// Add data to cells
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
Row row = new Row() { RowIndex = 1 };
row.Append(
new Cell() { CellValue = new CellValue("Hello"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("World"), DataType = CellValues.String }
);
sheetData.Append(row);
// Save changes
workbookPart.Workbook.Save();
}
}
Comparison
Method | Formats | Ease of Use | License |
---|---|---|---|
NPOI | XLS/XLSX | Moderate | Apache 2.0 (Commercial OK) |
EPPlus | XLSX | Easy | Non-commercial or Paid |
ClosedXML | XLSX | Easy | MIT (Commercial OK) |
Open XML SDK | XLSX | Complex | Free (Microsoft) |
Recommendation
- Use NPOI if you need
.XLS
support. - Use ClosedXML for
.XLSX
(simplest API). - Use Open XML SDK for low-level control (advanced use cases).
All methods work without Microsoft Office installed.