Skip to footer content
USING IRONXL

How to Load An Excel File in C#

To load Excel files in C#, use the IronXL library's WorkBook.Load() method which accepts file paths, streams, or byte arrays, then access worksheets and read cell values programmatically without requiring Microsoft Office installation.

This article will demonstrate how to load Excel files using IronXL, a comprehensive Excel C# library, efficiently and programmatically. Whether you're building ASP.NET MVC applications that read Excel files or creating Windows Forms Excel readers, IronXL provides a simple yet powerful solution.

How to Load Excel Files

  1. Open Visual Studio and create a new Visual Studio project.
  2. Download the IronXL NuGet package library.
  3. Make an Excel workbook in XLSX file or CSV file format.
  4. Now load the Excel file using the IronXL Library.
  5. Process the Excel values using the cell range.

What Is IronXL and Why Should I Use It?

What is IronXL?

The C# .NET Excel library, IronXL, can be used to read data from and convert Microsoft Excel documents to CSV files. No Microsoft Office, Microsoft.Office.Interop.Excel, or Excel Interop installation is required for IronXL. It is a completely independent .NET Excel application library that works seamlessly with Docker containers and can be deployed to AWS Lambda functions or Azure cloud services. Numerous spreadsheet formats can be read by it.

Excel spreadsheets in a .NET context may be easily generated, modified, and viewed thanks to IronXL's straightforward C# API. Core .NET apps, Linux environments, macOS systems, Azure, and Xamarin are all fully supported by IronXL. The library also integrates smoothly with Blazor applications for reading Excel files and .NET MAUI projects. Below is a bulleted list of IronXL's key features that set it apart from other applications for reading Excel files.

What Are the Key Features That Make IronXL Stand Out?

Why Should I Choose IronXL Over Other Excel Libraries?

With these features, IronXL becomes a flexible and strong library for working with Excel files, giving developers more convenience and flexibility when managing their Excel document-related tasks programmatically. The library provides comprehensive API reference documentation and extensive tutorials to help developers get started quickly. To know more about the IronXL library, refer to those documents.

How Do I Set Up a .NET Project for Excel File Operations?

The IronXL library makes it easy to write data and create and read Excel files, as you will see in the upcoming sections. Before we begin, ensure you have properly configured your license key if you're using the full version, or you can start with a free trial. For web applications, you may need to set up the license key in your web.config file.

First, let's start a new project for working with Excel files:

How Do I Create a New Project in Visual Studio?

Choose "New Project" from the "File" menu once Visual Studio has launched.

The .NET "Console App" project templates will appear in the dialog box that follows; select them and double-click on "Next."

Visual Studio 'Add a new project' dialog showing various C# project templates including Console App, ASP.NET Core Web App, Blazer Server App, and ASP.NET Core Web API. New Project

What Project Settings Should I Configure?

You can type any project name you want for the Project after entering the new project's location in the Location area. For this tutorial, consider naming it something descriptive like "ExcelReaderApp" or "IronXLDemo". Press the Next button to continue.

Visual Studio new project configuration dialog showing options for creating a C# Console App with empty fields for project name 'ExcelReaderApp' and a typical project location path. Project Configuration

Which .NET Framework Version Should I Select?

With the Framework drop-down menu, you can choose a .NET Framework. The long-term supported version of .NET, 6.0, is being used here. IronXL supports multiple .NET versions, so you can choose based on your project requirements. Hit "Create" after that.

Visual Studio project configuration screen showing Console App settings with .NET 6.0 highlighted as the selected framework. The 'Do not use top-level statements' checkbox is clearly visible and unchecked. Target framework Selection

How Do I Install the IronXL Library?

The following solution requires the IronXL library, which you should obtain. To accomplish this, input the below command into the Package Manager Console:

Install-Package IronXL.Excel

Package Manager Console window in Visual Studio showing the successful installation of 'Install-Package IronXL.Excel' command with installation progress and confirmation messages displayed. IronXL Installation

An alternative to this would be to use the NuGet Package Manager to search for the package "IronXL".

Browsing should result in a list of all NuGet packages linked to IronXL. Then, select the one needed and click the install button.

NuGet Package Manager window in Visual Studio highlighting the IronXL.Excel package with version 2023.4.13. The 'Install' button is prominently highlighted with an arrow pointing to it. NuGet Package Manager

How Can I Read Excel Files Using IronXL?

The following code allows you to load and read Excel files using IronXL. This example demonstrates the basic approach to reading Excel files without Interop.

using IronXL;
using System.Linq;

// Load the workbook with the name "Demo.xlsx".
WorkBook workBook = WorkBook.Load("Demo.xlsx");

// Access the first worksheet in the workbook.
WorkSheet sheet = workBook.WorkSheets.First();

