Create XLSX File in C#
Published: 2022-02-16 05:04:23

---
canonical_url: https://ironsoftware.com/csharp/excel/docs/questions/create-xlsx-file-c-sharp/
---

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.

**C#:**
```
WorkBook wb = WorkBook.Create();
```

**VB:**
```
Dim wb As WorkBook = WorkBook.Create()
```

---

## 1. Download IronXL DLL

IronXL provides the simplest approach to creating Excel (.xlsx) files in C# projects. [Download the DLL](https://ironsoftware.com/csharp/excel/packages/IronXL.Package.For.create.xlsx.zip) or [NuGet install](https://www.nuget.org/packages/IronXL.Excel) 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`.

**C#:**
```
WorkBook wb = WorkBook.Create();
```

**VB:**
```
Dim wb As WorkBook = WorkBook.Create()
```

### 2.2 Create .XLS File

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

**C#:**
```
WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS);
```

**VB:**
```
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 `ws1` with name `sheet1` in WorkBook `wb`.

**C#:**
```
WorkSheet ws1 = wb.CreateWorkSheet("sheet1");
```

**VB:**
```
Dim ws1 As WorkSheet = wb.CreateWorkSheet("sheet1")
```

### 3.1 Create Multiple WorkSheets

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

**C#:**
```
WorkSheet ws2 = wb.CreateWorkSheet("sheet2");
WorkSheet ws3 = wb.CreateWorkSheet("sheet3");
```

**VB:**
```
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.

**C#:**
```
 worksheet["CellAddress"].Value = "MyValue";
```

**VB:**
```
worksheet("CellAddress").Value = "MyValue"
```

### 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`.

**C#:**
```
ws1["A1"].Value = "Hello World";
```

**VB:**
```
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 below code will write `NewValue` from cell `A3` to `A8` in WorkSheet `ws1`.

**C#:**
```
ws1["A3:A8"].Value = "NewValue";
```

**VB:**
```
ws1("A3:A8").Value = "NewValue"
```

---

## 5. Make a Sample Project

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

**C#:**
```
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");
}
```

**VB:**
```
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
```

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`:

![Image 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhtceqlhhg3omdumb0s8.png)

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.

**C#:**
```
WorkBook wb = WorkBook.Create();
wb.Metadata.Author = "AuthorName";
wb.Metadata.Title="TitleValue";
```

**VB:**
```
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 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:

**C#:**
```
WorkSheet["CellAddress"].Style.Font.Bold =true;
WorkSheet["CellAddress"].Style.Font.Italic =true;
```

**VB:**
```
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:

**C#:**
```
WorkSheet["CellAddress"].Style.Font.Strikeout = true;
```

**VB:**
```
WorkSheet("CellAddress").Style.Font.Strikeout = True
```

### 7.3. Set Border Style

Border styling can be done using the following code:

**C#:**
```
WorkSheet["CellAddress"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Dotted;
```

**VB:**
```
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.

**C#:**
```
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");
}
```

**VB:**
```
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
```

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

![Image 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ghfbebvivn1oq9vw3tos.png)
 
---

## 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](https://ironsoftware.com/csharp/excel/tutorials/create-excel-file-net/).

---