跳至页脚内容
使用 IRONXL

如何在 C# 中加载 Excel 文件

This article will demonstrate how to load Excel files using IronXL, a first-class Excel C# library, efficiently and programmatically.

How to Load Excel Files

  1. Open Visual Studio and create a new Visual Studio project.
  2. Download the IronXL NuGet package library.
  3. Make an Excel workbook in XLSX file or CSV file format.
  4. Now load the Excel file using the IronXL Library.
  5. Process the Excel values using the cell range.

IronXL and Features

What is IronXL?

The C# .NET Excel library, IronXL, can be used to read data from and convert Microsoft Excel documents to CSV files. No Microsoft Office, Microsoft.Office.Interop.Excel, or Excel Interop installation is required for IronXL. It is a completely independent .NET Excel applications library. Numerous spreadsheet formats can be read by it. IronXL is not just a static library.

Excel spreadsheets in a .NET context may be easily generated, modified, and viewed thanks to IronXL's straightforward C# API. Core .NET apps, Linux, macOS, Azure, and Xamarin are all fully supported by IronXL. Below is a bulleted list of IronXL's key features that set it apart from other applications for reading Excel files.

Key Features of IronXL

  • IronXL is one of the greatest C# libraries for Excel spreadsheets; it works with both .NET Core and .NET Framework.
  • Web Applications, Windows Forms, Console, and practically every other .NET Framework are supported by IronXL.
  • Operating systems that IronXL is compatible with include Windows, macOS, and Linux.
  • Accessing Excel files is quick and simple with IronXL.
  • A large number of Excel file types, including XLSX, CSV, XLS, XLST, TSV, XLSM, and others, can be read by IronXL.
  • Among our many possibilities are the import, update, and export functions for data tables and datasets.
  • IronXL allows you to import information into Excel files from a variety of sources, including databases, data tables, arrays, and collections. Data from Excel can then be exported to CSV, HTML, PDF, and other formats.
  • IronXL can generate computations for the Excel spreadsheet.
  • Text, integers, dates, currencies, formulas, and percentages are just a few of the data types that IronXL can handle for Excel columns.
  • Performance for large Excel files and datasets is optimized to manage substantial amounts of data effectively, with enhanced performance in multi-threaded scenarios due to multithreading capability

With these features, IronXL becomes a flexible and strong library for working with Excel files, giving developers more convenience and flexibility when managing their Excel document-related tasks programmatically. To know more about the IronXL library, refer to those documents.

Creating a .NET Core 6 Project

The IronXL library makes it easy to write data and create and read Excel files, as you will see in the upcoming sections of this newsletter.

The first step, starting a fresh project to make Excel files:

Create a new project

Choose "New Project" from the "File" menu once Visual Studio has launched.

The .NET "Console App" project templates will appear in the dialog box that follows; select them and double-click on "Next."

How to Load An Excel File in C#, Figure 1: New Project New Project

Configuring Your Project

You can type any project name you want for the Project after entering the new project's location in the Location area. Press the Next button to continue.

How to Load An Excel File in C#, Figure 2: Project Configuration Project Configuration

Selecting a Framework

With the Framework drop-down menu, you can choose a .NET Framework. The long-term supported version of .NET, 6.0, is being used here. Hit "Create" after that.

How to Load An Excel File in C#, Figure 3: Target framework Selection Target framework Selection

Installing IronXL Library

The following solution requires the IronXL library, which you should obtain. To accomplish this, input the below command into the Package Manager Console:

Install-Package IronXL.Excel

How to Load An Excel File in C#, Figure 4: IronXL Installation IronXL Installation

An alternative to this would be to use the NuGet Package Manager to search for the package "IronXL".

Browsing should result in a list of all NuGet packages linked to IronXL. Then, select the one needed and click the install button.

How to Load An Excel File in C#, Figure 5: NuGet Package Manager NuGet Package Manager

Using IronXL to Read Excel Files

The following code allows you to load and read Excel files using IronXL.

using IronXL;
using System.Linq;

// Load the workbook with the name "Demo.xlsx".
WorkBook workBook = WorkBook.Load("Demo.xlsx");

// Access the first worksheet in the workbook.
WorkSheet sheet = workBook.WorkSheets.First();