// Iterate through the range of cells A2 to B10 and print each cell's address and text content.
foreach (var cell in sheet["A2:B10"])
{
    Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
using IronXL;
using System.Linq;

// Load the workbook with the name "Demo.xlsx".
WorkBook workBook = WorkBook.Load("Demo.xlsx");

// Access the first worksheet in the workbook.
WorkSheet sheet = workBook.WorkSheets.First();

// Iterate through the range of cells A2 to B10 and print each cell's address and text content.
foreach (var cell in sheet["A2:B10"])
{
    Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
$vbLabelText   $csharpLabel

What Does Each Part of the Code Do?

The first line of code, using IronXL;, is called to implement the library into this demo project, which makes the IronXL functionalities available.

Then load the existing Excel file, named "Demo.xlsx", using the method Load which is available in the WorkBook object.

The Load method accepts three types of inputs: a file name with the path, a stream, and byte arrays. In the above example, the method with the file name and the path is used to load the file.

How Do I Access Specific Worksheets and Cell Ranges?

To perform the read Excel file operation, we obtain the default WorkSheet by using the First() method to select the initial worksheet from the workbook's worksheet collection. You can also manage worksheets by accessing them by name or index.

Subsequently, the Excel worksheet range is selected using the worksheet object by specifying the cell range. The select range functionality allows you to work with specific cells, entire rows, or columns. It will then iterate through each of the cell values within the specified range (A2:B10) and display the value along with the cell address.

What Output Should I Expect from This Code?

Excel spreadsheet showing sample data (Customer Name, Order Date, Product, etc.) with a C# console window below displaying the cell references and values being read by the IronXL code. Output of Previous Code Example

The above example demonstrates loading an Excel file in C# and its resulting output when using IronXL. It utilizes Excel sheets by referencing the row and column index.

How Can I Handle Different Excel File Formats?

IronXL supports various Excel file formats beyond the standard XLSX. You can read XLSX files, work with older XLS formats, and even read CSV files using the same API. Here's an example of loading different file types:

// Load an XLSX file
WorkBook xlsxWorkbook = WorkBook.Load("data.xlsx");

// Load a CSV file
WorkBook csvWorkbook = WorkBook.LoadCSV("data.csv", delimiter: ",");

// Load an XLS file (older Excel format)
WorkBook xlsWorkbook = WorkBook.Load("legacy.xls");

// Load from a stream instead of a file path
using (FileStream stream = new FileStream("data.xlsx", FileMode.Open))
{
    WorkBook streamWorkbook = WorkBook.Load(stream);
}
// Load an XLSX file
WorkBook xlsxWorkbook = WorkBook.Load("data.xlsx");

// Load a CSV file
WorkBook csvWorkbook = WorkBook.LoadCSV("data.csv", delimiter: ",");

// Load an XLS file (older Excel format)
WorkBook xlsWorkbook = WorkBook.Load("legacy.xls");

// Load from a stream instead of a file path
using (FileStream stream = new FileStream("data.xlsx", FileMode.Open))
{
    WorkBook streamWorkbook = WorkBook.Load(stream);
}
$vbLabelText   $csharpLabel

To know more about coding with IronXL, refer to this example.

What Are the Next Steps After Loading Excel Files?

IronXL is a well-liked Excel add-on that works without the need for any extra external libraries. It has several uses and doesn't need to have Microsoft Excel installed.

All programmable duties involving Microsoft Excel documents can be fully resolved with IronXL. You can add and remove data, search and replace, combine and unmerge data tables or cells, sort strings or numbers, visualize a data table, and save files. Formula computations are also possible with IronXL using the edit formulas feature. You can also set cell data types and examine spreadsheet data with it. The ability to read and write CSV files available with IronXL is a necessity for working with Excel data.

Advanced Features to Explore

Once you've mastered loading Excel files, consider exploring these advanced features:

Want to try it yourself?

While IronXL offers a free trial, users can choose to upgrade to a paid membership for 1 year to receive updates and customer assistance. IronXL charges extra for security that allows for unfettered distribution. To find out more about the specifics of the pricing, see this licensing page.

Frequently Asked Questions

How can I load an Excel file in C# without using Interop?

You can load an Excel file in C# without using Interop by utilizing the IronXL library. Start by installing the IronXL NuGet package in your project, then use the WorkBook.Load method to programmatically load your Excel file.

What are the steps to set up IronXL in a Visual Studio project?

To set up IronXL in a Visual Studio project, create a new project, install the IronXL NuGet package via the NuGet Package Manager, and then you can begin creating or loading Excel files using the library's API.

Can IronXL be used with .NET Core 6?

Yes, IronXL is fully compatible with .NET Core 6. You can seamlessly integrate it into your .NET Core projects to manage Excel files.

How does IronXL manage large Excel datasets?

IronXL is optimized for handling large datasets efficiently. Its multithreading capabilities allow it to process substantial amounts of data quickly without compromising performance.

Is it possible to read and convert Excel files with IronXL?

Yes, IronXL allows you to read data from Excel files and convert them to different formats such as CSV. This can be done programmatically using its C# API.

What file formats does IronXL support for Excel files?

IronXL supports a variety of Excel file formats including XLSX, CSV, XLS, XLST, TSV, and XLSM, making it versatile for different project requirements.

Can IronXL be used on different operating systems?

Yes, IronXL is cross-platform and works on Windows, macOS, and Linux, allowing developers to use the library in diverse environments.

How can I handle different data types in Excel using IronXL?

IronXL can manage various data types in Excel columns, such as text, numbers, dates, and formulas, providing a flexible solution for data manipulation.

What are the advantages of using IronXL for Excel tasks in C#?

IronXL offers several advantages including ease of use, compatibility with multiple platforms, support for a wide range of Excel formats, and no dependency on Microsoft Excel installations.

How can I try out IronXL for Excel file management?

You can try IronXL with a free trial. To access additional features, updates, and customer support, consider upgrading to a paid membership.

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he ...
Read More