與其他組件的比較 IronXL 與 ClosedXML 的比較 Jordi Bardia 更新日期:7月 28, 2025 Download IronXL NuGet 下載 DLL 下載 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article Today's software applications require the ability to create databases and generate reports using Excel files. There are many libraries available today that allow users to do this without the need for Microsoft Excel. In this article, we are going to discuss and compare how to work with Microsoft Excel documents programmatically in C# .NET technologies using two of the most popular libraries, IronXL and ClosedXML, for reading, manipulating, and writing Excel reports. IronXL and ClosedXML both provide methods to create, edit, and read Excel documents in the .NET framework. The next question is to decide which C# Excel library is best suited to your project. 這篇文章將幫助您為您的應用程式選擇最佳選擇。 Let's look first at what both libraries have to offer, and then move on to the comparison itself. IronXL 函式庫 IronXL 是一個 .NET 函式庫,旨在通過 C# 讀取和編輯 Microsoft Excel 文檔。 IronXL.Excel is a standalone .NET software library for reading a wide range of spreadsheet formats. 它不需要安裝 Microsoft Excel,也不依賴於 Interop。 IronXL is an intuitive C# API that allows you to read, edit, and create Excel spreadsheet files in .NET with lightning-fast performance. IronXL 完全支持 .NET Core、.NET 框架、Xamarin、行動裝置、Linux、macOS 以及 Azure。 IronXL is a leading .NET Core and .NET Framework Excel spreadsheet library for C#. IronXL 功能集 Load, read, and edit data from XLS/XLSX/CSV/TSV Saving and exporting to XLS/XLSX/CSV/TSV/JSON System.Data Objects: Work with Excel Spreadsheets as System.Data.DataSet and System.Data.DataTable objects. Formulas: Work with Excel formulas, recalculated every time a sheet is edited. Ranges: Easy to use WorkSheet ["A1:B10"] syntax. 直觀地組合和創建範圍。 Sorting: Sort ranges, columns, and rows. Styling: Cell visual styles, font, size, background pattern, border, alignment, and number formats. More Features of IronXL can be explored using this link. ClosedXML ClosedXML is a .NET library for reading, manipulating, and writing Excel 2007+ (.xlsx, .xlsm) files. It aims to provide an intuitive and user-friendly interface for dealing with the underlying OpenXML Library. The .xlsx is a file extension for an OpenXML underlying API spreadsheet file format used by Microsoft Excel. The .xlsm files support macros. The .xltm are macro-enabled template files. The .xls format is a proprietary binary format, while .xlsx is based on Office OpenXML format. ClosedXML is a .NET library for report generation in Microsoft Excel without requiring Excel to be installed on the machine that's running the code. ClosedXML Features ClosedXML has a user-friendly interface with extensive API functions to handle creation and extraction of content in Excel. With these API calls and pull requests, you can modify every little detail in an Excel sheet or workbook. All the features are listed below: Formulas Validation Hyper-Links Protection (Sheet and Cell level) Conditional Formatting Freeze Panes Tables Ranges Styling Page Setup (Printing) Auto-Filters Comments 本文接下來的部分內容如下: Create a Console Application IronXL C# Library Installation ClosedXML Installation Create and Save a new Excel Workbook and Sheet Read Excel File Working with Excel Formulas Licensing Summary and Conclusion 1. 創建控制應用程式 使用以下步驟創建控制應用程式: 開啟 Visual Studio 2022 IDE。 Click on "Create a new project." On the "Create a new project" page, select C# in the language dropdown list, Windows from the Platforms list, and Console from the Project types list. 從顯示的專案範本中選擇Console App (.NET Framework)。 Create a Project 點擊下一步。 Name the project "DemoApp" and Click Next. DemoApp Project 在附加信息頁面,指定您想使用的框架版本。 We will use .NET Framework 6.0 in this example. .NET Framework 6.0 點擊創建完成過程。 Now, the project is created and we are almost ready to test the libraries. 然而,我們仍然需要在專案中安裝並集成它們。 讓我們先安裝 IronXL。 2. IronXL C# 函式庫安裝 You can download and install the IronXL library using the following methods: 使用含有 NuGet 套件的 Visual Studio。 Manually installing the DLL. Let's take a closer look at each one. 2.1. Using Visual Studio with NuGet Packages Visual Studio 提供 NuGet 包管理器來在專案中安裝 NuGet 套件。 您可以通過專案選單訪問它,或者在方案資源管理器中右鍵單擊您的項目。 Package Manager Next, from the Browse tab > search for IronXL.Excel > Install IronXL NuGet Install 完成。 2.2. Manually Installing the DLL Another way to download and install the IronXL C# Library is to make use of the following steps to install the IronXL NuGet package through the Developer Command Prompt. 打開 開發命令提示 —— 通常在 Visual Studio 資料夾中找到。 輸入以下命令: Install-Package IronXL.Excel 按 Enter 鍵。 這將會下載並安裝套件。 重新加載您的 Visual Studio 專案並開始使用。 2.3. Add Necessary Using Directives In Solution Explorer, right-click the Program.cs file and then click View Code. 在代碼文件的頂部添加以下命名空間指令: using IronXL; using IronXL; Imports IronXL $vbLabelText $csharpLabel 完成了! IronXL 已經下載並安裝完成,準備就緒。 However, before that, we should install ClosedXML. 3. ClosedXML Installation To install the ClosedXML package, you can directly download it from NuGet or from NuGet Package Manager/Console in the project. For directly installing ClosedXML, click on this link. 3.1. Using the NuGet Package Manager/Console Open the NuGet Package Manager from project solution explorer, browse for ClosedXML, and click install. ClosedXML NuGet Install 或者: 打開 開發命令提示 —— 通常在 Visual Studio 資料夾中找到。 輸入以下命令: Install-Package ClosedXML 按 Enter 鍵。 這將會下載並安裝套件。 重新加載您的 Visual Studio 專案並開始使用。 3.2. Add Necessary Using Directives In Solution Explorer, right-click the Program.cs file and then click View Code. 在代碼文件的頂部添加以下命名空間指令: using ClosedXML.Excel; using ClosedXML.Excel; Imports ClosedXML.Excel $vbLabelText $csharpLabel 4. Create and Save a New Excel Workbook and Sheets 工作簿是包含多個帶有行和列的工作表的 Excel 文件。 Both libraries provide the facility to create a new Excel workbook and sheets. 讓我們一步步查看代碼。 4.1. New Excel Workbook and Sheets using IronXL It could not be any easier to create a new Excel Workbook using IronXL. It takes just one line of code. 是的,真的。 將以下代碼添加到 Program.cs 文件中的靜態 void 主函數: // Create a new Excel workbook using IronXL WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX); // Create a new Excel workbook using IronXL WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX); ' Create a new Excel workbook using IronXL Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX) $vbLabelText $csharpLabel IronXL 可以創建 XLS (較舊的 Excel 文件版本) 和 XLSX (當前且較新的文件版本) 文件格式。 And, it's even simpler to create a default Worksheet: // Create a new worksheet within the workbook var worksheet = workbook.CreateWorkSheet("IronXL Features"); // Create a new worksheet within the workbook var worksheet = workbook.CreateWorkSheet("IronXL Features"); ' Create a new worksheet within the workbook Dim worksheet = workbook.CreateWorkSheet("IronXL Features") $vbLabelText $csharpLabel 您現在可以使用工作表變量來設置單元格值以及執行除保存以外的幾乎所有操作。 // Add data and styles to the new worksheet worksheet["A1"].Value = "Hello World"; worksheet["A2"].Style.BottomBorder.SetColor("#ff6600"); worksheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double; // Add data and styles to the new worksheet worksheet["A1"].Value = "Hello World"; worksheet["A2"].Style.BottomBorder.SetColor("#ff6600"); worksheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double; ' Add data and styles to the new worksheet worksheet("A1").Value = "Hello World" worksheet("A2").Style.BottomBorder.SetColor("#ff6600") worksheet("A2").Style.BottomBorder.Type = IronXL.Styles.BorderType.Double $vbLabelText $csharpLabel Save the Excel file: // Save the spreadsheet workbook.SaveAs("NewExcelFile.xlsx"); // Save the spreadsheet workbook.SaveAs("NewExcelFile.xlsx"); ' Save the spreadsheet workbook.SaveAs("NewExcelFile.xlsx") $vbLabelText $csharpLabel The output file looks as follows: NewExcelFile output 4.2. New Excel Workbook and Sheets using ClosedXML You can also create excel files (.xlsx, .xlsm) easily using ClosedXML. The following code is a typical example which serves to generate a simple Excel file and save it. You can add a new sample sheet to your workbook and assign values to cells in your Excel application. // Create a new empty Excel file var workbook = new XLWorkbook(); // Create a new worksheet and set cell A1 value to 'Hello world!' var worksheet = workbook.Worksheets.Add("ClosedXML Features"); worksheet.Cell("A1").Value = "Hello world!"; // Save to XLSX file workbook.SaveAs("Spreadsheet.xlsx"); // Create a new empty Excel file var workbook = new XLWorkbook(); // Create a new worksheet and set cell A1 value to 'Hello world!' var worksheet = workbook.Worksheets.Add("ClosedXML Features"); worksheet.Cell("A1").Value = "Hello world!"; // Save to XLSX file workbook.SaveAs("Spreadsheet.xlsx"); ' Create a new empty Excel file Dim workbook = New XLWorkbook() ' Create a new worksheet and set cell A1 value to 'Hello world!' Dim worksheet = workbook.Worksheets.Add("ClosedXML Features") worksheet.Cell("A1").Value = "Hello world!" ' Save to XLSX file workbook.SaveAs("Spreadsheet.xlsx") $vbLabelText $csharpLabel The output file looks as follows: Spreadsheet File Output 5. Read Excel File (Import Excel File) Both libraries can open and read existing Excel documents. 讓我們看看代碼範例。 5.1. 使用 IronXL 讀取 Excel 文件 The IronXL WorkBook class represents an Excel sheet. To open an Excel file using C#, we use WorkBook.Load and specify the path of the file (.xlsx). 以下一行代碼用於打開文件以供閱讀: // Load WorkBook var workbook = WorkBook.Load(@"Spreadsheets\\sample.xlsx"); // Load WorkBook var workbook = WorkBook.Load(@"Spreadsheets\\sample.xlsx"); ' Load WorkBook Dim workbook = WorkBook.Load("Spreadsheets\\sample.xlsx") $vbLabelText $csharpLabel Each WorkBook can have multiple worksheets in the Excel document. If the workbook contains multiple worksheets, retrieve them by name as follows: // Open Sheet for reading var worksheet = workbook.GetWorkSheet("sheetnamegoeshere"); // Open Sheet for reading var worksheet = workbook.GetWorkSheet("sheetnamegoeshere"); ' Open Sheet for reading Dim worksheet = workbook.GetWorkSheet("sheetnamegoeshere") $vbLabelText $csharpLabel 讀取單元格值的代碼: // Read from ranges of cells elegantly foreach (var cell in worksheet["A2:A10"]) { Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text); } // Read from ranges of cells elegantly foreach (var cell in worksheet["A2:A10"]) { Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text); } ' Read from ranges of cells elegantly For Each cell In worksheet("A2:A10") Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text) Next cell $vbLabelText $csharpLabel 5.2. Read Excel Files using ClosedXML ClosedXML allows reading of previously created files from your C# application. You can import an excel file using the following code. // Import Excel file from file's path var workbook = new XLWorkbook("SimpleTemplate.xlsx"); // Read worksheet from workbook using sheet number var worksheet1 = workbook.Worksheet(1); // Read cell value from the first sheet var data = worksheet1.Cell("A1").GetValue<string>(); // Display on screen Console.WriteLine(data); // Import Excel file from file's path var workbook = new XLWorkbook("SimpleTemplate.xlsx"); // Read worksheet from workbook using sheet number var worksheet1 = workbook.Worksheet(1); // Read cell value from the first sheet var data = worksheet1.Cell("A1").GetValue<string>(); // Display on screen Console.WriteLine(data); ' Import Excel file from file's path Dim workbook = New XLWorkbook("SimpleTemplate.xlsx") ' Read worksheet from workbook using sheet number Dim worksheet1 = workbook.Worksheet(1) ' Read cell value from the first sheet Dim data = worksheet1.Cell("A1").GetValue(Of String)() ' Display on screen Console.WriteLine(data) $vbLabelText $csharpLabel 6. 處理 Excel 公式 Excel formulas are one of the most important features of working with Excel. Both libraries have a powerful formula calculation engine. They provide the facility to work with formulas and easily apply them to cells. 6.1. 使用 IronXL 處理 Excel 公式 After loading the workbook and worksheet, the following code sample can be used to either make changes to formulas, or be applied to specific cells. The code is as follows: // Set formulas worksheet["A1"].Formula = "=Sum(B8:C12)"; worksheet["B8"].Formula = "=C9/C11"; worksheet["G30"].Formula = "=Max(C3:C7)"; // Force recalculating all formula values in all sheets workbook.EvaluateAll(); // Set formulas worksheet["A1"].Formula = "=Sum(B8:C12)"; worksheet["B8"].Formula = "=C9/C11"; worksheet["G30"].Formula = "=Max(C3:C7)"; // Force recalculating all formula values in all sheets workbook.EvaluateAll(); ' Set formulas worksheet("A1").Formula = "=Sum(B8:C12)" worksheet("B8").Formula = "=C9/C11" worksheet("G30").Formula = "=Max(C3:C7)" ' Force recalculating all formula values in all sheets workbook.EvaluateAll() $vbLabelText $csharpLabel 您還可以檢索公式及其值: // Get the formula's calculated value e.g. "52" string formulaValue = worksheet["G30"].Value; // Get the formula as a string e.g. "Max(C3:C7)" string formulaString = worksheet["G30"].Formula; // Save your changes with updated formulas and calculated values workbook.Save(); // Get the formula's calculated value e.g. "52" string formulaValue = worksheet["G30"].Value; // Get the formula as a string e.g. "Max(C3:C7)" string formulaString = worksheet["G30"].Formula; // Save your changes with updated formulas and calculated values workbook.Save(); ' Get the formula's calculated value e.g. "52" Dim formulaValue As String = worksheet("G30").Value ' Get the formula as a string e.g. "Max(C3:C7)" Dim formulaString As String = worksheet("G30").Formula ' Save your changes with updated formulas and calculated values workbook.Save() $vbLabelText $csharpLabel 6.2. Working with Excel Formulas using ClosedXML ClosedXML does not support all formulas and you'll probably get a nasty error if the formula isn't supported or if there's an error in the formula. Please test your formulas before going to production. Let's have a look at how to use them. // Set formulas worksheet.Cell("A1").Value = "Hello World!"; worksheet.Cell("A2").FormulaA1 = "=MID(A1, 7, 5)"; // You can use Evaluate function to directly calculate formula var sum = worksheet.Evaluate("SUM(B1:B7)"); // Force recalculation worksheet.RecalculateAllFormulas(); // Save Excel file workbook.SaveAs("Formula Calculation.xlsx"); // Set formulas worksheet.Cell("A1").Value = "Hello World!"; worksheet.Cell("A2").FormulaA1 = "=MID(A1, 7, 5)"; // You can use Evaluate function to directly calculate formula var sum = worksheet.Evaluate("SUM(B1:B7)"); // Force recalculation worksheet.RecalculateAllFormulas(); // Save Excel file workbook.SaveAs("Formula Calculation.xlsx"); ' Set formulas worksheet.Cell("A1").Value = "Hello World!" worksheet.Cell("A2").FormulaA1 = "=MID(A1, 7, 5)" ' You can use Evaluate function to directly calculate formula Dim sum = worksheet.Evaluate("SUM(B1:B7)") ' Force recalculation worksheet.RecalculateAllFormulas() ' Save Excel file workbook.SaveAs("Formula Calculation.xlsx") $vbLabelText $csharpLabel The property FormulaA1 is with reference to A1 cell. If you are referencing another cell, you must add it to the Formula property. E.g., FormulaC2. 7. Licensing IronXL 是商業開放式的 C# Excel 函式庫。 它對於開發者是免費的,並且可以始終授權用於商業部署。 Licenses are available for single-project use, single developers, agencies, and global corporations as well as SaaS and OEM redistribution. 所有授權包括 30 天退款保證,一年的產品支持和更新,適用於開發/測試/生產的有效性,並且也是永久性許可證(一次性購買)。 The lite package starts from $799. IronXL License Packages For ClosedXML, applications using this DLL file do not require a separate license either for single-use or commercial use. In order for any solution to work with ClosedXML, just install the publicly available NuGet package "ClosedXML". ClosedXML is licensed under the MIT License. MIT License is a short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code. ClosedXML License 8. Summary and Conclusion 總結 IronXL is a complete library offering everything you need to manipulate an Excel file. IronXL allows developers to read, generate, and edit Excel (and other Spreadsheet files) in .NET applications & websites. It provides a fast and natural approach to work with Excel and other Spreadsheet files in C#. ClosedXML is a .NET library for reading and writing Excel files, which makes it easier for developers to create Excel 2007+ files. It provides a nice object-oriented way to manipulate the files (similar to VBA) without dealing with the hassles of XML Documents. It can be used by any .NET language like C# and Visual Basic (VB). It provides features of OpenXML but still some are not implemented. E.g., Macros, Embedding, and Charts. 結論 IronXL is also helpful in other Excel operations like cell data formats, sorting, cell styling. It can also work with Excel Spreadsheets as System.Data.DataSet and System.Data.DataTable. It supports console, web server, and desktop-based applications. It is also supported on all OS platforms. ClosedXML is a lightweight library for reading, manipulating, and writing Excel applications using OpenXML API which is fast and easier to implement. It helps in all major Excel operations like Page Setup, Freeze Panes, Hyperlinks, Tables, and Conditional Formatting. It has no overhead or low performance issues. In comparison, IronXL and ClosedXML both are fast and accurate when it comes to Excel spreadsheets. They do not need Microsoft Office installation or license when it comes to creating Excel reports. However, IronXL has an advantage over ClosedXML as ClosedXML is not thread-safe. IronXL is easy to use and provides the facility to convert other formats to XLSX, and also from XLSX to other formats such as CSV and HTML. ClosedXML doesn't allow this conversion. This interconversion allows users the flexibility to work with other file formats with ease. Now you can get five Iron products for the price of just two in the lite package for 1 developer($suitePrice) and unlimited for $unlimitedSuitePrice and save up to 60%. [{i:(ClosedXML is a registered trademark of its respective owner. This site is not affiliated with, endorsed by, or sponsored by ClosedXML. 所有產品名稱、徽標和品牌均為其各自所有者的財產。 比較僅供參考,反映撰寫時公開可用的信息。 常見問題解答 如何在C#中不使用Interop讀取Excel文件? 要在C#中不使用Interop讀取Excel文件,您可以使用如IronXL或ClosedXML這樣的庫。這些庫不需要安裝Microsoft Excel,並提供程式化讀取和操作Excel文件的方法。 使用IronXL進行Excel文件操作有哪些好處? IronXL提供了用戶友好的API,支持多種電子表格格式,並提供如排序、樣式和公式處理這樣的操作。它也是線程安全的,允許導出為多種格式,使其成為一個多功能的Excel文件操作工具。 IronXL能處理Excel公式嗎? 是的,IronXL可以處理Excel公式。它允許您在Excel文件中創建、評估和操作公式,提供了對複雜電子表格操作的強大功能。 IronXL支持哪些平台? IronXL支持多個平台,包括.NET Framework、.NET Core、Xamarin和Azure,這使其成為開發人員在不同環境中工作的靈活選擇。 如何在C#中將Excel文件轉換為HTML? 要在C#中將Excel文件轉換為HTML,您可以使用IronXL。它提供了將Excel文件導出為HTML格式的方法,從而便於在網頁上共享和顯示電子表格數據。 IronXL和ClosedXML在格式支持方面有什麼區別? IronXL支持多種格式,包括XLS、XLSX、CSV、TSV和JSON,並允許這些格式之間的轉換。ClosedXML側重於使用OpenXML格式處理Excel 2007+ (.xlsx, .xlsm)文件。 如何在C#專案中安裝IronXL? 您可以通過在Visual Studio中的NuGet包管理器中使用命令Install-Package IronXL.Excel或手動將DLL添加到您的專案中來安裝IronXL。 在商業專案中使用IronXL有任何授權要求嗎? 是的,IronXL需要在商業專案中部署時擁有商業許可證。它提供了包括單一專案、開發者、代理商和公司許可證在內的多種授權選項,並提供30天退款保證。 Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 更新日期 6月 22, 2025 IronXL 使用 C# 開發人員的 Zip 壓縮教程 在本教程中,我們將探索如何在 C# 中使用相對路徑創建 ZIP 文件、從壓縮文件中提取數據及操作 ZIP 文件。 閱讀更多 更新日期 7月 28, 2025 比較三個開放源代碼的 C# Excel 函式庫 本文將探討三個 C# 開源 Excel 程式庫,旨在簡化 .NET 環境中的 Excel 檔案操作 閱讀更多 更新日期 8月 4, 2025 EPPlus 讀取 Excel 到 DataTable C#(IronXL 教程) EPPlus 是一個功能強大的開源庫,用於在 C# 中創建和操作 Excel 文件。它提供了一個簡單直觀的 API,允許開發人員以程式的方式生成、讀取和修改 Excel 試算表。 閱讀更多 IronXL 與 FastExcel for .NET 的比較IronXL 與 GrapeCity Excel Viewer ...
更新日期 6月 22, 2025 IronXL 使用 C# 開發人員的 Zip 壓縮教程 在本教程中,我們將探索如何在 C# 中使用相對路徑創建 ZIP 文件、從壓縮文件中提取數據及操作 ZIP 文件。 閱讀更多
更新日期 8月 4, 2025 EPPlus 讀取 Excel 到 DataTable C#(IronXL 教程) EPPlus 是一個功能強大的開源庫,用於在 C# 中創建和操作 Excel 文件。它提供了一個簡單直觀的 API,允許開發人員以程式的方式生成、讀取和修改 Excel 試算表。 閱讀更多