How to Import Excel Files in C#

IronXL enables C# developers to import Excel data with just one line of code, supporting XLSX, CSV, and other formats without Interop dependencies, allowing immediate access to cells, ranges, and worksheets for data manipulation.

Quickstart: Instantly Load Your Excel File

With just one method call using IronXL's timeout-free API, you can load any supported Excel sheet (XLSX, CSV, etc.) in seconds—no Interop, no fuss. Begin interacting with the workbook immediately by accessing cells, ranges, or sheets as needed.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    WorkBook wb = IronXL.WorkBook.Load("path/to/data.xlsx");
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer

Import Excel Data C#

  • Import Data with the IronXL Library
  • Import Excel data in C#
  • Import data of specific cell range
  • Import Excel data with aggregate functions SUM, AVG, MIN, MAX, and more
How To Work related to How to Import Excel Files in C#

Step 1

Installing the IronXL Library

IronXL Makes Excel Import Easier

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. IronXL provides a comprehensive C# Excel API that simplifies working with Excel files without requiring Microsoft Office or Interop dependencies. This makes it ideal for server environments and cloud deployments.

Installation Methods

Install into your C# Project via DLL Download or navigate using the NuGet package. For detailed installation guidance, check out our getting started overview.

Install-Package IronXL.Excel

How to Tutorial

Accessing a WorkSheet in Your Project

Basic WorkBook Loading Process

For our project needs today, we'll import Excel data into our C# application using the IronXL software installed in step 1. The library supports various Excel formats and provides intuitive methods for loading spreadsheets.

For step 2, we'll 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:

// 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")
$vbLabelText   $csharpLabel

The Excel file at the specified path will be loaded into wb. This method supports XLSX, XLS, CSV, TSV, and other common spreadsheet formats.

Accessing a Specific WorkSheet

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, passing the sheet name as a string parameter to specify which sheet of the WorkBook to import. Learn more about managing worksheets in our comprehensive guide.

// 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")
$vbLabelText   $csharpLabel

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

Alternative Methods to Access WorkSheets

The following alternative ways exist to import an Excel WorkSheet into the project. Each method provides flexibility depending on your specific use case:

// Import WorkSheet by various methods

// by sheet indexing
WorkSheet mySheet = wb.WorkSheets[SheetIndex];

// get default WorkSheet
WorkSheet defaultSheet = wb.DefaultWorkSheet;

// get first WorkSheet
WorkSheet firstSheet = wb.WorkSheets.First();

// for the first or default sheet
WorkSheet firstOrDefaultSheet = wb.WorkSheets.FirstOrDefault();
// Import WorkSheet by various methods

// by sheet indexing
WorkSheet mySheet = wb.WorkSheets[SheetIndex];

// get default WorkSheet
WorkSheet defaultSheet = wb.DefaultWorkSheet;

// get first WorkSheet
WorkSheet firstSheet = wb.WorkSheets.First();

// for the first or default sheet
WorkSheet firstOrDefaultSheet = wb.WorkSheets.FirstOrDefault();
' Import WorkSheet by various methods

' by sheet indexing
Dim mySheet As WorkSheet = wb.WorkSheets(SheetIndex)

' get default WorkSheet
Dim defaultSheet As WorkSheet = wb.DefaultWorkSheet

' get first WorkSheet
Dim firstSheet As WorkSheet = wb.WorkSheets.First()

' for the first or default sheet
Dim firstOrDefaultSheet As WorkSheet = wb.WorkSheets.FirstOrDefault()
$vbLabelText   $csharpLabel

Now, we can easily import any type of data from the specified Excel files. Let's explore all possible aspects for importing Excel file data in our project.


Importing Excel Data in C#

Basic Cell Import Method

This is the basic aspect of importing Excel file data into our project. IronXL provides multiple ways to access cell data, making it flexible for different scenarios.

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

var cellValue = ws["Cell Address"];
var cellValue = ws["Cell Address"];
Dim cellValue = ws("Cell Address")
$vbLabelText   $csharpLabel

Importing Data Using Row and Column Indexes

We can also import cell data from Excel files by using row and column indexes. This line of code returns the value of the specified row and column index. This approach is particularly useful when iterating through data programmatically:

