Generate Excel File C#

The applications we develop are constantly communicating with Excel spreadsheets to obtain data for evaluation and results. It's really helpful to be able to generate Excel files in C# programmatically, saving us time and effort with our development. In this tutorial we'll learn about generating the Excel file in different formats, setting cell styles and inserting data using efficient function C# programming.

Generate XLSX File
using IronXL;
static void Main(string[] args)
{
    //create new WorkBook of .xlsx Extension
    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX);
    //create workSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "Hello";
    ws["A2"].Value = "World";
    ws["C4"].Value = "IronXL";
    //save the file as .xlsx
    wb.SaveAs("sample.xlsx");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook of .xlsx Extension
	Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
	'create workSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws("A1").Value = "Hello"
	ws("A2").Value = "World"
	ws("C4").Value = "IronXL"
	'save the file as .xlsx
	wb.SaveAs("sample.xlsx")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
Jump to Article
Generate XLS File
using IronXL;
static void Main(string[] args)
{
    //create new WorkBook of .xls Extension 
    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS);
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "Hello";
    ws["A2"].Value = "World";
    //save the file as .xls
    wb.SaveAs("sample.xls");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook of .xls Extension 
	Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLS)
	'create WorkSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws("A1").Value = "Hello"
	ws("A2").Value = "World"
	'save the file as .xls
	wb.SaveAs("sample.xls")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
