C# Create Excel

Creating Excel spreadsheet files programmatically is a common task for C# developers that we'll be tackling today, including learning about creating new Excel files, setting cell styles, and inserting data with C# programming. With the right code, you can fully customize and style your sheets to meet your requirements. We'll go step by step how to create C# Excel workbooks for your .NET project.


Step 1

1. Create C# Excel Spreadsheets with IronXL

Today we'll be using IronXL, a C# library for Excel functionality that makes working with Excel files a lot more efficient. It's available free for development projects. Install it and follow the tutorial.

Download to your project or follow NuGet to install into Visual Studio.


 PM > Install-Package IronXL.Excel

How to Tutorial

2. C# Create Excel WorkBook

Once we have IronXL installed in our project, we can create an Excel Workbook. Use the WorkBook.Create() function of IronXL.

WorkBook wb = WorkBook.Create();
WorkBook wb = WorkBook.Create();
Dim wb As WorkBook = WorkBook.Create()
VB   C#

It will create a new Excel WorkBook wb. We can specify the type of XL WorkBook ( .xlsx or xls ) by using ExcelFileFormat as the parameter in the WorkBook.Create() function as follows:

/**
Create Csharp WorkBook 
anchor-c-num-create-excel-workbook
**/
//for creating .xlsx extension file
WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX);
//for creating .xls extension file
WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS);
/**
Create Csharp WorkBook 
anchor-c-num-create-excel-workbook
**/
//for creating .xlsx extension file
WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX);
//for creating .xls extension file
WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS);
'''
'''Create Csharp WorkBook 
'''anchor-c-num-create-excel-workbook
'''*
'for creating .xlsx extension file
Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
'for creating .xls extension file
Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLS)
VB   C#

Now, we can use wb to create WorkSheets.


3. C# Create Excel SpreadSheet

To create a WorkSheet, IronXL provides the Workbook.CreateWorkSheet() function. The function requires a string parameter, in which we can specify the name of the WorkSheet.

WorkSheet ws = wb.CreateWorkSheet("SheetName");
WorkSheet ws = wb.CreateWorkSheet("SheetName");
Dim ws As WorkSheet = wb.CreateWorkSheet("SheetName")
VB   C#

wb is the WorkBook (see above) and ws is a newly created WorkSheet. In the same way, we can create as many WorkSheets as we need. For example:

/**
Create Csharp WorkSheets 
anchor-c-num-create-excel-workbook
**/
WorkSheet ws1 = wb.CreateWorkSheet("Sheet1");
WorkSheet ws2 = wb.CreateWorkSheet("Sheet2");
/**
Create Csharp WorkSheets 
anchor-c-num-create-excel-workbook
**/
WorkSheet ws1 = wb.CreateWorkSheet("Sheet1");
WorkSheet ws2 = wb.CreateWorkSheet("Sheet2");
'''
'''Create Csharp WorkSheets 
'''anchor-c-num-create-excel-workbook
'''*
Dim ws1 As WorkSheet = wb.CreateWorkSheet("Sheet1")
Dim ws2 As WorkSheet = wb.CreateWorkSheet("Sheet2")
VB   C#

4. Insert Cell Data

Now, we can insert data in the specified WorkSheet. We will use a cell addressing system of Excel:

/**
Insert Data in Cell Address
anchor-insert-cell-data
**/
WorkSheet["CellAddress"].Value = "Value";
/**
Insert Data in Cell Address
anchor-insert-cell-data
**/
WorkSheet["CellAddress"].Value = "Value";
'''
'''Insert Data in Cell Address
'''anchor-insert-cell-data
'''*
WorkSheet("CellAddress").Value = "Value"
VB   C#

5. Insert Data in Range

This methodology also allows us to insert values in as many cells as we need. We can use Range to insert data in a spread of cells.