var cellValueByIndex = ws.Rows[RowIndex].Columns[ColumnIndex];
var cellValueByIndex = ws.Rows[RowIndex].Columns[ColumnIndex];
Dim cellValueByIndex = ws.Rows(RowIndex).Columns(ColumnIndex)
$vbLabelText   $csharpLabel

Storing Imported Values in Variables

To assign imported cell values to variables, use this code. The ToString() method ensures compatibility with string variables, but you can also cast to other types as needed:

// Import Data by Cell Address
// by cell addressing
string val = ws["Cell Address"].ToString();

// by row and column indexing
string valWithIndexing = ws.Rows[RowIndex].Columns[ColumnIndex].Value.ToString();

// for numeric values
decimal numericValue = ws["B2"].DecimalValue;

// for date values
DateTime dateValue = ws["C2"].DateTimeValue;
// Import Data by Cell Address
// by cell addressing
string val = ws["Cell Address"].ToString();

// by row and column indexing
string valWithIndexing = ws.Rows[RowIndex].Columns[ColumnIndex].Value.ToString();

// for numeric values
decimal numericValue = ws["B2"].DecimalValue;

// for date values
DateTime dateValue = ws["C2"].DateTimeValue;
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

In the above examples, the row and column index starts at 0. For more advanced cell operations, explore our guide on clearing cells and copying cells.


Importing Data from a Specific Range

Range Function Syntax

To import data in a specific range from an Excel WorkBook, use the range function. Define the range by describing the starting and ending cell addresses. This returns all cell values within the specified range. For comprehensive range selection techniques, see our select range guide.

var rangeData = ws["Starting Cell Address:Ending Cell Address"];
var rangeData = ws["Starting Cell Address:Ending Cell Address"];
Dim rangeData = ws("Starting Cell Address:Ending Cell Address")
$vbLabelText   $csharpLabel

Complete Range Import Example

For more information about working with range in Excel files and learn more about pulling data in different methods. The following example demonstrates importing both individual cell values and ranges:

:path=/static-assets/excel/content-code-examples/how-to/csharp-import-excel-import.cs
using IronXL;
using System;

// 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();
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

The above code displays the following output:

Console output showing Excel range B3:B9 import results with Midmarket value and country list including Germany, Mexico, Canada

With the values of Excel file sample.xlsx as:

Excel spreadsheet with sales data showing blue highlighted cells for range selection during import process

Importing Excel Data Using Aggregate Functions

Available Aggregate Functions

Apply aggregate functions to Excel files and import the resulting data from these functions. IronXL provides built-in math functions that make data analysis straightforward. Here are examples of different functions and their usage:

  • Sum()

    // To find the sum of a specific cell range
    var sum = ws["Starting Cell Address:Ending Cell Address"].Sum();
    // To find the sum of a specific cell range
    var sum = ws["Starting Cell Address:Ending Cell Address"].Sum();
    ' To find the sum of a specific cell range
    Dim sum = ws("Starting Cell Address:Ending Cell Address").Sum()
    $vbLabelText   $csharpLabel
  • Average()

    // To find the average of a specific cell range
    var average = ws["Starting Cell Address:Ending Cell Address"].Avg();
    // To find the average of a specific cell range
    var average = ws["Starting Cell Address:Ending Cell Address"].Avg();
    ' To find the average of a specific cell range
    Dim average = ws("Starting Cell Address:Ending Cell Address").Avg()
    $vbLabelText   $csharpLabel
  • Min()

    // To find the minimum in a specific cell range
    var minimum = ws["Starting Cell Address:Ending Cell Address"].Min();
    // To find the minimum in a specific cell range
    var minimum = ws["Starting Cell Address:Ending Cell Address"].Min();
    ' To find the minimum in a specific cell range
    Dim minimum = ws("Starting Cell Address:Ending Cell Address").Min()
    $vbLabelText   $csharpLabel
  • Max()

    // To find the maximum in a specific cell range
    var maximum = ws["Starting Cell Address:Ending Cell Address"].Max();
    // To find the maximum in a specific cell range
    var maximum = ws["Starting Cell Address:Ending Cell Address"].Max();
    ' To find the maximum in a specific cell range
    Dim maximum = ws("Starting Cell Address:Ending Cell Address").Max()
    $vbLabelText   $csharpLabel

Using Multiple Aggregate Functions Together

Read more about working with aggregate functions in Excel for C# and learn more about pulling data in different methods. These functions are particularly useful for generating summary statistics or validating imported data.

