C# Import Excel

As developers, we often need to import data from Excel files and use it to fulfill our application and data management requirements. Without requiring many lines of code, IronXL gives us an easy way to import exactly the data we need directly into a C# project and then manipulate it programmatically.

Import WorkSheet
//by sheet indexing
WorkBook.WorkSheets[SheetIndex];
//get default  WorkSheet
WorkBook.DefaultWorkSheet;
//get first WorkSheet
WorkBook.WorkSheets.First();
//for the first or default sheet:
WorkBook.WorkSheets.FirstOrDefault();
'by sheet indexing
WorkBook.WorkSheets(SheetIndex)
'get default  WorkSheet
WorkBook.DefaultWorkSheet
'get first WorkSheet
WorkBook.WorkSheets.First()
'for the first or default sheet:
WorkBook.WorkSheets.FirstOrDefault()
Jump to Article
Import Data by Cell Address
//by cell addressing
string val = WorkSheet["Cell Address"].ToString();
//by row and column indexing
string val = WorkSheet.Rows[RowIndex].Columns[ColumnIndex].Value.ToString();
'by cell addressing
Dim val As String = WorkSheet("Cell Address").ToString()
'by row and column indexing
Dim val As String = WorkSheet.Rows(RowIndex).Columns(ColumnIndex).Value.ToString()
Jump to Article
Import Data by Range
using IronXL;
static void Main(string[] args)
{
    //import Excel WorkBook
    WorkBook wb = WorkBook.Load("sample.xlsx");
    //specify WorkSheet
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //import data of specific cell
    string val = ws["A4"].Value.ToString();
    Console.WriteLine("Import Value of A4 Cell address: {0}",val);
    Console.WriteLine("import Values in Range From B3 To B9 :\n");
    //import data in specific range
    foreach (var item in ws["B3:B9"])
    {
        Console.WriteLine(item.Value.ToString());
    }
 
    Console.ReadKey();
}
Imports Microsoft.VisualBasic
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'import Excel WorkBook
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	'specify WorkSheet
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	'import data of specific cell
	Dim val As String = ws("A4").Value.ToString()
	Console.WriteLine("Import Value of A4 Cell address: {0}",val)
	Console.WriteLine("import Values in Range From B3 To B9 :" & vbLf)
	'import data in specific range
	For Each item In ws("B3:B9")
		Console.WriteLine(item.Value.ToString())
	Next item

	Console.ReadKey()
