USING IRONXL

How to Read an Excel File in a C# Console Application

Published April 3, 2024
Share:

Working with Excel files is a common task in software development, especially when dealing with data manipulation and analysis.

Reading Excel files in a C# console application is a common task in many business and data processing scenarios. Excel files, with their tabular data structure, are frequently used to store and exchange information.

In C# applications, IronXL provides a powerful and user-friendly solution for handling Excel files. This article will guide you through the process of reading Excel files in a C# Console Application using the IronXL library.

How to Read an Excel File in a C# Console Application

  1. Create a C# Console Application in Visual Studio
  2. Install the IronXL C# Excel Library
  3. Create a Workbook class Object
  4. Load the Excel file using Workbook.Load method
  5. Load the Worksheet using the WorkSheets method
  6. Read the Excel File data using WorkSheet.Row method
  7. Loop through the Cell values
  8. Print the Cell.Text on Console Window

Introduction to IronXL Library

IronXL is a .NET library designed to simplify Excel-related tasks for developers. Whether you need to create, modify, or read Excel files, IronXL offers a comprehensive set of features and functionalities. IronXL simplifies the process of interacting with Excel workbooks, sheets, and cells.

With IronXL, developers can effortlessly read and write data to Excel files, enabling seamless integration of Excel functionality into C# projects without Microsoft Office Interop or Excel installed.

By leveraging the capabilities of IronXL, developers can manipulate cell values, extract data from an Excel Workbook, and generate Excel documents dynamically. With its intuitive API and robust feature set, IronXL empowers developers to efficiently handle Excel data, making tasks such as data analysis, reporting, and document generation a breeze.

Whether you're working with Microsoft Excel files, spreadsheets, or worksheets, IronXL provides the tools you need to streamline your C# application development process.

Features of IronXL

Before moving forward, let's highlight some key features of IronXL:

  1. Create and Edit Excel Files: IronXL supports the creation and modification of Excel files, allowing developers to manipulate worksheets, cells, and formatting.
  2. Read Excel Files: The library facilitates the extraction of data from existing Excel files, making it easy to read and process Excel spreadsheet data within .NET applications.
  3. Export to Excel: IronXL enables the exporting of data from your application to an Excel format, ensuring compatibility with other tools and platforms.
  4. Formulas and Functions: Support for Excel formulas and functions allows dynamic calculations and data manipulation.
  5. Cell Formatting: IronXL provides features for formatting cells, including styles, fonts, colors, and borders.

Creating Console Application using Visual Studio

Let's start by creating a new C# Console Application in Visual Studio.

  1. Open Visual Studio.
    1. Select "Create a New Project".

How to Read an Excel File in a C# Console Application: Figure 1

  1. Choose "Console App" under C# templates.

How to Read an Excel File in a C# Console Application: Figure 2

  1. Provide a name for your project and click "Next."

How to Read an Excel File in a C# Console Application: Figure 3

  1. Set the appropriate .NET Framework and click "Create."

How to Read an Excel File in a C# Console Application: Figure 4

Install IronXL using the NuGet Package Manager Console or Solutions

Now that we have our console application, we need to install the IronXL library.

Option 1: Using the NuGet Package Manager Console

Install-Package IronXL.Excel

Option 2: Using the NuGet Package Manager in Visual Studio

  1. Right-click on your project in Solution Explorer.
  2. Select "Manage NuGet Packages."
  3. Search for "IronXL" and click "Install."

How to Read an Excel File in a C# Console Application: Figure 5

Steps to Read Excel Files using IronXL

Now, let's go through the steps to read an Excel file using IronXL in our C# Console Application. The following code snippet allows you to read Excel files in the C# console application:

using IronXL;
class Program
{
    public static void Main()
    {
        // Specify the path to the Excel file
        string excelFilePath = "path/to/your/excel/file.xlsx";
        // Create a WorkBook object
        WorkBook workBook = WorkBook.Load(excelFilePath);
        // Access the first worksheet
        WorkSheet workSheet = workBook.WorkSheets [0];
        // Iterate through rows and columns
        foreach (var row in workSheet.Rows)
        {
            foreach (var cell in row)
            {
                Console.Write(cell.Text + "\t");
            }
            Console.WriteLine();
        }
        // Close the workbook
        workBook.Close();
    }
}
using IronXL;
class Program
{
    public static void Main()
    {
        // Specify the path to the Excel file
        string excelFilePath = "path/to/your/excel/file.xlsx";
        // Create a WorkBook object
        WorkBook workBook = WorkBook.Load(excelFilePath);
        // Access the first worksheet
        WorkSheet workSheet = workBook.WorkSheets [0];
        // Iterate through rows and columns
        foreach (var row in workSheet.Rows)
        {
            foreach (var cell in row)
            {
                Console.Write(cell.Text + "\t");
            }
            Console.WriteLine();
        }
        // Close the workbook
        workBook.Close();
    }
}
Imports Microsoft.VisualBasic
Imports IronXL
Friend Class Program
	Public Shared Sub Main()
		' Specify the path to the Excel file
		Dim excelFilePath As String = "path/to/your/excel/file.xlsx"
		' Create a WorkBook object
		Dim workBook As WorkBook = WorkBook.Load(excelFilePath)
		' Access the first worksheet
		Dim workSheet As WorkSheet = workBook.WorkSheets (0)
		' Iterate through rows and columns
		For Each row In workSheet.Rows
			For Each cell In row
				Console.Write(cell.Text & vbTab)
			Next cell
			Console.WriteLine()
		Next row
		' Close the workbook
		workBook.Close()
	End Sub
