C# Open Excel Worksheets

This article was translated from English: Does it need improvement?
Translated
View the article in English

Learn how to use C# to open Excel Worksheet functions to work with Excel Spreadsheets and open all file types including (.xls, .csv, .tsv, and .xlsx). Opening an Excel Worksheet, reading its data, and manipulating it programmatically are essential for many developing applications. Here's a solution for every developer who wants a method with fewer lines of code and faster response times.

Quickstart: Load a Workbook and Open a Worksheet in One Line

Just two simple method calls let you load any supported Excel file and open a named worksheet—no complex setup or interop required. It’s an easy get-started path using IronXL so you can start reading or editing data immediately.

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 = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1");
  3. Deploy to test on your live environment

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

Minimal Workflow (5 Steps)

  • Install C# library to open Excel files
  • Load the Excel file into WorkBook object
  • Explore many ways to select WorkSheet from the opened Excel file
  • Access cell data via the selected WorkSheet object
  • Get data from rows and columns range
How To Work related to C# Open Excel Worksheets

Step 1

1. Access Excel C# Library

Access the Excel C# Library via DLL or install it using your preferred NuGet manager. Once you've accessed the IronXL library and added it to your project, you can use all the functions below to open Excel Worksheets in C#.

Install-Package IronXL.Excel

How to Tutorial

2. Load Excel File

Use the WorkBook.Load() function from IronXL to load Excel files into the project. This function requires a string parameter, which is the path of the Excel file to be opened. See here:

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-load-workbook.cs
using IronXL;

// Get a worksheet by its name
WorkSheet workSheet = workBook.GetWorkSheet("SheetName");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

The Excel file at the specified path will load into the workBook object. Now, we need to specify the Excel Worksheet which will be opened.


3. Open Excel WorkSheet

To open a specific WorkSheet of an Excel file, IronXL provides the WorkBook.GetWorkSheet() function. Using this, we can easily open the Worksheet by its name:

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-get-worksheet.cs
// Get a worksheet by its name
WorkSheet workSheet = workBook.GetWorkSheet("SheetName");
' Get a worksheet by its name
Dim workSheet As WorkSheet = workBook.GetWorkSheet("SheetName")
$vbLabelText   $csharpLabel

The specified WorkSheet will open in workSheet with all its data. There are also a few other ways to open a specific WorkSheet of an Excel file:

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-multiple-open.cs
using IronXL;
using System.Linq;

// Open by sheet index
WorkSheet workSheet = workBook.WorkSheets[0];

// Open the default worksheet
WorkSheet workSheet2 = workBook.DefaultWorkSheet;

// Open the first sheet
WorkSheet workSheet3 = workBook.WorkSheets.First();

// Open the first or default sheet
WorkSheet workSheet4 = workBook.WorkSheets.FirstOrDefault();
Imports IronXL
Imports System.Linq

' Open by sheet index
Private workSheet As WorkSheet = workBook.WorkSheets(0)

' Open the default worksheet
Private workSheet2 As WorkSheet = workBook.DefaultWorkSheet

' Open the first sheet
Private workSheet3 As WorkSheet = workBook.WorkSheets.First()

' Open the first or default sheet
Private workSheet4 As WorkSheet = workBook.WorkSheets.FirstOrDefault()
$vbLabelText   $csharpLabel

Now, we just need to get data from the opened Excel WorkSheet.


4. Get Data from WorkSheet

We can get data from an opened Excel WorkSheet in the following ways:

  1. Get a specific cell value of Excel WorkSheet.
  2. Get data in a specific Range.
  3. Get all the data from WorkSheet.

Let's see one by one how to get data in different ways with these examples:

4.1. Get Specific Cell Value

The first approach to getting data from an Excel WorkSheet is to get the specific cell values. It can be accessed like this:

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-cell-address.cs
// Access a specific cell value by its address
string val = workSheet["Cell Address"].ToString();
' Access a specific cell value by its address
Dim val As String = workSheet("Cell Address").ToString()
$vbLabelText   $csharpLabel