// Iterate through the range of cells A2 to B10 and print each cell's address and text content.
foreach (var cell in sheet["A2:B10"])
{
    Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
using IronXL;
using System.Linq;

// Load the workbook with the name "Demo.xlsx".
WorkBook workBook = WorkBook.Load("Demo.xlsx");

// Access the first worksheet in the workbook.
WorkSheet sheet = workBook.WorkSheets.First();

// Iterate through the range of cells A2 to B10 and print each cell's address and text content.
foreach (var cell in sheet["A2:B10"])
{
    Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
Imports IronXL
Imports System.Linq

' Load the workbook with the name "Demo.xlsx".
Private workBook As WorkBook = WorkBook.Load("Demo.xlsx")

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

' Iterate through the range of cells A2 to B10 and print each cell's address and text content.
For Each cell In sheet("A2:B10")
	Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)
Next cell
$vbLabelText   $csharpLabel

Above Code Explained

The first line of code, using IronXL;, is called to implement the library into this demo project, which makes the IronXL functionalities available.

Then load the existing Excel file, named "Demo.xlsx", using the method Load which is available in the WorkBook object.

In the code, the Load method can accept three types of inputs: a file name with the path, a stream, and byte arrays. In the above example, the method with the file name and the path is used to load the file.

To perform the read Excel file operation, we obtain the default WorkSheet by using the First() method to select the initial worksheet from the workbook's worksheet collection.

Subsequently, the Excel worksheet range is selected using the worksheet object by specifying the cell range. It will then iterate through each of the cell values within the specified range (A2:B10) and display the value along with the cell address.

How to Load An Excel File in C#, Figure 6: Output of Previous Code Example Output of Previous Code Example

The above example demonstrates loading an Excel file in C# and its resulting output when using IronXL. It utilizes Excel sheets by referencing the row and column index.

To know more about coding with IronXL, refer to this example.

Conclusion

IronXL is a well-liked Excel add-on that works without the need for any extra external libraries. It has several uses and doesn't need to have Microsoft Excel installed.

All programmable duties involving Microsoft Excel documents can be fully resolved with IronXL. You can add and remove data, search and replace, combine and unmerge data tables or cells, sort strings or numbers, visualize a data table, and save files. Formula computations are also possible with IronXL. You can also set cell data types and examine spreadsheet data with it. The ability to read and write CSV files obtainable with IronXL is a necessity for working with Excel data.

Want to try it yourself?

While IronXL offers a free trial, users can choose to upgrade to a paid membership for 1 year to receive updates and customer assistance. IronXL charges extra for security that allows for unfettered distribution. To find out more about the specifics of the pricing, see this licensing page.

常见问题解答

如何在C#中不使用Interop加载Excel文件?

你可以通过利用IronXL库在C#中加载Excel文件而无需使用Interop。首先在你的项目中安装IronXL NuGet包,然后使用WorkBook.Load方法以编程方式加载你的Excel文件。

设置IronXL在Visual Studio项目中的步骤是什么?

要在Visual Studio项目中设置IronXL,创建一个新项目,通过NuGet包管理器安装IronXL NuGet包,然后你可以开始使用该库的API创建或加载Excel文件。

IronXL可以与.NET Core 6配合使用吗?

是的,IronXL与.NET Core 6完全兼容。你可以无缝地将其集成到你的.NET Core项目中管理Excel文件。

IronXL如何管理大型Excel数据集?

IronXL经过优化,能够高效处理大型数据集。其多线程能力允许快速处理大量数据而不影响性能。

是否可以使用IronXL读取和转换Excel文件?

是的,IronXL允许你从Excel文件中读取数据并将其转换为例如CSV等不同格式。这可以通过其C# API以编程方式完成。

IronXL支持哪些Excel文件格式?

IronXL支持多种Excel文件格式,包括XLSX、CSV、XLS、XLST、TSV和XLSM,使其可用于不同项目需求。

IronXL可以在不同的操作系统上使用吗?

是的,IronXL是跨平台的,可以在Windows、macOS和Linux上使用,允许开发人员在多样化的环境中使用该库。

如何使用IronXL处理Excel中的不同数据类型?

IronXL可以管理Excel列中的各种数据类型,如文本、数字、日期和公式,提供一个灵活的数据操作解决方案。

在C#中使用IronXL进行Excel操作有哪些优势?

IronXL提供多个优势,包括易用性、与多个平台的兼容性、对广泛的Excel格式的支持,以及不依赖于Microsoft Excel安装。

如何尝试IronXL进行Excel文件管理?

你可以使用一个免费试用版来尝试IronXL。要访问其他特性、更新和客户支持,建议升级到付费会员。

Curtis Chau
技术作家

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

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