Create an XLSX File in C#

With so much automation in business, we frequently need to work with Excel Spreadsheets in .NET applications, including creating spreadsheets and inserting data programmatically. In the following tutorial, we will learn about creating the Excel Spreadsheet in different formats (.xls, .xlsx, .csv and .tsv), setting cell styles, and inserting data using C# programming.


How to Create XLXL File in C#

  1. Install the Excel library to create XLSX files.
  2. Use the Workbook object to create an Excel file.
  3. Choose a default Worksheet.
  4. Add data to the default Worksheet.
  5. Save the Excel file to the disk.

Step 1

1. Download IronXL DLL

IronXL provides the simplest approach to creating Excel (.xlsx) files in C# projects. Download the DLL or NuGet install and use it free for development.

Install-Package IronXL.Excel

How to Tutorial

2. Create A Workbook

Using this software, we can insert data and also set cell properties, like font styles or borders.

2.1 Create .XLSX File

Create a WorkBook using this code to make a new Excel file, which, by default, has the extension .xlsx.

/**
Create XLSX File
anchor-create-a-workbook
**/
WorkBook wb = WorkBook.Create();
/**
Create XLSX File
anchor-create-a-workbook
**/
WorkBook wb = WorkBook.Create();
'''
'''Create XLSX File
'''anchor-create-a-workbook
'''*
Dim wb As WorkBook = WorkBook.Create()
VB   C#

2.2 Create .XLS File

If you want to create a .xls extension file, use:

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

3. Create Excel WorkSheet

After creating a WorkBook in the file format of your choice, create an Excel WorkSheet. This code will create a new WorkSheet ws1 with name sheet1 in WorkBook wb.

WorkSheet ws1 = wb.CreateWorkSheet("sheet1");
WorkSheet ws1 = wb.CreateWorkSheet("sheet1");
Dim ws1 As WorkSheet = wb.CreateWorkSheet("sheet1")
VB   C#

3.1 Create Multiple WorkSheets

A number of WorkSheets can be created in the same way:

/**
Create WorkSheets
anchor-create-an-excel-worksheet
**/
WorkSheet ws2 = wb.CreateWorkSheet("sheet2");
WorkSheet ws3 = wb.CreateWorkSheet("sheet3");
/**
Create WorkSheets
anchor-create-an-excel-worksheet
**/
WorkSheet ws2 = wb.CreateWorkSheet("sheet2");
WorkSheet ws3 = wb.CreateWorkSheet("sheet3");
'''
'''Create WorkSheets
'''anchor-create-an-excel-worksheet
'''*
Dim ws2 As WorkSheet = wb.CreateWorkSheet("sheet2")
Dim ws3 As WorkSheet = wb.CreateWorkSheet("sheet3")
VB   C#

4. Insert Data into WorkSheets

Now we can easily insert data into WorkSheet cells.

 worksheet ["CellAddress"].Value = "MyValue";
 worksheet ["CellAddress"].Value = "MyValue";
worksheet ("CellAddress").Value = "MyValue"
VB   C#

4.1 Insert Data into Specific WorkSheet

For example, data in worksheet ws1 can be specifically inserted. The code below will write Hello World in A1 cell of WorkSheet ws1.

/**
Insert WorkSheet Data
anchor-insert-data-into-worksheets
**/
ws1 ["A1"].Value = "Hello World";
/**
Insert WorkSheet Data
anchor-insert-data-into-worksheets
**/
ws1 ["A1"].Value = "Hello World";
'''
'''Insert WorkSheet Data
'''anchor-insert-data-into-worksheets
'''*
ws1 ("A1").Value = "Hello World"
VB   C#

4.2 Insert Data into Multiple Cells

It is also possible to write data into many cells using the range function. The below code will write NewValue from cell A3 to A8 in WorkSheet ws1.