workSheet is the WorkSheet of the Excel file, as we will see in the following examples. Specific cell values can also be accessed by specifying "row index" and "column index."

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-cell-row.cs
// Access a cell value by row index and column index
string val = workSheet.Rows[RowIndex].Columns[ColumnIndex].Value.ToString();
' Access a cell value by row index and column index
Dim val As String = workSheet.Rows(RowIndex).Columns(ColumnIndex).Value.ToString()
$vbLabelText   $csharpLabel

Let's see an example of how to open an Excel file in our C# project and get specific cell values using both methods:

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-specified-cell.cs
using IronXL;
using System;

WorkBook workBook = WorkBook.Load("sample.xlsx");

// Open WorkSheet
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");

// Get value By Cell Address
int intValue = workSheet["C6"].Int32Value;

// Get value by Row and Column Address
string strValue = workSheet.Rows[3].Columns[1].Value.ToString();

Console.WriteLine("Getting Value by Cell Address: {0}", intValue);
Console.WriteLine("Getting Value by Row and Column Indexes: {0}", strValue);
Imports IronXL
Imports System

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")

' Open WorkSheet
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")

' Get value By Cell Address
Private intValue As Integer = workSheet("C6").Int32Value

' Get value by Row and Column Address
Private strValue As String = workSheet.Rows(3).Columns(1).Value.ToString()

Console.WriteLine("Getting Value by Cell Address: {0}", intValue)
Console.WriteLine("Getting Value by Row and Column Indexes: {0}", strValue)
$vbLabelText   $csharpLabel

This code displays the following output:

1output related to 4.1. Get Specific Cell Value

Value of Excel file sample.xlsx in row [3].Column [1] and C6 cell:

1excel related to 4.1. Get Specific Cell Value

The rows and column indices start from 0.

Open Excel WorkSheets and get specific cell data, and you can read more about how to read Excel data in C# from already open Excel Worksheets.

4.2. Get Data from Specific Range

Now let's see how to get data in a specific range from an opened Excel WorkSheet using IronXL.

IronXL provides an intelligent way to get data in a specific range. We just specify from to to values:

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-select-range.cs
// Access data from a specific range
var rangeData = workSheet["From Cell Address : To Cell Address"];
' Access data from a specific range
Dim rangeData = workSheet("From Cell Address : To Cell Address")
$vbLabelText   $csharpLabel

Let's see an example of how to use range to get data from an open Excel WorkSheet:

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-from-range.cs
using IronXL;
using System;

// Load Excel file
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");

