C# 使用 IronXL 打开 Excel 工作表
使用IronXL的GetWorkSheet()方法在C#中打开Excel工作表,以访问任何Excel文件类型(.xls、.csv、.tsv、.xlsx),只需两行代码即可读取或操作数据。
了解如何使用C#打开Excel工作表功能以处理Excel电子表格和所有文件类型,包括.xlsx。 打开 Excel 工作表、读取其数据并以编程方式对其进行操作是许多业务应用程序的基本功能。 IronXL.Excel 提供了一种简化的方法,无需 Excel Interop,提供了一种代码行数更少、响应时间更快的解决方案。
只需两个简单的方法调用,即可加载任何受支持的 Excel 文件并打开指定的工作表——无需复杂的设置或互操作。 IronXL 可以让您轻松地立即开始阅读或编辑数据。
-
1Install IronXL with NuGet Package Manager
-
2复制并运行这段代码。
WorkBook wb = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1");Dim wb As WorkBook = WorkBook.Load("sample.xlsx") Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")Dim wb As WorkBook = WorkBook.Load("sample.xlsx") Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") -
3部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronXL
最简工作流程(5 个步骤)
要打开和处理Excel工作表,请按照以下步骤操作:
- 安装用于打开Excel文件的C#库
- 使用
WorkBook.Load()将Excel文件加载到WorkBook对象中 - 使用多种访问方法选择一个
WorkSheet - 通过所选的
WorkSheet对象访问单元格数据 - 从行和列范围内获取数据
如何访问 Excel C# 库?
可以通过 DLL 访问 Excel C# 库,或者使用您喜欢的NuGet 管理器安装它。 访问 IronXL 库并将其添加到您的项目后,您就可以使用下面的所有函数在 C# 中打开 Excel 工作表。 有关详细的安装说明和系统要求,请查阅 IronXL 文档。
IronXL.Excel 支持在多个平台上部署,包括 Linux、macOS和 Docker容器,因此可用于各种开发环境。
如何加载 Excel 文件?
使用IronXL的WorkBook.Load()功能将Excel文件加载到项目中。 该函数需要一个字符串参数,即要打开的 Excel 文件的路径。 IronXL 支持加载各种电子表格格式,包括 XLS、XLSX、CSV、TSV 等。 有关加载不同文件类型的全面指导,请参阅 加载电子表格教程。
using IronXL;
// Get a worksheet by its name
WorkSheet workSheet = workBook.GetWorkSheet("SheetName");using IronXL;
// Get a worksheet by its name
WorkSheet workSheet = workBook.GetWorkSheet("SheetName");Imports IronXL
' Get a worksheet by its name
Dim workSheet As WorkSheet = workBook.GetWorkSheet("SheetName")Imports IronXL
' Get a worksheet by its name
Dim workSheet As WorkSheet = workBook.GetWorkSheet("SheetName")指定路径中的Excel文件将加载到workBook对象中。 现在,指定要打开的 Excel 工作表。 WorkBookLoadOptions参数允许您处理密码保护的工作簿并控制公式重算行为。
如何打开 Excel 工作表?
要打开Excel文件的特定WorkBook.GetWorkSheet()功能。 使用它可以根据工作表的名称打开工作表:
// Get a worksheet by its name
WorkSheet workSheet = workBook.GetWorkSheet("SheetName");// Get a worksheet by its name
WorkSheet workSheet = workBook.GetWorkSheet("SheetName");' Get a worksheet by its name
Dim workSheet As WorkSheet = workBook.GetWorkSheet("SheetName")' Get a worksheet by its name
Dim workSheet As WorkSheet = workBook.GetWorkSheet("SheetName")指定的workSheet中打开并显示其所有数据。 还有其他多种方法可以打开Excel文件的特定WorkSheet:
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();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()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()有关添加、重命名或删除工作表等更高级的工作表管理任务,请参阅管理工作表指南。
现在,从打开的Excel文件中获取WorkSheet的数据。
如何从工作表中获取数据?
以下方法获取打开的Excel文件中WorkSheet的数据:
- 获取Excel文件
WorkSheet的特定单元格值。 - 获取特定
Range中的数据。 - 获取
WorkSheet中的所有数据。 4.将数据导出为其他格式。
让我们通过这些示例来研究如何以不同的方式获取数据:
如何获取特定单元格值?
从ExcelWorkSheet中获取数据的第一种方法是获取特定单元格的值。 像这样访问它们:
// Access a specific cell value by its address
string val = workSheet["Cell Address"].ToString();// 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()' Access a specific cell value by its address
Dim val As String = workSheet("Cell Address").ToString()WorkSheet,如下例所示。 还可以通过指定行索引和列索引来访问特定的单元格值。
// 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
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()' Access a cell value by row index and column index
Dim val As String = workSheet.Rows(RowIndex).Columns(ColumnIndex).Value.ToString()以下是如何在 C# 项目中打开 Excel 文件并使用这两种方法获取特定单元格值的示例:
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);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)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)这段代码会显示以下输出:
Excel文件C6单元格中的值:
行和列的索引从0开始。
打开ExcelWorkSheets并获取特定的单元格数据,您可以在关于如何在C#中读取Excel数据的页面上阅读更多。 有关阅读 Excel 文件的更多示例,请查看 如何阅读 Excel 文件教程。
如何从特定范围获取数据?
现在检查如何使用IronXL从打开的ExcelWorkSheet中获取特定范围的数据。 选择范围功能为数据提取提供了强大的选项。
IronXL 提供了一种智能的方式来获取特定范围内的数据。 指定to的值:
// Access data from a specific range
var rangeData = workSheet["From Cell Address : To Cell Address"];// 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")' Access data from a specific range
Dim rangeData = workSheet("From Cell Address : To Cell Address")以下是使用范围从打开的ExcelWorkSheet中获取数据的示例:
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);
}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);
}Imports IronXL
Imports System
' Load Excel file
Dim workBook As WorkBook = WorkBook.Load("sample.xlsx")
Dim workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")
' Specify the range
For Each cell In workSheet("B2:B10")
Console.WriteLine("Value is: {0}", cell.Text)
NextImports IronXL
Imports System
' Load Excel file
Dim workBook As WorkBook = WorkBook.Load("sample.xlsx")
Dim workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")
' Specify the range
For Each cell In workSheet("B2:B10")
Console.WriteLine("Value is: {0}", cell.Text)
Next以上代码将从B10提取数据,如下所示:
Excel文件B10:
如何从行中获取数据?
您还可以描述特定行的范围。 例如:
var rowData = workSheet["A1:E1"];var rowData = workSheet["A1:E1"];Dim rowData = workSheet("A1:E1")Dim rowData = workSheet("A1:E1")这将显示E1的所有值。 阅读更多关于C# Excel 区域以及如何使用不同的行和列标识的内容。
如何从工作表中获取所有数据?
使用IronXL从打开的ExcelWorkSheet中获取所有单元格数据很简单。 对于这项任务,需要通过行和列索引访问每个单元格的值。 您还可以将整个工作表导出为 CSV、JSON 或 XML 等各种格式,以便于处理。 请参见以下示例,它遍历所有WorkSheet单元格并访问它们的值。
在此示例中,两个循环协同工作:一个遍历ExcelWorkSheet的每一行,另一个遍历特定行的每一列。 这样,每个单元格的值都很容易获取。
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());
}
}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 iImports 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上述代码的输出将显示完整打开的ExcelWorkSheet的每个单元格值。 为了处理更大的数据集,考虑使用IronXL的DataTable导出功能以获得更佳的性能和内存管理。
常见问题解答
在没有安装 Microsoft Office 的情况下,如何用 C# 打开 Excel 工作表?
IronXL 使用 WorkBook.Load() 和 GetWorkSheet() 方法提供了一个简单的解决方案。只需两行代码,您就可以打开任何 Excel 文件(.xls、.xlsx、.csv、.tsv),而无需在系统中安装 Excel Interop 或 Microsoft Office。
打开 Excel 工作表时支持哪些文件格式?
IronXL 支持多种电子表格格式,包括 .xls(旧 Excel 格式)、.xlsx(现代 Excel 格式)、.csv(逗号分隔值)和 .tsv(制表符分隔值)。WorkBook.Load() 函数可自动无缝处理所有这些格式。
我可以在 Linux 或 macOS 上部署 Excel 工作表应用程序吗?
是的,IronXL 支持跨平台部署,包括 Linux、macOS 和 Docker 容器。这使其成为 Windows 以外各种开发环境的通用工具,消除了特定平台的限制。
如何为我的项目安装 Excel C# 库?
您可以通过 DLL 下载访问 IronXL,也可以通过 NuGet 软件包管理器进行安装。该库提供了全面的文档,包含详细的安装说明和系统要求,可帮助您快速上手。
加载 Excel 文件并访问特定工作表的基本代码是什么?
只需使用两行WorkBook wb = WorkBook.Load("sample.xlsx");WorkSheet ws = wb.GetWorkSheet("Sheet1"); 这将加载 Excel 文件并打开命名的工作表,以便立即进行数据访问和操作。
Can IronXL handle password-protected Excel workbooks?
IronXL supports loading password-protected workbooks through its LoadingOptions parameter, which allows you to specify the password when opening the workbook.
How do I install IronXL to start working with Excel worksheets in C#?
You can install IronXL via the NuGet package manager by searching for IronXL.Excel or by downloading the library directly from Iron Software's website.
What platforms are supported by IronXL for Excel operations?
IronXL supports a wide range of platforms including Windows, Linux, macOS, and even Docker containers, making it adaptable for various development environments.
How does IronXL compare to using Excel Interop for C# applications?
IronXL provides a more efficient approach compared to Excel Interop by requiring fewer lines of code, offering faster performance, and eliminating dependencies on Excel installations.
Can I export data from an Excel worksheet to other formats using IronXL?
Yes, IronXL allows you to export data from an Excel worksheet to formats like CSV, JSON, or XML, enabling easier data handling and integration with other systems.
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。



