Create 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.

Create XLSX File
WorkBook wb = WorkBook.Create();
Dim wb As WorkBook = WorkBook.Create()
Jump to Article
Create WorkSheets
WorkSheet ws2 = wb.CreateWorkSheet("sheet2");
WorkSheet ws3 = wb.CreateWorkSheet("sheet3");
Dim ws2 As WorkSheet = wb.CreateWorkSheet("sheet2")
Dim ws3 As WorkSheet = wb.CreateWorkSheet("sheet3")
Jump to Article
Insert WorkSheet Data
ws1["A1"].Value = "Hello World";
ws1("A1").Value = "Hello World"
Jump to Article
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");
}
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
Jump to Article
Set Metadata
WorkBook wb = WorkBook.Create();
wb.Metadata.Author = "AuthorName";
wb.Metadata.Title="TitleValue";
Dim wb As WorkBook = WorkBook.Create()
wb.Metadata.Author = "AuthorName"
wb.Metadata.Title="TitleValue"
Jump to Article
Set Font Style
WorkSheet["CellAddress"].Style.Font.Bold =true;
WorkSheet["CellAddress"].Style.Font.Italic =true;
WorkSheet("CellAddress").Style.Font.Bold =True
WorkSheet("CellAddress").Style.Font.Italic =True
Jump to Article
Add Strikeout
WorkSheet["CellAddress"].Style.Font.Strikeout = true;
WorkSheet("CellAddress").Style.Font.Strikeout = True
Jump to Article
Set Border Style
WorkSheet["CellAddress"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted;
WorkSheet("CellAddress").Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted
Jump to Article
Sample Cell Styling Set
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");
}
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
Jump to Article
Try IronXL free for development


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.

 PM > 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.

WorkBook wb = WorkBook.Create();
WorkBook wb = WorkBook.Create();
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:

WorkSheet ws2 = wb.CreateWorkSheet("sheet2");
WorkSheet ws3 = wb.CreateWorkSheet("sheet3");
WorkSheet ws2 = wb.CreateWorkSheet("sheet2");
WorkSheet ws3 = wb.CreateWorkSheet("sheet3");
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.

ws1["A1"].Value = "Hello World";
ws1["A1"].Value = "Hello World";
ws1("A1").Value = "Hello World"
VB   C#

4.1 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.

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");
}
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");
}
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.

WorkBook wb = WorkBook.Create();
wb.Metadata.Author = "AuthorName";
wb.Metadata.Title="TitleValue";
WorkBook wb = WorkBook.Create();
wb.Metadata.Author = "AuthorName";
wb.Metadata.Title="TitleValue";
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:

WorkSheet["CellAddress"].Style.Font.Bold =true;
WorkSheet["CellAddress"].Style.Font.Italic =true;
WorkSheet["CellAddress"].Style.Font.Bold =true;
WorkSheet["CellAddress"].Style.Font.Italic =true;
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:

WorkSheet["CellAddress"].Style.Font.Strikeout = true;
WorkSheet["CellAddress"].Style.Font.Strikeout = true;
WorkSheet("CellAddress").Style.Font.Strikeout = True
VB   C#

7.3. Set Border Style

Border styling can be done using the following code:

WorkSheet["CellAddress"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted;
WorkSheet["CellAddress"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted;
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.

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");
}
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");
}
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 Object Reference

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

View the Object Reference