// Specify the range
foreach (var cell in workSheet["B2:B10"])
{
    Console.WriteLine("Value is: {0}", cell.Text);
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

The above code will pull data from B2 to B10 as follows:

2output related to 4.2. Get Data from Specific Range

We can see the values of the Excel file sample.xlsx, from B2 to B10:

2excel related to 4.2. Get Data from Specific Range

4.3. Get Data from Row

We can also describe a range for a specific row. For example:

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-select-row-range.cs
var rowData = workSheet["A1:E1"];
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This will display all values from A1 to E1. Read more about C# Excel Ranges and how to work with different row and column identifications.

4.4. Get All Data from WorkSheet

Getting all the cell data from the open Excel WorkSheet is also easy using IronXL. For this task, we need to access each cell value by row and column indexes. Let's see the following example, in which we will traverse all WorkSheet cells and access its values.

In this example, basically, two loops are working: one is for traversing each row of Excel WorkSheet and the other is for traversing each column of a specific row. In this way, each cell value can be easily accessed.

:path=/static-assets/excel/content-code-examples/how-to/c-sharp-open-excel-worksheet-all.cs
using IronXL;
using System;
using System.Linq;

// Load Excel file
WorkBook workBook = WorkBook.Load("sample2.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");

// Access all rows of the open Excel WorkSheet
for (int i = 0; i < workSheet.Rows.Count(); i++)
{
    // Access all columns of a specific row
    for (int j = 0; j < workSheet.Columns.Count(); j++)
    {
        // Access each cell for the specified column
        Console.WriteLine(workSheet.Rows[i].Columns[j].Value.ToString());
    }
}
Imports IronXL
Imports System
Imports System.Linq

' Load Excel file
Private workBook As WorkBook = WorkBook.Load("sample2.xlsx")
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")

' Access all rows of the open Excel WorkSheet
For i As Integer = 0 To workSheet.Rows.Count() - 1
	' Access all columns of a specific row
	For j As Integer = 0 To workSheet.Columns.Count() - 1
		' Access each cell for the specified column
		Console.WriteLine(workSheet.Rows(i).Columns(j).Value.ToString())
	Next j
Next i
$vbLabelText   $csharpLabel

The output of the above code will display each cell value of the complete open Excel WorkSheet.


Tutorial Quick Access

Documentation related to Tutorial Quick Access

API Reference Resource

Use the IronXL API Reference resource as your guide to all functions and classes for use in your projects, as well as namespaces, method fields, enums, and feature sets.

API Reference Resource

常见问题解答

如何使用 C# 打开和操作 Excel 工作表?

您可以使用 IronXL 库在 C# 中打开和操作 Excel 工作表。首先安装库,然后使用 WorkBook.Load() 将 Excel 文件加载到 WorkBook 对象中。可以使用 WorkBook.GetWorkSheet() 方法选择并打开特定工作表。

C# 中支持哪些 Excel 文件类型?

在 C# 中,您可以使用 IronXL 库处理各种 Excel 文件类型,例如 .xls、.csv、.tsv 和 .xlsx。

如何从 C# 中的 Excel 工作表中检索特定单元格的值?

要使用 C# 从 Excel 工作表中检索特定单元格的值,利用 IronXL 库通过地址访问单元格,使用 ws["Cell Address"].ToString() 或指定行和列索引使用 ws.Rows[RowIndex].Columns[ColumnIndex].Value.ToString()。

我可以使用 C# 从工作表中定义的单元格范围中提取数据吗?

是的,您可以通过使用 IronXL 库的语法 ws["From Cell Address : To Cell Address"] 使用 C# 从工作表中定义的单元格范围中提取数据,以访问指定范围内的数据。

如何以编程方式读取 Excel 工作表中的所有数据?

要在 C# 中以编程方式读取 Excel 工作表中的所有数据,可以使用 IronXL 库通过每行每列循环,使用 ws.Rows[i].Columns[j].Value.ToString() 访问每个单元格的值。

为什么开发人员应该使用库来处理 C# 中的 Excel 文件?

使用像 IronXL 这样的库对开发人员有利,因为它简化了在 C# 项目中打开、读取和操作 Excel 文件的过程,提供了处理 Excel 内容的强大功能和类。

如何在我的 C# 项目中安装 Excel 处理库?

要在 C# 项目中安装 IronXL 库进行 Excel 处理,请使用命令 dotnet add package IronXL.Excel 通过 NuGet 包管理器。

是否可以在 workbook 中通过索引打开工作表?

是的,您可以使用 IronXL 库的语法 WorkSheet ws = wb.WorkSheets[index]; 打开 workbook 中的工作表,其中 'index' 代表工作表在 workbook 中的位置。

如何在 C# 中处理 Excel 文件中的特定单元格范围?

IronXL 允许您通过指定诸如 "A1:E1" 的范围来处理特定的单元格范围,以访问和操作 Excel 文件中该定义范围内的数据。

我可以在哪里找到有关 C# 中 Excel 处理可用函数的更多信息?

有关 C# 中 Excel 处理可用函数的详细信息,您可以参考 IronXL API 参考资源,该资源提供有关函数、类和命名空间的全面文档。访问 API 参考:https://ironsoftware.com/csharp/excel/object-reference/api/。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布