End Sub
Jump to Article
Import Data by Aggregate Function
using IronXL;
static void Main(string[] args)
{
    //Import Excel file
    WorkBook wb = WorkBook.Load("sample.xlsx");
    //Specify WorkSheet
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //Import Excel file data by applying aggregate functions
    decimal Sum = ws["D2:D9"].Sum();
    decimal Avg = ws["D2:D9"].Avg();
    decimal Min = ws["D2:D9"].Min();
    decimal Max = ws["D2:D9"].Max();
    Console.WriteLine("Sum From D2 To D9: {0}", Sum);
    Console.WriteLine("Avg From D2 To D9: {0}", Avg);
    Console.WriteLine("Min From D2 To D9: {0}", Min);
    Console.WriteLine("Max From D2 To D9: {0}", Max);
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'Import Excel file
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	'Specify WorkSheet
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	'Import Excel file data by applying aggregate functions
	Dim Sum As Decimal = ws("D2:D9").Sum()
	Dim Avg As Decimal = ws("D2:D9").Avg()
	Dim Min As Decimal = ws("D2:D9").Min()
	Dim Max As Decimal = ws("D2:D9").Max()
	Console.WriteLine("Sum From D2 To D9: {0}", Sum)
	Console.WriteLine("Avg From D2 To D9: {0}", Avg)
	Console.WriteLine("Min From D2 To D9: {0}", Min)
	Console.WriteLine("Max From D2 To D9: {0}", Max)
	Console.ReadKey()
End Sub
Jump to Article
Import to Dataset
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    DataSet ds = new DataSet();
    ds = wb.ToDataSet(true);
    Console.WriteLine("Excel file data imported to dataset successfully.");
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	Dim ds As New DataSet()
	ds = wb.ToDataSet(True)
	Console.WriteLine("Excel file data imported to dataset successfully.")
	Console.ReadKey()
End Sub
Jump to Article
Try IronXL free for development


Step 1

1. Import Data with the IronXL Library

Import data using the functions provided by the IronXL Excel library, which we'll be using in this tutorial. The software is available free for development.

Install into your C# Project via DLL Download or navigate using NuGet the package.


 PM > Install-Package IronXL.Excel


How to Tutorial

2. Access WorkSheet for Project

For our project needs today, we will be importing Excel data into our C# application, using the IronXL software installed in step 1.

For step 2, we will load our Excel WorkBook in our CSharp project by using the WorkBook.Load() function of IronXL. We pass the path of the Excel WorkBook as a string parameter in this function, like this:

//load Excel file
WorkBook wb = WorkBook.Load("Path");
//load Excel file
WorkBook wb = WorkBook.Load("Path");
'load Excel file
Dim wb As WorkBook = WorkBook.Load("Path")
VB   C#

The Excel file of the specified path will be loaded in wb.

Next, we need to access a specific WorkSheet of the Excel file whose data will be imported into the project. For this purpose, we can use the GetWorkSheet() function of IronXL. We will pass sheet name as a string parameter in this function to specify which sheet of WorkBook to be imported.

//specify sheet name of Excel WorkBook
WorkSheet ws = wb.GetWorkSheet("SheetName");
//specify sheet name of Excel WorkBook
WorkSheet ws = wb.GetWorkSheet("SheetName");
'specify sheet name of Excel WorkBook
Dim ws As WorkSheet = wb.GetWorkSheet("SheetName")
VB   C#

The WorkSheet will import as ws, and wb is the WorkBook which we have defined in the above code sample.

There are also the following alternative ways to import an Excel WorkSheet into the project.

//by sheet indexing
WorkBook.WorkSheets[SheetIndex];
//get default  WorkSheet
WorkBook.DefaultWorkSheet;
//get first WorkSheet
WorkBook.WorkSheets.First();
//for the first or default sheet:
WorkBook.WorkSheets.FirstOrDefault();
//by sheet indexing
WorkBook.WorkSheets[SheetIndex];
//get default  WorkSheet
WorkBook.DefaultWorkSheet;
//get first WorkSheet
WorkBook.WorkSheets.First();
//for the first or default sheet:
WorkBook.WorkSheets.FirstOrDefault();
'by sheet indexing
WorkBook.WorkSheets(SheetIndex)
'get default  WorkSheet
WorkBook.DefaultWorkSheet
'get first WorkSheet
WorkBook.WorkSheets.First()
'for the first or default sheet:
WorkBook.WorkSheets.FirstOrDefault()
VB   C#

Now, we can easily import any type of data from the specified Excel files. Let's see all the possible aspects which we use to import Excel file data in our project.


3. Import Excel Data in C#

This is the basic aspect of importing Excel file data in our project.

For this purpose, we can use a cell addressing system to specify which cell data we need to import. It will return the value of a specific cell address of the Excel file.

WorkSheet["Cell Address"];
WorkSheet["Cell Address"];
WorkSheet("Cell Address")
VB   C#

We can also import cell data from Excel files by using the row and column index. This line of code will return the value of specified row and column index.

WorkSheet.Rows[RowIndex].Columns[ColumnIndex]
WorkSheet.Rows[RowIndex].Columns[ColumnIndex]
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'WorkSheet.Rows[RowIndex].Columns[ColumnIndex]
VB   C#

If we want to assign imported cell values into variables, then we can use this code.

//by cell addressing
string val = WorkSheet["Cell Address"].ToString();
//by row and column indexing
string val = WorkSheet.Rows[RowIndex].Columns[ColumnIndex].Value.ToString();
//by cell addressing
string val = WorkSheet["Cell Address"].ToString();
//by row and column indexing
string val = WorkSheet.Rows[RowIndex].Columns[ColumnIndex].Value.ToString();
'by cell addressing
Dim val As String = WorkSheet("Cell Address").ToString()
'by row and column indexing
Dim val As String = WorkSheet.Rows(RowIndex).Columns(ColumnIndex).Value.ToString()
VB   C#

In the above examples, the row and column index starts at 0.


4. Import Excel Data of Specific Range

If we want to import data in a specific range from an Excel WorkBook, it can easily be done by using the range function. To define the range, we need to describe the starting cell and ending cell address. In this way, it will return all the cell values which lie in the specified range.

WorkSheet["starting Cell Address : Ending Cell Address"];
WorkSheet["starting Cell Address : Ending Cell Address"];
WorkSheet("starting Cell Address : Ending Cell Address")
VB   C#

For more about working with range in Excel files check out the code examples provided.

using IronXL;
static void Main(string[] args)
{
    //import Excel WorkBook
    WorkBook wb = WorkBook.Load("sample.xlsx");
    //specify WorkSheet
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //import data of specific cell
    string val = ws["A4"].Value.ToString();
    Console.WriteLine("Import Value of A4 Cell address: {0}",val);
    Console.WriteLine("import Values in Range From B3 To B9 :\n");
    //import data in specific range
    foreach (var item in ws["B3:B9"])
    {
        Console.WriteLine(item.Value.ToString());
    }

    Console.ReadKey();
}
using IronXL;
static void Main(string[] args)
{
    //import Excel WorkBook
    WorkBook wb = WorkBook.Load("sample.xlsx");
    //specify WorkSheet
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //import data of specific cell
    string val = ws["A4"].Value.ToString();
    Console.WriteLine("Import Value of A4 Cell address: {0}",val);
    Console.WriteLine("import Values in Range From B3 To B9 :\n");
    //import data in specific range
    foreach (var item in ws["B3:B9"])
    {
        Console.WriteLine(item.Value.ToString());
    }

    Console.ReadKey();
}
Imports Microsoft.VisualBasic
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'import Excel WorkBook
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	'specify WorkSheet
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	'import data of specific cell
	Dim val As String = ws("A4").Value.ToString()
	Console.WriteLine("Import Value of A4 Cell address: {0}",val)
	Console.WriteLine("import Values in Range From B3 To B9 :" & vbLf)
	'import data in specific range
	For Each item In ws("B3:B9")
		Console.WriteLine(item.Value.ToString())
	Next item

	Console.ReadKey()
End Sub
VB   C#

The above code displays the following output:

With the values of Excel file sample.xlsx as:


5. Import Excel Data by Aggregate Functions

We can also apply aggregate functions to Excel files and import the resulting data of these aggregate functions. Here are some examples of the different functions and how to use them.

  • Sum()

    //to find the sum of specific cell range 
    WorkSheet["Starting Cell Address : Ending Cell Address"].Sum();
    //to find the sum of specific cell range 
    WorkSheet["Starting Cell Address : Ending Cell Address"].Sum();
    'to find the sum of specific cell range 
    WorkSheet("Starting Cell Address : Ending Cell Address").Sum()
    VB   C#
  • Average()

    //to find the average of specific cell range 
    WorkSheet["Starting Cell Address : Ending Cell Address"].Avg()
    //to find the average of specific cell range 
    WorkSheet["Starting Cell Address : Ending Cell Address"].Avg()
    'to find the average of specific cell range 
    'INSTANT VB TODO TASK: The following line uses invalid syntax:
    'WorkSheet["Starting Cell Address : Ending Cell Address"].Avg()
    VB   C#
  • Min()

    //to find the Min In specific cell range 
    WorkSheet["Starting Cell Address : Ending Cell Address"].Min()
    //to find the Min In specific cell range 
    WorkSheet["Starting Cell Address : Ending Cell Address"].Min()
    'to find the Min In specific cell range 
    'INSTANT VB TODO TASK: The following line uses invalid syntax:
    'WorkSheet["Starting Cell Address : Ending Cell Address"].Min()
    VB   C#
  • Max()
    //to find the Max in specific cell range 
    WorkSheet["Starting Cell Address : Ending Cell Address"].Max()
    //to find the Max in specific cell range 
    WorkSheet["Starting Cell Address : Ending Cell Address"].Max()
    'to find the Max in specific cell range 
    'INSTANT VB TODO TASK: The following line uses invalid syntax:
    'WorkSheet["Starting Cell Address : Ending Cell Address"].Max()
    VB   C#

You can read more about working with aggregate functions in Excel for C# and learn more about pulling data in different methods.

Let's see an example of how to import Excel file data by applying these functions.

using IronXL;
static void Main(string[] args)
{
    //Import Excel file
    WorkBook wb = WorkBook.Load("sample.xlsx");
    //Specify WorkSheet
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //Import Excel file data by applying aggregate functions
    decimal Sum = ws["D2:D9"].Sum();
    decimal Avg = ws["D2:D9"].Avg();
    decimal Min = ws["D2:D9"].Min();
    decimal Max = ws["D2:D9"].Max();
    Console.WriteLine("Sum From D2 To D9: {0}", Sum);
    Console.WriteLine("Avg From D2 To D9: {0}", Avg);
    Console.WriteLine("Min From D2 To D9: {0}", Min);
    Console.WriteLine("Max From D2 To D9: {0}", Max);
    Console.ReadKey();
}
using IronXL;
static void Main(string[] args)
{
    //Import Excel file
    WorkBook wb = WorkBook.Load("sample.xlsx");
    //Specify WorkSheet
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    //Import Excel file data by applying aggregate functions
    decimal Sum = ws["D2:D9"].Sum();
    decimal Avg = ws["D2:D9"].Avg();
    decimal Min = ws["D2:D9"].Min();
    decimal Max = ws["D2:D9"].Max();
    Console.WriteLine("Sum From D2 To D9: {0}", Sum);
    Console.WriteLine("Avg From D2 To D9: {0}", Avg);
    Console.WriteLine("Min From D2 To D9: {0}", Min);
    Console.WriteLine("Max From D2 To D9: {0}", Max);
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	'Import Excel file
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	'Specify WorkSheet
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	'Import Excel file data by applying aggregate functions
	Dim Sum As Decimal = ws("D2:D9").Sum()
	Dim Avg As Decimal = ws("D2:D9").Avg()
	Dim Min As Decimal = ws("D2:D9").Min()
	Dim Max As Decimal = ws("D2:D9").Max()
	Console.WriteLine("Sum From D2 To D9: {0}", Sum)
	Console.WriteLine("Avg From D2 To D9: {0}", Avg)
	Console.WriteLine("Min From D2 To D9: {0}", Min)
	Console.WriteLine("Max From D2 To D9: {0}", Max)
	Console.ReadKey()
End Sub
VB   C#

The above code gives us this output:

And our file sample.xlsx will have these values:


6. Import Complete Excel File Data

If we want to import complete Excel file data in our CSharp project, then we can first parse our loaded WorkBook into a DataSet. In this way, complete Excel data would be imported into the DataSet, and WorkSheets on Excel files become DataTables of that DataSet. Here it is in action:

//import WorkBook into dataset
DataSet ds = WorkBook.ToDataSet();
//import WorkBook into dataset
DataSet ds = WorkBook.ToDataSet();
'import WorkBook into dataset
Dim ds As DataSet = WorkBook.ToDataSet()
VB   C#

In this way, our specified WorkSheet will be imported into a DataSet that we can use according to our requirements.

Often, the first column of an Excel file is used as ColumnName. In this case, we need to make the first column as DataTable ColumnName. To do this, we set the boolean parameter of ToDataSet() function of IronXL as follows:

ToDataSet(true);
ToDataSet(true);
ToDataSet(True)
VB   C#

This will make the first column of Excel file as a DataTable ColumnName.

Let's see a complete example of how to import Excel data into a Dataset, and use the first column of an Excel WorkSheets as DataTable ColumnName:

using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    DataSet ds = new DataSet();
    ds = wb.ToDataSet(true);
    Console.WriteLine("Excel file data imported to dataset successfully.");
    Console.ReadKey();
}
using IronXL;
static void Main(string[] args)
{
    WorkBook wb = WorkBook.Load("sample.xlsx");
    WorkSheet ws = wb.GetWorkSheet("Sheet1");
    DataSet ds = new DataSet();
    ds = wb.ToDataSet(true);
    Console.WriteLine("Excel file data imported to dataset successfully.");
    Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
	Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
	Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
	Dim ds As New DataSet()
	ds = wb.ToDataSet(True)
	Console.WriteLine("Excel file data imported to dataset successfully.")
	Console.ReadKey()
End Sub
VB   C#

Working with Excel Dataset and Datatable functions can be complicated, but we have more examples available for incorporating file data into your C# project.


Library Quick Access

Explore the IronXL Reference

Learn more about pulling Excel data via cells, range, datasets and datables in our full documentation API Reference for IronXl.

Explore the IronXL Reference