ws1 ["A3:A8"].Value = "NewValue";
ws1 ["A3:A8"].Value = "NewValue";
ws1 ("A3:A8").Value = "NewValue"
VB   C#

5. Make a Sample Project

We will create a new Excel file Sample.xlsx and insert data in it.

/**
Sample Project
anchor-make-a-sample-project
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Create();  
    WorkSheet ws1 = wb.CreateWorkSheet("sheet1");                    
    ws1 ["A1"].Value = "Hello";           
    ws1 ["A2"].Value = "World";
    ws1 ["B1:B8"].Value = "RangeValue";
    wb.SaveAs("Sample.xlsx");
}
/**
Sample Project
anchor-make-a-sample-project
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Create();  
    WorkSheet ws1 = wb.CreateWorkSheet("sheet1");                    
    ws1 ["A1"].Value = "Hello";           
    ws1 ["A2"].Value = "World";
    ws1 ["B1:B8"].Value = "RangeValue";
    wb.SaveAs("Sample.xlsx");
}
'''
'''Sample Project
'''anchor-make-a-sample-project
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Create()
	Dim ws1 As WorkSheet = wb.CreateWorkSheet("sheet1")
	ws1 ("A1").Value = "Hello"
	ws1 ("A2").Value = "World"
	ws1 ("B1:B8").Value = "RangeValue"
	wb.SaveAs("Sample.xlsx")
End Sub
VB   C#

Note: By default, the new Excel file will be created in the bin>Debug folder of the project. If we want to create new file in custom path, use: wb.SaveAs(@"E:\IronXL\Sample.xlsx");

Here's a screenshot of our newly created Excel file sample.xlsx:

It is clear how simple it is to create Excel files using IronXL in your C# Application.


6. Set ExcelMetadata for Excel Files

IronXL also provides the function to set metadata for Excel files.

/**
Set Metadata
anchor-set-metadata-for-excel-files
**/
WorkBook wb = WorkBook.Create();
wb.Metadata.Author = "AuthorName";
wb.Metadata.Title="TitleValue";
/**
Set Metadata
anchor-set-metadata-for-excel-files
**/
WorkBook wb = WorkBook.Create();
wb.Metadata.Author = "AuthorName";
wb.Metadata.Title="TitleValue";
'''
'''Set Metadata
'''anchor-set-metadata-for-excel-files
'''*
Dim wb As WorkBook = WorkBook.Create()
wb.Metadata.Author = "AuthorName"
wb.Metadata.Title="TitleValue"
VB   C#

7. Set Cell Style

It is very easy to set the cell style of Excel WorkSheet in your C# application. IronXL provides all necessary cell styling properties.

7.1. Set Font Style

Font style can be set as follows:

/**
Set Font Style
anchor-set-font-style
**/
WorkSheet ["CellAddress"].Style.Font.Bold =true;
WorkSheet ["CellAddress"].Style.Font.Italic =true;
/**
Set Font Style
anchor-set-font-style
**/
WorkSheet ["CellAddress"].Style.Font.Bold =true;
WorkSheet ["CellAddress"].Style.Font.Italic =true;
'''
'''Set Font Style
'''anchor-set-font-style
'''*
WorkSheet ("CellAddress").Style.Font.Bold =True
WorkSheet ("CellAddress").Style.Font.Italic =True
VB   C#

7.2. Add Strikeout

Strikeout to any cell value can be done as follows:

/**
Add Strikeout
anchor-add-strikeout
**/
WorkSheet ["CellAddress"].Style.Font.Strikeout = true;
/**
Add Strikeout
anchor-add-strikeout
**/
WorkSheet ["CellAddress"].Style.Font.Strikeout = true;
'''
'''Add Strikeout
'''anchor-add-strikeout
'''*
WorkSheet ("CellAddress").Style.Font.Strikeout = True
VB   C#

7.3. Set Border Style

Border styling can be done using the following code:

