USING IRONXL

How to Open Excel File in C#

Updated February 6, 2024
Share:

This tutorial will use IronXL as a main tool to open and read Excel files in C# projects.

IronXL Excel Library

IronXL is a .NET library that prioritizes ease of use, accuracy and speed for its users. It helps you open, read, create, and edit Excel files with lightning-fast performance and without any errors. It works without MS Office Interop, which makes it a powerful tool for developers.

IronXL is compatible with all .NET Frameworks along with Linux, MacOS, Docker, Azure and AWS. It can be used to create Console, Web and Desktop Applications such as Blazor and MAUI for modern Web Apps. It supports different workbook formats like XLS and XLSX files, XSLT and XLSM, CSV and TSV.

Some Important IronXL Features

  • Open, read and search data in different formats (XLS/XLSX file/CSV/TSV).
  • Export Excel Worksheets to XLS/XLSX/CSV/TSV/JSON.
  • Encrypting and decrypting XLSX/XLSM/XLTX files with passwords.
  • Work with Excel sheets as System.Data.DataSet and System.Data.DataTable objects.
  • Excel file formulas are recalculated automatically each time a sheet is edited.
  • Easy spreadsheet data editing with an intuitive cell-range-based syntax (e.g., WorkSheet["A1:B10"]).
  • Sort Cell Ranges, Columns and Rows.
  • Styling Cells - Font, Font Size, Background color, Border, Alignment and Numbering formats.

How to open an Excel file in C#?

Prerequisites

To use IronXL in C# applications, those components are needed to install on your local computer:

  1. Visual Studio - It is the official IDE for developing C# .NET applications. You can download and install Visual Studio from the Microsoft website. You can also use Jetbrains ReSharper & Rider.
  2. IronXL - It is the Excel library that helps to work with Excel sheets at a given path in C#. It must be installed in your C# application before using it. It can be downloaded from the NuGet website or from Manage NuGet packages in Visual Studio tools. You can also download the .NET Excel DLL file directly.

Adding Necessary Namespaces

Once Visual Studio and IronXL are installed, IronXL namepsaces are needed to reference in the source code. Add the following line of code on top of the file within a new project where IronXL functions will be used:

//add reference
using IronXL;
//add reference
using IronXL;
'add reference
Imports IronXL
VB   C#

Open an Existing Excel file in C#

Excel files are also known as workbooks. Each workbook contains multiple worksheets, and each worksheet contains cell values. To open and read an Excel file, it should be loaded using the WorkBook class's Load method.

//Supported Excel spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workbook = WorkBook.Load("test.xlsx");
//Supported Excel spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workbook = WorkBook.Load("test.xlsx");
'Supported Excel spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Dim workbook As WorkBook = WorkBook.Load("test.xlsx")
VB   C#

This opens the Excel file in WorkBook instance reference variable. It can have multiple worksheets, so it can be used to open a specific WorkSheet or all at once. The following code opens the first WorkSheet in the sheet instance variable:

WorkSheet sheet = workbook.WorkSheets.First();
WorkSheet sheet = workbook.WorkSheets.First();
Dim sheet As WorkSheet = workbook.WorkSheets.First()
VB   C#

This will open the first sheet in an Excel file and now data can be read and written to this sheet.

The Excel file opened here is:

How to Open Excel File in C#, Figure 1: Excel File Excel File

Read Excel Files in C#

Once the Excel file is opened, it is ready for reading data. Reading data from Excel files in C# using IronXL is very easy and simple. You can read cell values by simply mentioning the cell reference number.

The following code retrieves the value of a cell from the file:

//Select cells easily in Excel-notation and return the value
int cellValue = sheet ["C2"].IntValue;

//Display the value
Console.WriteLine(cellValue);
//Select cells easily in Excel-notation and return the value
int cellValue = sheet ["C2"].IntValue;

//Display the value
Console.WriteLine(cellValue);
'Select cells easily in Excel-notation and return the value
Dim cellValue As Integer = sheet ("C2").IntValue

'Display the value
Console.WriteLine(cellValue)
VB   C#

The output is as follows:

How to Open Excel File in C#, Figure 2: Read Excel Read Excel

Now, let's read data from a range of cells in the opened Excel file. The code goes as follows:

// Read from Range of cells elegantly.
foreach (var cell in sheet ["A2:A6"])
{
    Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
// Read from Range of cells elegantly.
foreach (var cell in sheet ["A2:A6"])
{
    Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
' Read from Range of cells elegantly.
For Each cell In sheet ("A2:A6")
	Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)
Next cell
VB   C#

The value within the cell within cell range A2 - A6 is accessed using a for loop. Each value is printed to the console.

How to Open Excel File in C#, Figure 3: Read Range of Cells Read Range of Cells

For a detailed work of reading and writing to cell values, check the read Excel file in another C# example tutorial.

Create a New Workbook

IronXL also helps in creating new workbooks, which can later be used for data saving and retrieving.

Just like loading the Excel files, creating Excel files is also very easy and can be done using only one line of code.

WorkBook workBook = new WorkBook(ExcelFileFormat.XLSX);
WorkBook workBook = new WorkBook(ExcelFileFormat.XLSX);
Dim workBook As New WorkBook(ExcelFileFormat.XLSX)
VB   C#

Let's create a worksheet and add some data to it for later use.

Create a New Worksheet

WorkSheet workSheet = workBook.CreateWorkSheet("GDPByCountry");
WorkSheet workSheet = workBook.CreateWorkSheet("GDPByCountry");
Dim workSheet As WorkSheet = workBook.CreateWorkSheet("GDPByCountry")
VB   C#

The line of code shown above creates a worksheet named "GDPByCountry" in the currently loaded workbook and now can add cell values into the current WorkSheet.

The following code adds a value to A1 cell:

workSheet ["A1"].Value = "Example";
workSheet ["A1"].Value = "Example";
workSheet ("A1").Value = "Example"
VB   C#

The final output is as follows:

How to Open Excel File in C#, Figure 4: Add Value to Cell Add Value to Cell

Summary

This article demonstrated how to open and read Excel files such as XLS, XLSX etc in C# using IronXL. IronXL does not require Microsoft Excel to be installed on the system to perform all Excel-related tasks.

IronXL provides a solution for all Excel-related tasks to be done programmatically whether it be formula calculation, string sorting, trimming, finding and replacing, merging and unmerging, saving files etc. You can also set cell data formats.

IronXL is available for a free 30-day trial and can be licensed for commercial use. IronXL's Lite package starts from $749.

< PREVIOUS
How to Read Excel File in C#
NEXT >
How to Convert a Table to a Range in Excel

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 1,068,832 View Licenses >