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 this tutorial, we will learn about creating Excel Spreadsheets in different formats (.xls
, .xlsx
, .csv
, and .tsv
), setting cell styles, and inserting data using C#.
How to Create an XLSX File in C#
- Install the Excel library to create XLSX files.
- Use the
Workbook
object to create an Excel file. - Choose a default
Worksheet
. - Add data to the default
Worksheet
. - Save the Excel file to the disk.
Step 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 for free for development.
Install-Package IronXL.Excel
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 a new XLSX WorkBook
WorkBook wb = WorkBook.Create();
// Create a new XLSX WorkBook
WorkBook wb = WorkBook.Create();
' Create a new XLSX WorkBook
Dim wb As WorkBook = WorkBook.Create()
2.2 Create .XLS File
To create a .xls
extension file, use the following code:
// Create a new XLS WorkBook
WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS);
// Create a new XLS WorkBook
WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS);
' Create a new XLS WorkBook
Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLS)
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
named sheet1
in WorkBook
wb
.
// Create a new WorkSheet in the WorkBook
WorkSheet ws1 = wb.CreateWorkSheet("sheet1");
// Create a new WorkSheet in the WorkBook
WorkSheet ws1 = wb.CreateWorkSheet("sheet1");
' Create a new WorkSheet in the WorkBook
Dim ws1 As WorkSheet = wb.CreateWorkSheet("sheet1")
3.1 Create Multiple WorkSheets
Multiple WorkSheets
can be created in the same way:
// Create multiple WorkSheets in the WorkBook
WorkSheet ws2 = wb.CreateWorkSheet("sheet2");
WorkSheet ws3 = wb.CreateWorkSheet("sheet3");
// Create multiple WorkSheets in the WorkBook
WorkSheet ws2 = wb.CreateWorkSheet("sheet2");
WorkSheet ws3 = wb.CreateWorkSheet("sheet3");
' Create multiple WorkSheets in the WorkBook
Dim ws2 As WorkSheet = wb.CreateWorkSheet("sheet2")
Dim ws3 As WorkSheet = wb.CreateWorkSheet("sheet3")
4. Insert Data into WorkSheets
Now we can easily insert data into WorkSheet cells:
// Insert data into a specific cell
worksheet["CellAddress"].Value = "MyValue";
// Insert data into a specific cell
worksheet["CellAddress"].Value = "MyValue";
' Insert data into a specific cell
worksheet("CellAddress").Value = "MyValue"
4.1 Insert Data into Specific WorkSheet
For example, data in ws1
can be specifically inserted. The code below will write Hello World
in the A1
cell of WorkSheet
ws1
.
// Insert "Hello World" into cell A1 of WorkSheet ws1
ws1["A1"].Value = "Hello World";
// Insert "Hello World" into cell A1 of WorkSheet ws1
ws1["A1"].Value = "Hello World";
' Insert "Hello World" into cell A1 of WorkSheet ws1
ws1("A1").Value = "Hello World"
4.2 Insert Data into Multiple Cells
It is also possible to write data into many cells using the range function. The code below will write NewValue
from cell A3
to A8
in WorkSheet
ws1
.
// Insert "NewValue" into cells A3 through A8 in WorkSheet ws1
ws1["A3:A8"].Value = "NewValue";
// Insert "NewValue" into cells A3 through A8 in WorkSheet ws1
ws1["A3:A8"].Value = "NewValue";
' Insert "NewValue" into cells A3 through A8 in WorkSheet ws1
ws1("A3:A8").Value = "NewValue"
5. Make a Sample Project
We will create a new Excel file Sample.xlsx
and insert data into it.
using IronXL;
class Program
{
static void Main(string[] args)
{
// Create a new WorkBook (XLSX by default)
WorkBook wb = WorkBook.Create();
// Create a new WorkSheet named "sheet1"
WorkSheet ws1 = wb.CreateWorkSheet("sheet1");
// Insert data into WorkSheet ws1
ws1["A1"].Value = "Hello";
ws1["A2"].Value = "World";
ws1["B1:B8"].Value = "RangeValue";
// Save the WorkBook as 'Sample.xlsx'
wb.SaveAs("Sample.xlsx");
}
}
using IronXL;
class Program
{
static void Main(string[] args)
{
// Create a new WorkBook (XLSX by default)
WorkBook wb = WorkBook.Create();
// Create a new WorkSheet named "sheet1"
WorkSheet ws1 = wb.CreateWorkSheet("sheet1");
// Insert data into WorkSheet ws1
ws1["A1"].Value = "Hello";
ws1["A2"].Value = "World";
ws1["B1:B8"].Value = "RangeValue";
// Save the WorkBook as 'Sample.xlsx'
wb.SaveAs("Sample.xlsx");
}
}
Imports IronXL
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Create a new WorkBook (XLSX by default)
Dim wb As WorkBook = WorkBook.Create()
' Create a new WorkSheet named "sheet1"
Dim ws1 As WorkSheet = wb.CreateWorkSheet("sheet1")
' Insert data into WorkSheet ws1
ws1("A1").Value = "Hello"
ws1("A2").Value = "World"
ws1("B1:B8").Value = "RangeValue"
' Save the WorkBook as 'Sample.xlsx'
wb.SaveAs("Sample.xlsx")
End Sub
End Class
Note: By default, the new Excel file will be created in the
bin>Debug
folder of the project. If you want to create a new file in a 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 functionality to set metadata for Excel files.
// Create a new WorkBook and set its metadata
WorkBook wb = WorkBook.Create();
wb.Metadata.Author = "AuthorName";
wb.Metadata.Title = "TitleValue";
// Create a new WorkBook and set its metadata
WorkBook wb = WorkBook.Create();
wb.Metadata.Author = "AuthorName";
wb.Metadata.Title = "TitleValue";
' Create a new WorkBook and set its metadata
Dim wb As WorkBook = WorkBook.Create()
wb.Metadata.Author = "AuthorName"
wb.Metadata.Title = "TitleValue"
7. Set Cell Style
It is very easy to set the cell style of an 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 for a specific cell
worksheet["CellAddress"].Style.Font.Bold = true;
worksheet["CellAddress"].Style.Font.Italic = true;
// Set font style for a specific cell
worksheet["CellAddress"].Style.Font.Bold = true;
worksheet["CellAddress"].Style.Font.Italic = true;
' Set font style for a specific cell
worksheet("CellAddress").Style.Font.Bold = True
worksheet("CellAddress").Style.Font.Italic = True
7.2. Add Strikeout
Strikeout to any cell value can be done as follows:
// Add strikeout to font in a specific cell
worksheet["CellAddress"].Style.Font.Strikeout = true;
// Add strikeout to font in a specific cell
worksheet["CellAddress"].Style.Font.Strikeout = true;
' Add strikeout to font in a specific cell
worksheet("CellAddress").Style.Font.Strikeout = True
7.3. Set Border Style
Border styling can be done using the following code:
// Set border styles for a specific cell
worksheet["CellAddress"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted;
// Set border styles for a specific cell
worksheet["CellAddress"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted;
' Set border styles for a specific cell
worksheet("CellAddress").Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted
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;
class Program
{
static void Main(string[] args)
{
// Create a new WorkBook and WorkSheet
WorkBook wb = WorkBook.Create();
WorkSheet ws = wb.CreateWorkSheet("sheet1");
// Insert data into WorkSheet
ws["A1"].Value = "MyVal";
ws["B2"].Value = "Hello World";
// Apply various styles to cells
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");
// Save the WorkBook with styles
wb.SaveAs("Sample.xlsx");
}
}
using IronXL;
class Program
{
static void Main(string[] args)
{
// Create a new WorkBook and WorkSheet
WorkBook wb = WorkBook.Create();
WorkSheet ws = wb.CreateWorkSheet("sheet1");
// Insert data into WorkSheet
ws["A1"].Value = "MyVal";
ws["B2"].Value = "Hello World";
// Apply various styles to cells
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");
// Save the WorkBook with styles
wb.SaveAs("Sample.xlsx");
}
}
Imports IronXL
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Create a new WorkBook and WorkSheet
Dim wb As WorkBook = WorkBook.Create()
Dim ws As WorkSheet = wb.CreateWorkSheet("sheet1")
' Insert data into WorkSheet
ws("A1").Value = "MyVal"
ws("B2").Value = "Hello World"
' Apply various styles to cells
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")
' Save the WorkBook with styles
wb.SaveAs("Sample.xlsx")
End Sub
End Class
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 ReferenceFrequently Asked Questions
What is this library for Excel files?
IronXL is a .NET library that facilitates working with Excel spreadsheets in C# applications. It allows developers to create, read, and edit Excel files in various formats, such as .xls, .xlsx, .csv, and .tsv.
How can I install this library for Excel handling?
You can install IronXL by downloading the DLL from the IronXL website or by using the NuGet package manager with the command: `nuget install IronXL.Excel`.
How do I create an XLSX file using C#?
To create an XLSX file in C#, use the IronXL library. First, create a `WorkBook` object, then add a `WorkSheet`, insert data into cells, and save the workbook using the `SaveAs` method.
Can I create multiple worksheets in a single workbook?
Yes, you can create multiple worksheets in a single workbook using IronXL. After creating a `WorkBook`, use the `CreateWorkSheet` method to add multiple sheets.
How do I insert data into an Excel worksheet using this library?
To insert data into a worksheet, reference specific cells using their address and set their `Value` property. For example, `worksheet['A1'].Value = 'Hello World';` inserts 'Hello World' into cell A1.
How can I style cells in an Excel worksheet?
IronXL allows styling of cells, including setting font styles, adding strikeout effects, and customizing border styles. Use the `Style` property of a cell to apply these customizations.
Is it possible to set metadata for an Excel file?
Yes, IronXL supports setting metadata for Excel files. You can set properties like `Author` and `Title` using the `Metadata` property of the `WorkBook` object.
Can I save an Excel file to a custom path?
Yes, you can save an Excel file to a custom path by specifying the desired path in the `SaveAs` method, e.g., `wb.SaveAs(@"E:\IronXL\Sample.xlsx");`.
What should I do if I want to learn more about Excel file creation in C#?
For a deeper understanding, you can refer to the 'Create Excel File Using C#' tutorial available on the IronXL website, which provides detailed guidance and examples.
Where can I find the API reference for this Excel library?
The API reference for IronXL is available on the IronXL website. It contains detailed documentation of all namespaces, classes, methods, and enums provided by IronXL.