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.
Get started making PDFs with NuGet now:
Install IronXL with NuGet Package Manager
Copy and run this code snippet.
WorkBook wb = IronXL.WorkBook.Load("path/to/data.xlsx");Deploy to test on your live environment
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
Minimal Workflow (5 steps)
- Download and install the C# library to import Excel files
- Prepare the Excel files to be imported
- Use the
Loadmethod to import spreadsheet - Edit the loaded Excel file using intuitive APIs
- Export the edited Excel file in various types
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")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")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()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")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)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.comIn 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")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.csusing 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.comThe above code displays the following output:
With the values of Excel file sample.xlsx as:
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 $csharpLabelAverage()// 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 $csharpLabelMin()// 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 $csharpLabelMax()// 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.csusing 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.comThe above code gives us this output:
And our file sample.xlsx contains these values:
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()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)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.csusing 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.comWorking 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 ReferenceFrequently 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.










