Automate Excel with C#
Back to all C# Recipes

How to create an Excel file (XLSX) in C#

Introduction

This C# recipe shows how you can use EPPlus library to create an Excel file. Noteably this library does not require Excel to be installed on the server.

Ingredients

The recipe uses the EPPlus library version 4.5.3.3. This is the last version of EPPlus under the LGPL License (aka free for commercial uses). You can install it using Nuget Package manager:

Install-Package EPPlus -Version 4.5.3.3

C# Code Snippet

var path  = @"C:\Temp\ExcelRecipes\";
var fileName = "newfilename.xlsx";
var fileInfo = new FileInfo(path + fileName);

using (var excelFile = new ExcelPackage(fileInfo))
{
    // file requires at least one worksheet
   excelFile.Workbook.Worksheets.Add("Sheet1");
   excelFile.Save();
}

Related C# Recipes

How to create an Excel file (XLSX) in C# and populate it using a list of C# objects

This C# recipe shows how you can use EPPlus library to create an Excel file and populate the first worksheet from a list of C# objects