See an example of importing Excel file data by applying these functions:

:path=/static-assets/excel/content-code-examples/how-to/csharp-import-excel-math-functions.cs
using IronXL;
using System;

// 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();
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

The above code gives us this output:

Console output showing Excel aggregate functions: Sum=452, Avg=56.5, Min=5, Max=350 for range D2 to D9

And our file sample.xlsx contains these values:

Excel spreadsheet with sales data showing highlighted Sale Price column containing values for aggregate function analysis

Importing Complete Excel File Data

The ToDataSet Method

To import complete Excel file data into a C# project, first parse the loaded WorkBook into a DataSet. This way, the complete Excel data imports into the DataSet, and WorkSheets in Excel files become DataTables within that DataSet. This approach is particularly useful for database operations or when working with data-bound controls. Learn more about importing and exporting as DataSet.

// Import WorkBook into DataSet
DataSet ds = wb.ToDataSet();
// Import WorkBook into DataSet
DataSet ds = wb.ToDataSet();
' Import WorkBook into DataSet
Dim ds As DataSet = wb.ToDataSet()
$vbLabelText   $csharpLabel

This imports our specified WorkSheet into a DataSet for use according to requirements. This method is especially powerful when working with multiple sheets simultaneously or when integrating Excel data with ADO.NET operations.

Handling Column Headers

Often, the first row of an Excel file serves as column names. In this case, make the first row DataTable column names. Set the boolean parameter of the ToDataSet() function of IronXL as follows:

// Import WorkBook into DataSet with first row as ColumnNames
DataSet ds = wb.ToDataSet(true);
// Import WorkBook into DataSet with first row as ColumnNames
DataSet ds = wb.ToDataSet(true);
' Import WorkBook into DataSet with first row as ColumnNames
Dim ds As DataSet = wb.ToDataSet(True)
$vbLabelText   $csharpLabel

This makes the first row of the Excel file DataTable column names, which is essential for maintaining data structure integrity when working with structured Excel data.

Complete DataSet Import Example

See a complete example of importing Excel data into a DataSet and using the first row of an Excel WorkSheet as DataTable column names:

:path=/static-assets/excel/content-code-examples/how-to/csharp-import-excel-dataset.cs
using IronXL;
using System;
using System.Data;

WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");

// Import Excel data into a DataSet
DataSet ds = wb.ToDataSet(true);

Console.WriteLine("Excel file data imported to dataset successfully.");
Console.ReadKey();
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Working with Excel Dataset and DataTable functions can be complex, but we have more examples available for incorporating file data into your C# project. For advanced scenarios, consider exploring our guides on Excel to SQL via DataTable and loading Excel from SQL database.


Library Quick Access

Explore the IronXL Reference

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

Explore the IronXL Reference
Documentation related to Library Quick Access

Frequently Asked Questions

How do I import an Excel file in C# without using Microsoft Office?

You can import Excel files in C# using IronXL, which doesn't require Microsoft Office or Interop dependencies. Simply use the WorkBook.Load() method with your file path, like: WorkBook wb = WorkBook.Load("path/to/data.xlsx"). This works with XLSX, XLS, CSV, TSV and other formats.

What Excel file formats can I import with this C# library?

IronXL supports importing various Excel formats including XLSX, XLS, CSV, TSV, and other common spreadsheet formats. The same WorkBook.Load() method handles all these formats automatically.

Is it possible to import Excel data on servers or cloud environments?

Yes, IronXL is ideal for server environments and cloud deployments because it doesn't require Microsoft Office or Interop dependencies. This makes it perfect for web applications, Azure functions, and other server-side scenarios.

How quickly can I start working with Excel data after importing?

With IronXL's timeout-free API, you can load any supported Excel sheet in seconds with just one line of code. After using WorkBook.Load(), you can immediately begin interacting with cells, ranges, or worksheets.

Can I import specific cell ranges from Excel files?

Yes, IronXL allows you to import data from specific cell ranges after loading the workbook. You can access individual cells, ranges, or entire worksheets using the intuitive API provided.

How do I install the Excel import library for C#?

You can install IronXL through NuGet Package Manager or by downloading the DLL directly. The library is available free for development and provides comprehensive documentation for getting started.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More
Ready to Get Started?
Nuget Downloads 1,765,830 | Version: 2025.12 just released