How to Create and Write Excel File in C#
To create and write Excel files in C#, you typically use a library because .NET does not natively generate .xlsx files. The most common modern choice is EPPlus, but there are alternatives too.
1) Recommended: EPPlus (modern + easy)
Step 1: Install package
dotnet add package EPPlus
Step 2: Create and write Excel file
using OfficeOpenXml;
using System.IO;
class Program
{
static void Main()
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (var package = new ExcelPackage())
{
var worksheet = package.Workbook.Worksheets.Add("Employees");
// Headers
worksheet.Cells[1, 1].Value = "ID";
worksheet.Cells[1, 2].Value = "Name";
worksheet.Cells[1, 3].Value = "Department";
// Data
worksheet.Cells[2, 1].Value = 1;
worksheet.Cells[2, 2].Value = "Alice";
worksheet.Cells[2, 3].Value = "HR";
worksheet.Cells[3, 1].Value = 2;
worksheet.Cells[3, 2].Value = "Bob";
worksheet.Cells[3, 3].Value = "IT";
// Save file
var file = new FileInfo("Employees.xlsx");
package.SaveAs(file);
}
}
}
What happens here
• Creates Excel workbook
• Adds worksheet
• Writes rows and columns
• Saves .xlsx file
2) Alternative: ClosedXML (very readable API)
Install:
dotnet add package ClosedXML
Example:
using ClosedXML.Excel;
class Program
{
static void Main()
{
var workbook = new XLWorkbook();
var sheet = workbook.Worksheets.Add("Employees");
sheet.Cell(1, 1).Value = "ID";
sheet.Cell(1, 2).Value = "Name";
sheet.Cell(2, 1).Value = 1;
sheet.Cell(2, 2).Value = "Alice";
workbook.SaveAs("Employees.xlsx");
}
}
3) Older approach: Interop (not recommended)
Uses Microsoft Excel Interop
Requires Excel installed on machine.
using Excel = Microsoft.Office.Interop.Excel;
var excel = new Excel.Application();
var workbook = excel.Workbooks.Add();
var sheet = workbook.Sheets[1];
sheet.Cells[1, 1] = "Hello Excel";
workbook.SaveAs("test.xlsx");
workbook.Close();
excel.Quit();
When to use Excel generation in C#
Use it when:
• Exporting reports
• Generating invoices
• Data export from databases
• Business reporting tools
Important tips
• Always close/save properly (using blocks)
• Avoid Interop in server environments
• Prefer EPPlus or ClosedXML for modern apps
• Excel files are .xlsx, not .xls
More detailed example for Microsoft.Office.Interop.Excel
First step is to add necessary references to the project.
1. Right click References and press Add References item in the menu.
2. Go to COM Tab and select Type Libraries.
3. Select the ticks of
- Microsoft Excel 14.0 Object Library
- Microsoft Office 14.0 Object Library
Required using statements:
using Microsoft.Office.Interop.Excel;
Here is the example code:
class Program
{
static void Main(string[] args)
{
object misValue = System.Reflection.Missing.Value;
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "deneme.xlsx");
var app = new Application();
var workbook = app.Workbooks.Add(misValue);
var sheet = (Worksheet)workbook.ActiveSheet;
// add new sheet after the existing sheets
// sheet = app.Worksheets.Add(Missing.Value, app.Worksheets[app.Worksheets.Count], Missing.Value, Missing.Value);
// choose existing sheet at index page
// var iter = app.Worksheets.GetEnumerator();
// for (int i = 0; i < page + 1; ++i)
// iter.MoveNext();
// sheet = iter.Current;
sheet.Cells[1, 1] = 5;
workbook.SaveAs(path);
app.Quit();
ReleaseObject(sheet);
ReleaseObject(workbook);
ReleaseObject(app);
Process.Start(path);
}
private static void ReleaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
Console.WriteLine("Unable to release the Object {0}", ex.ToString());
}
finally
{
GC.Collect();
}
}
}
Adding a line or bar chart to excel worksheet
Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
Excel.Chart chartPage = myChart.Chart;
chartRange = xlWorkSheet.get_Range("A1", "d5");
chartPage.SetSourceData(chartRange, Missing.Type);
chartPage.ChartType = Excel.XlChartType.xlColumnClustered;