/**
Insert Data in Range
anchor-insert-data-in-range
**/
WorkSheet["From Cell Address : To Cell Address"].Value="value";
/**
Insert Data in Range
anchor-insert-data-in-range
**/
WorkSheet["From Cell Address : To Cell Address"].Value="value";
'''
'''Insert Data in Range
'''anchor-insert-data-in-range
'''*
WorkSheet("From Cell Address : To Cell Address").Value="value"
VB   C#

This will insert value in all the cells which lie in the specified range. You can read more about C# Excel Ranges for use in your .NET project.


6. Save Excel File

After inserting data, we need to save the Excel file in the specified path.

/**
Save Excel File
anchor-save-excel-file
**/
WorkBook.SaveAs("Path + Filename");
/**
Save Excel File
anchor-save-excel-file
**/
WorkBook.SaveAs("Path + Filename");
'''
'''Save Excel File
'''anchor-save-excel-file
'''*
WorkBook.SaveAs("Path + Filename")
VB   C#

By the above single line of code, the newly created Excel WorkBook will save in the specified location. Dive deeper into C# Create Excel Spreadsheet examples.


7. Create, Insert Data, and Save Example

/**
Complete Example
anchor-create-insert-data-and-save-example
**/
using IronXL;
static void Main(string[] args)
{
    //Create new XL WorkBook
    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX);
    //Create worksheet of specified WorkBook
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data by cell addressing
    ws["A1"].Value = "Welcome";
    ws["A2"].Value = "To";
    ws["A3"].Value = "IronXL";
    //insert data by range
    ws["C3:C8"].Value = "Cell Value";
    //save the file in specified path
    wb.SaveAs("sample.xlsx");
    Console.WriteLine("successfully created.");
    Console.ReadKey();    
}
/**
Complete Example
anchor-create-insert-data-and-save-example
**/
using IronXL;
static void Main(string[] args)
{
    //Create new XL WorkBook
    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX);
    //Create worksheet of specified WorkBook
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data by cell addressing
    ws["A1"].Value = "Welcome";
    ws["A2"].Value = "To";
    ws["A3"].Value = "IronXL";
    //insert data by range
    ws["C3:C8"].Value = "Cell Value";
    //save the file in specified path
    wb.SaveAs("sample.xlsx");
    Console.WriteLine("successfully created.");
    Console.ReadKey();    
}
'''
'''Complete Example
'''anchor-create-insert-data-and-save-example
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'Create new XL WorkBook
	Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
	'Create worksheet of specified WorkBook
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data by cell addressing
	ws("A1").Value = "Welcome"
	ws("A2").Value = "To"
	ws("A3").Value = "IronXL"
	'insert data by range
	ws("C3:C8").Value = "Cell Value"
	'save the file in specified path
	wb.SaveAs("sample.xlsx")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
VB   C#

Here is a screenshot of our newly created Excel WorkBook named sample.xlsx:


8. C# Excel from DataTable

In what could otherwise be a complicated line of code, IronXL provides an efficient way to convert DataTable data into Excel files and save to a specified location. Create a new Excel file, and fill it from DataTable. Simple!

First, let's create a new DataTable with data. Then, create the Excel file, isnert, and save.

/**
Excel from DataTable
anchor-c-num-excel-from-datatable
**/
using IronXL;
static void Main(string[] args)
{
    //create new datatable
    DataTable dt = new DataTable();
    dt.Columns.Add("id");
    dt.Columns.Add("name");
    dt.Columns.Add("phone");
    //fill data in datatable
    for (int i = 0; i < 5; i++)
    {
        dt.Rows.Add("id" + i.ToString(), "name" + i.ToString(), "phone" + i.ToString());
    }
    //Create new XL file
    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX);
    //Create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("sheet1");
    //send data in worksheet from datatable
    int j = 1; 
    foreach (DataRow row in dt.Rows)
    {
        ws["A" + j].Value = row["id"].ToString();
        ws["B" + j].Value = row["name"].ToString();
        ws["C" + j].Value = row["phone"].ToString();
        j = j + 1;
    }
    //save the file
    wb.SaveAs("sample.xlsx");
}
/**
Excel from DataTable
anchor-c-num-excel-from-datatable
**/
using IronXL;
static void Main(string[] args)
{
    //create new datatable
    DataTable dt = new DataTable();
    dt.Columns.Add("id");
    dt.Columns.Add("name");
    dt.Columns.Add("phone");
    //fill data in datatable
    for (int i = 0; i < 5; i++)
    {
        dt.Rows.Add("id" + i.ToString(), "name" + i.ToString(), "phone" + i.ToString());
    }
    //Create new XL file
    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX);
    //Create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("sheet1");
    //send data in worksheet from datatable
    int j = 1; 
    foreach (DataRow row in dt.Rows)
    {
        ws["A" + j].Value = row["id"].ToString();
        ws["B" + j].Value = row["name"].ToString();
        ws["C" + j].Value = row["phone"].ToString();
        j = j + 1;
    }
    //save the file
    wb.SaveAs("sample.xlsx");
}
'''
'''Excel from DataTable
'''anchor-c-num-excel-from-datatable
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new datatable
	Dim dt As New DataTable()
	dt.Columns.Add("id")
	dt.Columns.Add("name")
	dt.Columns.Add("phone")
	'fill data in datatable
	For i As Integer = 0 To 4
		dt.Rows.Add("id" & i.ToString(), "name" & i.ToString(), "phone" & i.ToString())
	Next i
	'Create new XL file
	Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
	'Create WorkSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("sheet1")
	'send data in worksheet from datatable
	Dim j As Integer = 1
	For Each row As DataRow In dt.Rows
		ws("A" & j).Value = row("id").ToString()
		ws("B" & j).Value = row("name").ToString()
		ws("C" & j).Value = row("phone").ToString()
		j = j + 1
	Next row
	'save the file
	wb.SaveAs("sample.xlsx")
