跳至页脚内容
使用 IRONXL

如何在 C# 中读取 Excel 文件

This article will discuss how you can read Excel data in C# using IronXL, a lightweight and fast library that is one of the most widely used C# libraries to interact with Excel files.

1. IronXL

IronXL is a C# library that allows developers to easily read, write, and manipulate Excel files without the need for Microsoft Excel to be installed on the machine. It is a powerful tool for working with Excel data or reading Excel files in C# and provides a simple and easy-to-use API for reading and writing Excel files.

1.1. IronXL Features

IronXL is a C# Excel library that provides a wide range of features or Excel functionality for working with Excel data, including:

Load, Write and Read Excel files

IronXL can read, write, and manipulate Excel files in a variety of formats, including XLS, XLSX, CSV, and XML. It also supports reading of password-protected Excel files and other advanced operations.

Creating charts

IronXL provides support for working with Excel charts, allowing developers to create and manipulate charts within Excel files.

Saving and exporting

IronXL provides support for saving and exporting Excel files in different formats.

Supports many formats

IronXL supports various file formats, such as XLS, XLSX, CSV, and XML.

Working with formulas

IronXL provides support for working with Excel formulas, allowing developers to calculate formulas within Excel files.

Cell Styling and many more

IronXL provides support for working with Excel cell styling, allowing developers to format Excel files professionally.

2. Creating a C# Project

This tutorial will create a project using the C# programming language and Visual Studio 2019.

  • Start Visual Studio now.
  • To start a new project, click "Create."
  • Click on the Next button after selecting C# Console Application form template.

How to Read Excel File in C#, Figure 1: Creating a C# Console Application in Visual Studio Creating a C# Console Application in Visual Studio

  • New window will appear, enter the name of the project and hit Next.

How to Read Excel File in C#, Figure 2: Choose a name for the new C# Console Application Project Choose a name for the new C# Console Application Project

  • In the next window, select the C# .NET Framework that is best supported by your project.

How to Read Excel File in C#, Figure 3: For best results, choose the latest available version of the .NET Framework for your C# Console Project For best results, choose the latest available version of the .NET Framework for your C# Console Project

Existing C# projects can also be used. Simply open the project and add the library. The following section will show how to install the Excel library IronXL.

3. Installing the IronXL Excel Library

There are two methods for downloading and installing the IronXL library:

  1. NuGet Package Manager Console
  2. NuGet Package Manager

3.1 NuGet Package Manager Console

  • Navigate to the Package Manager Console to manage NuGet packages. It is normally at the bottom of Visual Studio.

How to Read Excel File in C#, Figure 4: Access the NuGet Package Manager Console by going to **Tools** - **NuGet Package Manager** - **Package Manager Console**

Access the NuGet Package Manager Console by going to Tools > NuGet Package Manager** > Package Manager Console

  • In the Console, type the following command and press Enter.

    Install-Package IronXL.Excel

The IronXL NuGet Package will start to install and after a minute or two, it will be ready to use.

3.2. Install using NuGet Package Manager

The IronXL library may be installed directly using the NuGet Package Manager UI. To install IronXL, go through the following steps:

  • Hover over NuGet Package Manager in the Tools menu bar.

How to Read Excel File in C#, Figure 5: Access the NuGet Package Manager by going to **Tools** - **NuGet Package Manager** - Manage packages for Solution Access the NuGet Package Manager by going to Tools > NuGet Package Manager > Manage packages for Solution...

  • A dropdown list will appear again. Hover on NuGet Package Manager and click on the Manage NuGet Packages for Solution.

How to Read Excel File in C#, Figure 6: From the NuGet Package Manager submenu, click on Manage NuGet Packages for Solution to access the NuGet Package Manager From the NuGet Package Manager submenu, click on Manage NuGet Packages for Solution to access the NuGet Package Manager

  • Click the Install button after selecting IronXL.Excel package. It will automatically install the library.

4. Read Excel Files