End Class
VB   C#

This C# code snippet demonstrates how to use the IronXL library to read data from an Excel file and display it in a console application. The following Excel file will be read and displayed on the console window:

How to Read an Excel File in a C# Console Application: Figure 6

Now, let's break down the code step by step:

Importing the IronXL Namespace

using IronXL;
using IronXL;
Imports IronXL
VB   C#

This line imports the IronXL namespace, which contains classes and methods for working with any Excel document.

Main Method

public static void Main() {
    // Main method where the program execution begins 
}
public static void Main() {
    // Main method where the program execution begins 
}
Public Shared Sub Main()
	' Main method where the program execution begins 
End Sub
VB   C#

The Main method is the entry point of the application. We will write the above code snippet in this main method.

Excel File Path

string excelFilePath = "path/to/your/excel/file.xlsx";
string excelFilePath = "path/to/your/excel/file.xlsx";
Dim excelFilePath As String = "path/to/your/excel/file.xlsx"
VB   C#

This line specifies the path to the Excel file that you want to read. Replace "path/to/your/excel/file.xlsx" with the actual path to your Excel file.

Load Excel File

WorkBook workBook = WorkBook.Load(excelFilePath);
WorkBook workBook = WorkBook.Load(excelFilePath);
Dim workBook As WorkBook = WorkBook.Load(excelFilePath)
VB   C#

The WorkBook.Load() method is used to load the Excel file specified by excelFilePath into a WorkBook (excel) object named workBook.

Accessing Worksheet

WorkSheet workSheet = workBook.WorkSheets [0];
WorkSheet workSheet = workBook.WorkSheets [0];
Dim workSheet As WorkSheet = workBook.WorkSheets (0)
VB   C#

This line accesses the first Excel sheet in the workbook (workBook.WorkSheets [0]) and assigns it to an Excel WorkSheet object named workSheet.

Iterating Through Rows and Columns

foreach (var row in workSheet.Rows)
{
    foreach (var cell in row)
    {
        Console.Write(cell.Text + "\t");
    }
    Console.WriteLine();
}
foreach (var row in workSheet.Rows)
{
    foreach (var cell in row)
    {
        Console.Write(cell.Text + "\t");
    }
    Console.WriteLine();
}
Imports Microsoft.VisualBasic

For Each row In workSheet.Rows
	For Each cell In row
		Console.Write(cell.Text & vbTab)
	Next cell
	Console.WriteLine()
Next row
VB   C#

These nested foreach loops iterate through each row and column in the worksheet. For each cell, the cell's text value (cell.Text) is printed to the console, followed by a tab character ("\t"). After printing all cells in a row, a newline character is printed to move to the next row.

Closing the Workbook

workBook.Close();
workBook.Close();
workBook.Close()
VB   C#

Finally, the workBook.Close() method is called to close the workbook and release any resources associated with it.

For more working code examples please visit this code examples page.

Output

Upon running the application, the data from Excel file will be printed on the console window:

How to Read an Excel File in a C# Console Application: Figure 7

Conclusion

In this tutorial, we explored the process of reading Excel files in a C# Console Application using the IronXL library. With its intuitive features, IronXL simplifies Excel-related tasks, making it a valuable tool for .NET developers working on data-centric applications.

Feel free to explore additional functionalities provided by IronXL for more advanced Excel manipulations in your C# projects by visiting this documentation page.

IronXL offers a free trial for testing out its complete functionality before making an informed decision. For commercial use, you need to purchase a license key starting from $749.

For more information on license packages, please visit this license page. Download the library from here and give it a try.

< PREVIOUS
How to Rename Excel WorkSheet in C#
NEXT >
How to Export Datagridview To Excel in C#

Ready to get started? Version: 2024.11 just released

Free NuGet Download Total downloads: 1,111,773 View Licenses >