End Sub
VB   C#

Here's a look at our output:

Now, let's move that to set cell properties of XL WorkBook using IronXL.


9. Set Excel WorkBook Style

Now, let's set the cell properties. Sometimes we need to set styles programmatically with a variety of different requirements. IronXL does allow us many options for different functions to easily set cell styles.

We can use the cell addressing system of Excel files to specify where styling should be applied. Let's set some basic styling properties that may often use in our daily life.

//bold the text of specified cell
WorkSheet["CellAddress"].Style.Font.Bold =true;

//Italic the text of specified cell
WorkSheet["CellAddress"].Style.Font.Italic =true;

//Strikeout the text of specified cell
WorkSheet["CellAddress"].Style.Font.Strikeout = true;

//border style of specific cell 
WorkSheet["CellAddress"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted;

//border color of specific cell 
WorkSheet["CellAddress"].Style.BottomBorder.SetColor("color value");
//bold the text of specified cell
WorkSheet["CellAddress"].Style.Font.Bold =true;

//Italic the text of specified cell
WorkSheet["CellAddress"].Style.Font.Italic =true;

//Strikeout the text of specified cell
WorkSheet["CellAddress"].Style.Font.Strikeout = true;

//border style of specific cell 
WorkSheet["CellAddress"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted;

//border color of specific cell 
WorkSheet["CellAddress"].Style.BottomBorder.SetColor("color value");
'bold the text of specified cell
WorkSheet("CellAddress").Style.Font.Bold =True

'Italic the text of specified cell
WorkSheet("CellAddress").Style.Font.Italic =True

'Strikeout the text of specified cell
WorkSheet("CellAddress").Style.Font.Strikeout = True

'border style of specific cell 
WorkSheet("CellAddress").Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted

'border color of specific cell 
WorkSheet("CellAddress").Style.BottomBorder.SetColor("color value")
VB   C#

IronXL also provides a way to set all the above properties in a specified range. Cell styling will apply on all the cells which lie within that range, like this:

//bold the text of specified range cells
WorkSheet["FromCellAddress : ToCellAddress"].Style.Font.Bold =true;

//Italic the text of specified range cells
WorkSheet["FromCellAddress : ToCellAddress"].Style.Font.Italic =true;

//Strikeout the text of specified range cells
WorkSheet["FromCellAddress : ToCellAddress"].Style.Font.Strikeout = true;

//border style of specified range cells 
WorkSheet["FromCellAddress : ToCellAddress"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted;

//border color of specified range cells 
WorkSheet["FromCellAddress : ToCellAddress"].Style.BottomBorder.SetColor("color value");
//bold the text of specified range cells
WorkSheet["FromCellAddress : ToCellAddress"].Style.Font.Bold =true;

//Italic the text of specified range cells
WorkSheet["FromCellAddress : ToCellAddress"].Style.Font.Italic =true;

//Strikeout the text of specified range cells
WorkSheet["FromCellAddress : ToCellAddress"].Style.Font.Strikeout = true;

//border style of specified range cells 
WorkSheet["FromCellAddress : ToCellAddress"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted;

//border color of specified range cells 
WorkSheet["FromCellAddress : ToCellAddress"].Style.BottomBorder.SetColor("color value");
'bold the text of specified range cells
WorkSheet("FromCellAddress : ToCellAddress").Style.Font.Bold =True

'Italic the text of specified range cells
WorkSheet("FromCellAddress : ToCellAddress").Style.Font.Italic =True

'Strikeout the text of specified range cells
WorkSheet("FromCellAddress : ToCellAddress").Style.Font.Strikeout = True

'border style of specified range cells 
WorkSheet("FromCellAddress : ToCellAddress").Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted

'border color of specified range cells 
WorkSheet("FromCellAddress : ToCellAddress").Style.BottomBorder.SetColor("color value")
VB   C#

Let's see a full example including creating a new XL WorkBook and applying cell styling.

/**
Set Workbook Styling
anchor-set-excel-workbook-style
**/
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX);
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    ws["B2:G2"].Value = "Range1";          
    ws["B4:G4"].Value = "Range2";
    //------setting the styles----------
    ws["B2:D2"].Style.Font.Bold = true;
    ws["E2:G2"].Style.Font.Italic = true;
    ws["B4:D4"].Style.Font.Strikeout = true;
    ws["E4:G4"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted;
    ws["E4:G4"].Style.BottomBorder.SetColor("#ff6600");
    wb.SaveAs("sample.xlsx");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
/**
Set Workbook Styling
anchor-set-excel-workbook-style
**/
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX);
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    ws["B2:G2"].Value = "Range1";          
    ws["B4:G4"].Value = "Range2";
    //------setting the styles----------
    ws["B2:D2"].Style.Font.Bold = true;
    ws["E2:G2"].Style.Font.Italic = true;
    ws["B4:D4"].Style.Font.Strikeout = true;
    ws["E4:G4"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted;
    ws["E4:G4"].Style.BottomBorder.SetColor("#ff6600");
    wb.SaveAs("sample.xlsx");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
'''
'''Set Workbook Styling
'''anchor-set-excel-workbook-style
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	ws("B2:G2").Value = "Range1"
	ws("B4:G4").Value = "Range2"
	'------setting the styles----------
	ws("B2:D2").Style.Font.Bold = True
	ws("E2:G2").Style.Font.Italic = True
	ws("B4:D4").Style.Font.Strikeout = True
	ws("E4:G4").Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted
	ws("E4:G4").Style.BottomBorder.SetColor("#ff6600")
	wb.SaveAs("sample.xlsx")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
VB   C#

Here is what our newly created Excel WorkBook named sample.xlsx looks like:

There's so much more you can do with your Excel worksheets programmatically. For a longer tutorial on C# open and write Excel files in .NET, follow the code examples and steps provided.


Tutorial Quick Access

C# Create Excel Documentation

Explore the documentation for creating Excel workbooks, worksheets, applying cell styling, and so much more in the API Reference for IronXL.

C# Create Excel Documentation