IronXL offers the ability to read Excel sheets programmatically without any software. Just create a C# .NET project and with just only few lines of code, you can read Excel files in your console.

4.1. Reading XLSX file

Reading XLSX files in C# is quite easy using IronXL. The below example will discuss how reading a Microsoft Excel workbook using C# is a piece of cake.

This is an example of an XLSX file containing data from the range A1 to D5 for the following example.

How to Read Excel File in C#, Figure 7: The Excel File that will be used in this example The Excel File that will be used in this example

using IronXL;
using System;
using System.Linq;

// Load the workbook from a file
WorkBook workBook = WorkBook.Load("test.xlsx");

// Access the first worksheet in the workbook
WorkSheet workSheet = workBook.WorkSheets[0];

// Print the content of the worksheet to the console
Console.Write(workSheet);
using IronXL;
using System;
using System.Linq;

// Load the workbook from a file
WorkBook workBook = WorkBook.Load("test.xlsx");

// Access the first worksheet in the workbook
WorkSheet workSheet = workBook.WorkSheets[0];

// Print the content of the worksheet to the console
Console.Write(workSheet);
Imports IronXL
Imports System
Imports System.Linq

' Load the workbook from a file
Private workBook As WorkBook = WorkBook.Load("test.xlsx")

' Access the first worksheet in the workbook
Private workSheet As WorkSheet = workBook.WorkSheets(0)

' Print the content of the worksheet to the console
Console.Write(workSheet)
$vbLabelText   $csharpLabel

How to Read Excel File in C#, Figure 8: The output created from the above code example The output created from the above code example

4.2. Reading Multiple Excel files

Using IronXL, you can read multiple Excel workbooks at the same time. In the source code below, multiple Excel files are read.

using IronXL;
using System;
using System.Linq;

// Load the first workbook and access its first worksheet
WorkBook workBook = WorkBook.Load("test.xlsx");
WorkSheet workSheet = workBook.WorkSheets[0];
Console.WriteLine(workSheet);
Console.WriteLine();

// Load the second workbook (CSV format) and access its first worksheet
WorkBook workBook2 = WorkBook.Load("Example2.Sheet0.csv");
WorkSheet workSheet2 = workBook2.WorkSheets[0];
Console.WriteLine(workSheet2);
Console.WriteLine();
using IronXL;
using System;
using System.Linq;

// Load the first workbook and access its first worksheet
WorkBook workBook = WorkBook.Load("test.xlsx");
WorkSheet workSheet = workBook.WorkSheets[0];
Console.WriteLine(workSheet);
Console.WriteLine();

// Load the second workbook (CSV format) and access its first worksheet
WorkBook workBook2 = WorkBook.Load("Example2.Sheet0.csv");
WorkSheet workSheet2 = workBook2.WorkSheets[0];
Console.WriteLine(workSheet2);
Console.WriteLine();
Imports IronXL
Imports System
Imports System.Linq

' Load the first workbook and access its first worksheet
Private workBook As WorkBook = WorkBook.Load("test.xlsx")
Private workSheet As WorkSheet = workBook.WorkSheets(0)
Console.WriteLine(workSheet)
Console.WriteLine()

' Load the second workbook (CSV format) and access its first worksheet
Dim workBook2 As WorkBook = WorkBook.Load("Example2.Sheet0.csv")
Dim workSheet2 As WorkSheet = workBook2.WorkSheets(0)
Console.WriteLine(workSheet2)
Console.WriteLine()
$vbLabelText   $csharpLabel

How to Read Excel File in C#, Figure 9: Outputting the contents of more than one Excel document Outputting the contents of more than one Excel document

4.3. Read multiple worksheets in a single Excel file

IronXL offers another breakthrough feature to read multiple sheets in a single C# .NET program. You can use this feature for side-by-side comparisons. The below example will read multiple Excel sheets.

using IronXL;
using System;

// Load the workbook that contains multiple sheets
WorkBook workBook = WorkBook.Load("multiple.xlsx");