/**
Set Border Style
anchor-set-border-style
**/
WorkSheet ["CellAddress"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted;
/**
Set Border Style
anchor-set-border-style
**/
WorkSheet ["CellAddress"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted;
'''
'''Set Border Style
'''anchor-set-border-style
'''*
WorkSheet ("CellAddress").Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted
VB   C#

8. Apply Cell Styling Sample Project

Let's see how to set multiple cell styles and bring it all together using the example project below.

/**
Sample Cell Styling Set
anchor-apply-cell-styling-sample-project
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Create();                     
    WorkSheet ws = wb.CreateWorkSheet("sheet1");

    ws ["A1"].Value = "MyVal";
    ws ["B2"].Value = "Hello World";

    ws ["A1"].Style.Font.Strikeout = true;

    ws ["B2"].Style.Font.Bold =true;
    ws ["B2"].Style.Font.Italic =true;

    ws ["C3"].Style.TopBorder.Type = IronXL.Styles.BorderType.Double;        
    ws ["C3"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted;
    ws ["C3"].Style.LeftBorder.Type = IronXL.Styles.BorderType.Thick;
    ws ["C3"].Style.RightBorder.Type = IronXL.Styles.BorderType.SlantedDashDot;
    ws ["C3"].Style.BottomBorder.SetColor("#ff6600");
    ws ["C3"].Style.TopBorder.SetColor("#ff6600");
    wb.SaveAs("Sample.xlsx");
}
/**
Sample Cell Styling Set
anchor-apply-cell-styling-sample-project
**/
using IronXL;
static void Main(string [] args)
{
    WorkBook wb = WorkBook.Create();                     
    WorkSheet ws = wb.CreateWorkSheet("sheet1");

    ws ["A1"].Value = "MyVal";
    ws ["B2"].Value = "Hello World";

    ws ["A1"].Style.Font.Strikeout = true;

    ws ["B2"].Style.Font.Bold =true;
    ws ["B2"].Style.Font.Italic =true;

    ws ["C3"].Style.TopBorder.Type = IronXL.Styles.BorderType.Double;        
    ws ["C3"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted;
    ws ["C3"].Style.LeftBorder.Type = IronXL.Styles.BorderType.Thick;
    ws ["C3"].Style.RightBorder.Type = IronXL.Styles.BorderType.SlantedDashDot;
    ws ["C3"].Style.BottomBorder.SetColor("#ff6600");
    ws ["C3"].Style.TopBorder.SetColor("#ff6600");
    wb.SaveAs("Sample.xlsx");
}
'''
'''Sample Cell Styling Set
'''anchor-apply-cell-styling-sample-project
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Create()
	Dim ws As WorkSheet = wb.CreateWorkSheet("sheet1")

	ws ("A1").Value = "MyVal"
	ws ("B2").Value = "Hello World"

	ws ("A1").Style.Font.Strikeout = True

	ws ("B2").Style.Font.Bold =True
	ws ("B2").Style.Font.Italic =True

	ws ("C3").Style.TopBorder.Type = IronXL.Styles.BorderType.Double
	ws ("C3").Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted
	ws ("C3").Style.LeftBorder.Type = IronXL.Styles.BorderType.Thick
	ws ("C3").Style.RightBorder.Type = IronXL.Styles.BorderType.SlantedDashDot
	ws ("C3").Style.BottomBorder.SetColor("#ff6600")
	ws ("C3").Style.TopBorder.SetColor("#ff6600")
	wb.SaveAs("Sample.xlsx")
End Sub
VB   C#

And here is a Screenshot of our newly created Excel file sample.xlsx.


9. Next Steps and Excel Tutorial

If you want to dive deeper and read a step-by-step guide for .NET Excel creation, check out the Create Excel Files Using C# tutorial.


Tutorial Quick Access

Dive into the API Reference

Read the Documentation for IronXL, with explanations of all of IronXL’s namespaces, feature sets, classes, methods fields and enums.

View the API Reference