Jump to Article
Generate CSV File
using IronXL;
static void Main(string[] args)
{
    //create new WorkBook  
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "Hello";
    ws["A2"].Value = "World";
    //save the file as .csv
    wb.SaveAsCsv("sample.csv");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook  
	Dim wb As WorkBook = WorkBook.Create()
	'create WorkSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws("A1").Value = "Hello"
	ws("A2").Value = "World"
	'save the file as .csv
	wb.SaveAsCsv("sample.csv")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
Jump to Article
Generate TSV File
using IronXL;
static void Main(string[] args)
{ 
    //create new WorkBook  
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "Hello";
    ws["A2"].Value = "World";
    //save the file as .tsv
    wb.SaveAs("sample.tsv");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook  
	Dim wb As WorkBook = WorkBook.Create()
	'create WorkSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws("A1").Value = "Hello"
	ws("A2").Value = "World"
	'save the file as .tsv
	wb.SaveAs("sample.tsv")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
Jump to Article
Generate JSON File
using IronXL;
static void Main(string[] args)
{
    //create new WorkBook 
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "1";
    ws["A2"].Value = "john";
    ws["B1"].Value = "2";
    ws["B2"].Value = "alex";
    ws["C1"].Value = "3";
    ws["C2"].Value = "stokes";
    //save the file as .json
    wb.SaveAsJson("sample.json");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook 
	Dim wb As WorkBook = WorkBook.Create()
	'create WorkSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws("A1").Value = "1"
	ws("A2").Value = "john"
	ws("B1").Value = "2"
	ws("B2").Value = "alex"
	ws("C1").Value = "3"
	ws("C2").Value = "stokes"
	'save the file as .json
	wb.SaveAsJson("sample.json")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
Jump to Article
Generate XML File
using IronXL;
static void Main(string[] args)
{
    //create new WorkBook 
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "Hello";
    ws["A2"].Value = "World";
    //save the file as .json
    wb.SaveAsXml("sample.xml");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook 
	Dim wb As WorkBook = WorkBook.Create()
	'create WorkSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws("A1").Value = "Hello"
	ws("A2").Value = "World"
	'save the file as .json
	wb.SaveAsXml("sample.xml")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
Jump to Article
Try IronXL free for development


Step 1

1. Generate Excel Files with IronXL

Generate Excel Files using the IronXL Excel for C# Library, providing a range of functions for generating and manipulating data in your project. The library is free for development, with licenses available when you're ready to push live. To follow this tutorial, you can download IronXL for generating or access it through Visual Studio and NuGet gallery.


 PM > Install-Package IronXL.Excel


How to Tutorial

2. C# Excel File Generator Overview

In business application development, we often need to generate different types of Excel files programmatically. For this purpose, we need the easiest and quickest way to generate different types of files and save them in the required location automatically.

After installing IronXL, we can use the functions to generate the different Excel file types:

  • Excel file with .xlsx extension.
  • Excel file with .xls extension.
  • Comma Separated Value (.csv) files.
  • Tab Separated Value (.tsv) files.
  • Javascript Object Notation (.json) files.
  • Extensible Markup Language (.xml) files.

To generate any type of file, firstly we need to create an Excel WorkBook.

//generate New WorkBook
WorkBook wb = WorkBook.Create();
//generate New WorkBook
WorkBook wb = WorkBook.Create();
'generate New WorkBook
Dim wb As WorkBook = WorkBook.Create()
VB   C#

The above line of code will create new WorkBook wb, and now we will create a WorkSheet.

//Generate New WorkSheet
WorkSheet ws = WorkBook.CreateWorkSheet("SheetName");
//Generate New WorkSheet
WorkSheet ws = WorkBook.CreateWorkSheet("SheetName");
'Generate New WorkSheet
Dim ws As WorkSheet = WorkBook.CreateWorkSheet("SheetName")
VB   C#

This will create a WorkSheet ws that we can use to insert data in Excel files.


3. Generate XLSX File C#

First, we follow the steps above to generate the WorkBook and WorkSheets.

Then, we insert data in it to create our .xlsx extension file. For this purpose, IronXL provides a Cell Addressing System that allows us to insert data in a specific cell address programmatically.

//Insert data by cell addressing
 WorkSheet["CellAddress"].Value = "MyValue";
//Insert data by cell addressing
 WorkSheet["CellAddress"].Value = "MyValue";
'Insert data by cell addressing
 WorkSheet("CellAddress").Value = "MyValue"
VB   C#

It will insert a new value we called MyValue in a specific cell address. In the same way, we can insert data in as many cells as we require. After this, we will save Excel file in the specified path as follows:

//specify file path and name
WorkBook.SaveAs("Path + FileName.xlsx");
//specify file path and name
WorkBook.SaveAs("Path + FileName.xlsx");
'specify file path and name
WorkBook.SaveAs("Path + FileName.xlsx")
VB   C#

This will create a new Excel file with the extension .xlsx in the specified path. Don't forget to write extension .xlsx with file name, while saving.

To take a step further into how to create an XL Workbook in a CSharp project, check out the code examples here.

using IronXL;
static void Main(string[] args)
{
    //create new WorkBook of .xlsx Extension
    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX);
    //create workSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "Hello";
    ws["A2"].Value = "World";
    ws["C4"].Value = "IronXL";
    //save the file as .xlsx
    wb.SaveAs("sample.xlsx");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
using IronXL;
static void Main(string[] args)
{
    //create new WorkBook of .xlsx Extension
    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX);
    //create workSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "Hello";
    ws["A2"].Value = "World";
    ws["C4"].Value = "IronXL";
    //save the file as .xlsx
    wb.SaveAs("sample.xlsx");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook of .xlsx Extension
	Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
	'create workSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws("A1").Value = "Hello"
	ws("A2").Value = "World"
	ws("C4").Value = "IronXL"
	'save the file as .xlsx
	wb.SaveAs("sample.xlsx")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
VB   C#

You can see the screenshot of newly created Excel file Sample.xlsx here:


4. Generate XLS File C#

It is also possible to generate .xls files using IonXL. For this purpose, we will use WorkBook.Create() function as follows:

WorkBook.Create(ExcelFileFormat.XLS)
WorkBook.Create(ExcelFileFormat.XLS)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'WorkBook.Create(ExcelFileFormat.XLS)
VB   C#

This will create a new Excel file with .xls extension. Keep in mind that while assigning a name to an Excel file, you must write extension .xls with file name, like this:

WorkBook.SaveAs("Path + FileName.xls");

Now, Let's see the example of how to generate Excel file with .xls extension:

using IronXL;
static void Main(string[] args)
{
    //create new WorkBook of .xls Extension 
    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS);
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "Hello";
    ws["A2"].Value = "World";
    //save the file as .xls
    wb.SaveAs("sample.xls");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
using IronXL;
static void Main(string[] args)
{
    //create new WorkBook of .xls Extension 
    WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS);
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "Hello";
    ws["A2"].Value = "World";
    //save the file as .xls
    wb.SaveAs("sample.xls");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook of .xls Extension 
	Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLS)
	'create WorkSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws("A1").Value = "Hello"
	ws("A2").Value = "World"
	'save the file as .xls
	wb.SaveAs("sample.xls")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
VB   C#


5. Generate CSV File C#

Comma Separated Value (.csv) files also play a very important role in keeping data in different types of organizations. So, we also need to learn how to generate .csv files and insert data in them programmatically.

We can use the same process as above, but we need to specify the .csv extension with file name while saving. Let's see an example of how to create .csv files in our C# project:

using IronXL;
static void Main(string[] args)
{
    //create new WorkBook  
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "Hello";
    ws["A2"].Value = "World";
    //save the file as .csv
    wb.SaveAsCsv("sample.csv");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
using IronXL;
static void Main(string[] args)
{
    //create new WorkBook  
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "Hello";
    ws["A2"].Value = "World";
    //save the file as .csv
    wb.SaveAsCsv("sample.csv");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook  
	Dim wb As WorkBook = WorkBook.Create()
	'create WorkSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws("A1").Value = "Hello"
	ws("A2").Value = "World"
	'save the file as .csv
	wb.SaveAsCsv("sample.csv")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
VB   C#


6. Generate TSV File C#

Sometimes we need to generate Tab Separated Value (.tsv) files and insert data programmatically.

Using IronXL we can also generate .tsv extension files, insert data in it, and then save it to the required location.

Let's see the example that how to generate .tsv extension file:

using IronXL;
static void Main(string[] args)
{ 
    //create new WorkBook  
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "Hello";
    ws["A2"].Value = "World";
    //save the file as .tsv
    wb.SaveAs("sample.tsv");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
using IronXL;
static void Main(string[] args)
{ 
    //create new WorkBook  
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "Hello";
    ws["A2"].Value = "World";
    //save the file as .tsv
    wb.SaveAs("sample.tsv");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook  
	Dim wb As WorkBook = WorkBook.Create()
	'create WorkSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws("A1").Value = "Hello"
	ws("A2").Value = "World"
	'save the file as .tsv
	wb.SaveAs("sample.tsv")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
VB   C#


7. Generate JSON File C#

We can comfortably say that Javascript Object Notation (.json) files are the most common data files, and are used in almost all software development companies. Therefore, we often need to save the data in JSON format. For this, we need the simplest method to generate JSON format files and insert the data into it.

In such conditions, IronXL is the best option by which we can easily generate these files for C#. Let's see the example.

using IronXL;
static void Main(string[] args)
{
    //create new WorkBook 
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "1";
    ws["A2"].Value = "john";
    ws["B1"].Value = "2";
    ws["B2"].Value = "alex";
    ws["C1"].Value = "3";
    ws["C2"].Value = "stokes";
    //save the file as .json
    wb.SaveAsJson("sample.json");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
using IronXL;
static void Main(string[] args)
{
    //create new WorkBook 
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "1";
    ws["A2"].Value = "john";
    ws["B1"].Value = "2";
    ws["B2"].Value = "alex";
    ws["C1"].Value = "3";
    ws["C2"].Value = "stokes";
    //save the file as .json
    wb.SaveAsJson("sample.json");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook 
	Dim wb As WorkBook = WorkBook.Create()
	'create WorkSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws("A1").Value = "1"
	ws("A2").Value = "john"
	ws("B1").Value = "2"
	ws("B2").Value = "alex"
	ws("C1").Value = "3"
	ws("C2").Value = "stokes"
	'save the file as .json
	wb.SaveAsJson("sample.json")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
VB   C#

And review the screenshot of the newly created json file Sample.json :


8. Generate XML File C#

In business application development, we often need to save the data in the Extensible Markup Language (.xml) file format. This is important because .xml file data is readable by both humans and machines.

Through the following examples, we will learn how to generate .xml files for C# and insert data programmatically.

using IronXL;
static void Main(string[] args)
{
    //create new WorkBook 
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "Hello";
    ws["A2"].Value = "World";
    //save the file as .json
    wb.SaveAsXml("sample.xml");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
using IronXL;
static void Main(string[] args)
{
    //create new WorkBook 
    WorkBook wb = WorkBook.Create();
    //create WorkSheet
    WorkSheet ws = wb.CreateWorkSheet("Sheet1");
    //insert data in the cells of WorkSheet
    ws["A1"].Value = "Hello";
    ws["A2"].Value = "World";
    //save the file as .json
    wb.SaveAsXml("sample.xml");
    Console.WriteLine("successfully created.");
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'create new WorkBook 
	Dim wb As WorkBook = WorkBook.Create()
	'create WorkSheet
	Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1")
	'insert data in the cells of WorkSheet
	ws("A1").Value = "Hello"
	ws("A2").Value = "World"
	'save the file as .json
	wb.SaveAsXml("sample.xml")
	Console.WriteLine("successfully created.")
	Console.ReadKey()
End Sub
VB   C#

You can read more about converting excel spreadsheets and files programmatically for use in C# projects.


Tutorial Quick Access

IronXL Generator Documentation

Read the full documentation on how IronXL generates files in every Excel format necessary for your C# project.

IronXL Generator Documentation