// Access the first worksheet
WorkSheet workSheet = workBook.WorkSheets[0];
Console.WriteLine(workSheet);
Console.WriteLine();

// Access the second worksheet
WorkSheet workSheet1 = workBook.WorkSheets[1];
Console.WriteLine(workSheet1);
Console.WriteLine();
using IronXL;
using System;

// Load the workbook that contains multiple sheets
WorkBook workBook = WorkBook.Load("multiple.xlsx");

// Access the first worksheet
WorkSheet workSheet = workBook.WorkSheets[0];
Console.WriteLine(workSheet);
Console.WriteLine();

// Access the second worksheet
WorkSheet workSheet1 = workBook.WorkSheets[1];
Console.WriteLine(workSheet1);
Console.WriteLine();
Imports IronXL
Imports System

' Load the workbook that contains multiple sheets
Private workBook As WorkBook = WorkBook.Load("multiple.xlsx")

' Access the first worksheet
Private workSheet As WorkSheet = workBook.WorkSheets(0)
Console.WriteLine(workSheet)
Console.WriteLine()

' Access the second worksheet
Dim workSheet1 As WorkSheet = workBook.WorkSheets(1)
Console.WriteLine(workSheet1)
Console.WriteLine()
$vbLabelText   $csharpLabel

How to Read Excel File in C#, Figure 10: Outputting the contents of more than one worksheet in a single Excel File Outputting the contents of more than one worksheet in a single Excel File

5. Conclusion

Reading Excel files in C# can be a difficult task for those new to programming. However, by using a library such as IronXL, the process becomes much simpler and more manageable.

IronXL is a powerful C# Excel library that provides a wide range of features for working with multiple Excel files or multiple worksheets at the same time.

For more information on how to read Excel files using IronXL, please visit the Read Excel Spreadsheets Code Example.

IronXL is free for development purposes but requires a license for commercial use. View the Reading Excel File in C# Tutorial for more code examples and step-by-step instructions on how to create and read Excel files.

常见问题解答

如何在 C# 中读取 Excel 文件而不使用 Interop?

您可以使用IronXL在C#中读取Excel文件,而无需安装Microsoft Excel。IronXL提供了一个简单的API与Excel文件交互,支持多种格式,如XLS、XLSX、CSV和XML。

使用IronXL进行Excel文件操作在C#中有什么优势?

IronXL提供许多优势,包括能够以多种格式读取和写入Excel文件,支持密码保护文件,创建图表,处理公式和单元格样式,所有这些都无需在您的机器上安装Microsoft Excel。

如何在 C# 项目中安装 IronXL?

通过在Visual Studio中使用NuGet包管理器控制台输入命令Install-Package IronXL.Excel或通过NuGet包管理器UI在您的C#项目中安装IronXL。

我可以使用IronXL一次性处理多个Excel文件吗?

是的,IronXL允许您同时读取和处理多个Excel文件。每个文件可以使用WorkBookWorkSheet类单独加载。

如何在C#中从Excel文件读取多个工作表?

使用IronXL,您可以通过WorkBook.Load方法从单个Excel文件读取多个工作表,然后使用WorkSheet类访问每个工作表。

IronXL可以免费用于开发吗?

IronXL可以用于开发目的免费,但需要商业或生产用途的商业许可证。

如何使用IronXL在C#中读取XLSX文件?

要使用IronXL读取XLSX文件,请使用WorkBook.Load("file.xlsx")加载文件,然后使用WorkSheet类访问您需要的特定工作表以读取其数据。

IronXL支持读取含有公式的Excel文件吗?

是的,IronXL支持读取包含公式的Excel文件,允许您以编程方式与公式进行交互和操作。

使用IronXL可以处理哪些文件格式?

IronXL支持多种文件格式,包括XLS、XLSX、CSV和XML,使其在不同的Excel文件操作中表现得非常灵活。

Curtis Chau
技术作家

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

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