跳至页脚内容
使用 IRONXL

如何使用 IronXL 处理 Excel 文件:一个 .NET Excel 库

In this article, one of the most popular libraries, IronXL, an Excel library will be used to compare and contrast how to interact with Microsoft Excel documents programmatically in .NET technologies and it will create an Excel spreadsheet environment to read Excel worksheets, write Excel tables, and export spreadsheet files to CSV.

IronXL - Fast Excel Library Features

Documents created in Microsoft Excel can be read and converted to CSV files using the C# IronXL, .NET Excel library. IronXL is a standalone .NET Excel software library that doesn't need Microsoft Office or Microsoft.Office.Interop.Excel or Excel Interop to be installed. It can read several spreadsheet formats and is not just a static library.

IronXL's simple C# API makes it easy to read multiple worksheets, modify, and generate Excel spreadsheets in a .NET environment. It fully supports Xamarin, Linux, macOS, Azure, .NET Core applications, and .NET Framework.

  • IronXL, a C# library compatible with both .NET Core and .NET Framework, is among the best for Excel spreadsheets.
  • IronXL supports almost every .NET Framework, including Web apps, Windows Forms, and Console.
  • IronXL can be used with Linux, macOS, and Windows operating systems.
  • IronXL offers quick and easy access to Excel files.
  • IronXL is capable of reading a wide range of Excel file types, including XLSX, CSV, XLS, XLST, TSV, XLSM, and more. The functions for importing, updating, and exporting data tables and datasets are only a few of the library's numerous options.
  • For the Excel spreadsheet, IronXL is capable of producing calculations.
  • IronXL supports numerous data types for Excel columns, including text, integers, dates, currencies, formulas, and percentages.
  • IronXL can handle multiple values in the form of dates, currencies, percentages, text, numbers, formulas, and more Excel column data types.

Creating a .NET Core 6 Project

The next sections of this article will show you how simple it is to create/read an Excel file with the IronXL library.

Step 1: Launching a new endeavor to create Excel files.

After starting Visual Studio, select "New Project" from the "File" menu.

In the resulting dialogue box, select the "Console App" .NET Project templates, then click "Next."

.NET Excel Library (Developer Tutorial), Figure 1: New Project New Project

Once the new project's location has been entered in the Location field, you may enter any project name you choose for the Project. Click the Next button to proceed.

.NET Excel Library (Developer Tutorial), Figure 2: Project Configuration Project Configuration

A .NET Framework can be selected using the Framework drop-down option. Here, we're using .NET 6.0, the long-term supported version. Then press Create.

.NET Excel Library (Developer Tutorial), Figure 3: Framework Selection Framework Selection

Get the IronXL library, which is required for the following solution. To do this, enter the following command into the NuGet Package Manager Console:

Install-Package IronXL.Excel

.NET Excel Library (Developer Tutorial), Figure 4: IronXL Installation IronXL Installation

Another option is to use the NuGet Package Manager to search for the package "IronXL". Then, select the desired package to download from the list of all NuGet packages related to IronXL.

.NET Excel Library (Developer Tutorial), Figure 5: NuGet Package Manager NuGet Package Manager

Create Excel Using IronXL

With IronXL, you will be able to create new Excel files with just a few lines of code! IronXL can be used to create files in the XLS (an older Excel format) and XLSX (a more current format) as shown in the code samples below.

using IronXL;

public class IronXLExample
{
    public static void Main(string[] args)
    {
        // Create a new workbook
        WorkBook workbook = WorkBook.Create();

        // Create a new worksheet named "Sheet1"
        WorkSheet worksheet = workbook.CreateWorkSheet("Sheet1");

        // Set the value of cell A1
        worksheet["A1"].Value = "test";

        // Save the workbook to a file
        workbook.SaveAs("sample1.xlsx");
    }
}
using IronXL;

public class IronXLExample
{
    public static void Main(string[] args)
    {
        // Create a new workbook
        WorkBook workbook = WorkBook.Create();

        // Create a new worksheet named "Sheet1"
        WorkSheet worksheet = workbook.CreateWorkSheet("Sheet1");

        // Set the value of cell A1
        worksheet["A1"].Value = "test";

        // Save the workbook to a file
        workbook.SaveAs("sample1.xlsx");
    }
}
Imports IronXL

Public Class IronXLExample
	Public Shared Sub Main(ByVal args() As String)
		' Create a new workbook
		Dim workbook As WorkBook = WorkBook.Create()

		' Create a new worksheet named "Sheet1"
		Dim worksheet As WorkSheet = workbook.CreateWorkSheet("Sheet1")

		' Set the value of cell A1
		worksheet("A1").Value = "test"

		' Save the workbook to a file
		workbook.SaveAs("sample1.xlsx")
	End Sub
End Class
$vbLabelText   $csharpLabel

The code above demonstrates the use of the IronXL package to create a new Excel workbook and worksheet. The WorkBook.Create() method is used to create a new workbook, and then the CreateWorkSheet("Sheet1") function creates a worksheet tab with the specified name. Changes are saved in a specified location using the SaveAs method.

.NET Excel Library (Developer Tutorial), Figure 6: Excel Output Excel Output

To learn more about creating Excel files, check this tutorial to create one in .NET.

Read Data From Excel and Export to an Excel File

Exporting data to the XLSX or XLS formats requires only a few lines of code. Below is an example of source code that can be used to export data from an Excel file into a simple tabular format:

using IronXL;

public class ExcelReadExportExample
{
    public static void Main(string[] args)
    {
        // Load an existing Excel file
        var workbook = WorkBook.LoadExcel("Demo file.xlsx");

        // Get a worksheet from the workbook
        WorkSheet workSheet = workbook.GetWorkSheet("Sheet1");

        // Read the value of cell A1
        string addressVal = workSheet["A1"].ToString();
        Console.WriteLine(addressVal);

        // Modify the value of cell A2
        workSheet["A2"].Value = "test";

        // Save the workbook in multiple formats
        workbook.SaveAs("export.xlsx");
        // Or save as XLS
        workbook.SaveAs("export.xls");
        // Or save the specific worksheet as an XLS file
        workbook.WorkSheets[0].SaveAs("export.xls");
    }
}
using IronXL;

public class ExcelReadExportExample
{
    public static void Main(string[] args)
    {
        // Load an existing Excel file
        var workbook = WorkBook.LoadExcel("Demo file.xlsx");

        // Get a worksheet from the workbook
        WorkSheet workSheet = workbook.GetWorkSheet("Sheet1");

        // Read the value of cell A1
        string addressVal = workSheet["A1"].ToString();
        Console.WriteLine(addressVal);

        // Modify the value of cell A2
        workSheet["A2"].Value = "test";

        // Save the workbook in multiple formats
        workbook.SaveAs("export.xlsx");
        // Or save as XLS
        workbook.SaveAs("export.xls");
        // Or save the specific worksheet as an XLS file
        workbook.WorkSheets[0].SaveAs("export.xls");
    }
}
Imports IronXL

Public Class ExcelReadExportExample
	Public Shared Sub Main(ByVal args() As String)
		' Load an existing Excel file
		Dim workbook = WorkBook.LoadExcel("Demo file.xlsx")

		' Get a worksheet from the workbook
		Dim workSheet As WorkSheet = workbook.GetWorkSheet("Sheet1")

		' Read the value of cell A1
		Dim addressVal As String = workSheet("A1").ToString()
		Console.WriteLine(addressVal)

		' Modify the value of cell A2
		workSheet("A2").Value = "test"

		' Save the workbook in multiple formats
		workbook.SaveAs("export.xlsx")
		' Or save as XLS
		workbook.SaveAs("export.xls")
		' Or save the specific worksheet as an XLS file
		workbook.WorkSheets(0).SaveAs("export.xls")
	End Sub
End Class
$vbLabelText   $csharpLabel

In the preceding example, an existing Excel file is loaded using the LoadExcel method, which takes the file name and path as an argument. The file is then imported into the WorkBook object. Worksheets are loaded using the GetWorkSheet function by specifying the sheet name. The value of a cell can be read by specifying its address. The code also demonstrates modifying a worksheet value and saving the workbook in different file formats using the SaveAs method.

.NET Excel Library (Developer Tutorial), Figure 7: Console Output Console Output

A worksheet can also be referenced either by name or index value to export data from an Excel spreadsheet to a separate file. For more details, visit another tutorial to learn more about exporting Excel files.

Conclusion

From creating new files to carrying out precise calculations, IronXL has you covered for all your Excel projects. Today, we took a closer look at how IronXL can be utilized to create, read, and export Excel files, and saw how with just a few lines of code you can have full control over these processes.

For every programmable activity about Microsoft Excel documents, IronXL offers a comprehensive solution. It is possible to perform formula calculations, sort strings or numbers, cut and add data, search and replace, merge and unmerge cells, visualize data, and save files. With it, you can also set cell data types and validate spreadsheet data. The ability to read and write CSV files enables interaction with Excel data.

IronXL costs $799 upon launch, but customers can opt to pay a one-year membership fee for improvements and product support. IronXL levies an additional security fee that permits unrestricted redistribution. Visit this licensing page to learn more about the specifics of the pricing.

常见问题解答

如何在不使用Interop的情况下在.NET中创建和读取Excel文件?

使用IronXL,您可以轻松地在.NET中创建和读取Excel文件,而无需依赖Microsoft Office Interop。您可以使用WorkBook.Create()方法创建新的Excel文件,并使用LoadExcel读取已有的文件。

使用独立的.NET Excel库有什么好处?

像IronXL这样的独立.NET Excel库的优点是不需要安装Microsoft Office或Interop。它允许跨不同平台(如Windows、macOS和Linux)无缝读取、写入和导出Excel文件。

如何以编程方式将Excel文件转换为CSV格式?

IronXL提供功能轻松将Excel文件转换为CSV格式。使用SaveAs方法将您的Excel工作簿导出为CSV文件格式。

哪个平台与IronXL兼容,用于处理Excel文件?

IronXL兼容包括Xamarin、Linux、macOS、Azure、.NET Core和.NET Framework在内的多个平台,为在不同环境中工作的开发人员提供灵活性。

使用IronXL可以在Excel列中处理哪些类型的数据?

IronXL支持广泛的数据类型,包括文本、整数、日期、货币、公式和百分比,用于Excel列中的灵活数据处理。

如何使用.NET库在Excel电子表格中执行计算?

使用IronXL,您可以通过编程方式使用公式在Excel电子表格中执行计算。这样可以自动化复杂的计算和数据处理任务。

是否可以使用.NET库处理Excel文件中的多个工作表?

是的,IronXL允许您在一个Excel文件中处理多个工作表。您可以轻松访问、修改并通过其API在不同工作表之间导出数据。

如何在.NET Core项目中设置IronXL?

要在.NET Core项目中设置IronXL,您可以通过NuGet包管理器安装。在包管理器控制台中使用命令Install-Package IronXL.Excel,或者通过Visual Studio中的NuGet包管理器添加。

IronXL有哪些许可和定价选项?

IronXL提供各种许可选项,包括为改进和支持支付的一年会员费。安全功能和无限制再分发权可能会产生额外费用。

Curtis Chau
技术作家

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

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