与其他组件比较 IronXL 和 Microsoft Office Interop Excel 的比较 Curtis Chau 已更新:六月 22, 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 MS Office tools such as Word, Excel, PowerPoint, etc. are very popular and widely used across all types of businesses. The creation of report files in Excel file format is a feature many users need in their software applications, and there is now a definitive way to include this feature in software applications using the different libraries available. In this article, we are going to discuss and compare how to work with Microsoft Excel files programmatically in C# using the two most popular libraries, IronXL and Microsoft Office Excel Interop. IronXL and Microsoft Excel Interop both provide the methods to create, edit, and read Excel documents in .NET frameworks. The next question is to decide which C# Excel library is best suited to your project. This article will help you decide on the best option for your applications. Let's first look at what both libraries have to offer, and then move on to the comparison itself. The IronXL Library IronXL is a .NET library that facilitates the reading and editing of Microsoft Excel documents in C#. IronXL.Excel is a standalone .NET software library for reading a wide range of spreadsheet formats. It does not require Microsoft Excel to be installed, nor does it depend on 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 fully supports .NET Core, .NET Framework, Xamarin, Mobile, Linux, macOS, and Azure. IronXL is a leading .NET core and .NET framework Excel spreadsheet library for C#. Docs & Support Online API reference documentation Useful get-started snippets and tutorials at ironsoftware.com NuGet package GitHub repos with more examples Supported by Team Iron IronXL Feature Set 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 — works with Excel formulas. Formulas are recalculated every time a sheet is edited. Ranges — Easy-to-use WorkSheet ["A1:B10"] syntax. Combine and create ranges intuitively. Sorting — Sort Ranges, Columns, and Rows. Styling — Cell visual styles, font, size, background pattern, border, alignment, and number formats. Microsoft Office Excel Interop Objects The Microsoft.Office.Interop.Excel namespace provides methods for interacting with the Microsoft Excel application in C# and Visual Basic. We can create new Excel workbooks, Excel worksheets, display data in existing sheets, modify existing Excel sheet contents, and much more with this namespace. The classes and interfaces in Microsoft.Office.Interop.Excel provide support for interoperability between the COM object model of Microsoft Excel files and managed applications that automate xls or xlsx files. The C# programming language includes capabilities that make working with Microsoft Office Interop API objects easier. The new features include named and optional arguments, a new type called dynamic, and the ability to pass arguments to reference parameters in COM methods as if they were value parameters. This makes it a better choice for working with COM and Interop objects. Note: Microsoft Excel Interop objects need Microsoft Excel to be installed on the computer The rest of the article goes as follows: Create a Console Application IronXL C# Library Installation Microsoft Office Interop Excel Installation Create a New Excel Workbook and Sheet Read Excel files Working with Range of Values in Excel files Working with Excel formulas Licensing Summary and Conclusion 1. Create a Console Application Use the following steps to create a Console Application: Start the Visual Studio 2022 IDE. Click on “Create new project”. On the “Create new project” page, select C# in the language drop-down list, Windows from the Platforms list, and Console from the “Project types” list. Select Console App (.NET Framework) from the project templates displayed. Click Next. In the Additional Information screen, specify the Framework version you would like to use. We will use .NET Framework 4.8 in this example. Click Create to complete the process. Now the project is created and we are almost ready to test the libraries. However, we still need to install and integrate them into our project. Let's install IronXL first. 2. IronXL C# Library Installation You can download and install the IronXL library using the following methods: Using Visual Studio with NuGet packages. Download the NuGet Package directly. Manually install with the DLL. Let’s take a closer look at each one. 2.1. Using Visual Studio with NuGet packages Visual Studio provides the NuGet Package Manager to install NuGet packages in your projects. You can access it through the Project Menu, or by right-clicking your project in the Solution Explorer. Now, from the browse tab -> search for IronXL.Excel -> Install And we are done. 3.2. Using the NuGet Package Manager Console Another way to download and install the Microsoft.Office.Interop.Excel C# Library is to make use of the following steps to install the NuGet package through the Developer Command Prompt. Open the Developer Command Prompt — usually found in the Visual Studio folder. Type the following command: PM> Install-Package Microsoft.Office.Interop.Excel PM> Install-Package Microsoft.Office.Interop.Excel SHELL Press Enter. This will download and install the package. Reload your Visual Studio project and begin using it. 3.3. Add necessary using directives In Solution Explorer, right-click the Program.cs file and then click View Code. Add the following using directive to the top of the code file: using Excel = Microsoft.Office.Interop.Excel; using Excel = Microsoft.Office.Interop.Excel; Imports Excel = Microsoft.Office.Interop.Excel $vbLabelText $csharpLabel 4. Create a New Excel Workbook and Sheet A workbook is an Excel file containing multiple worksheets with rows and columns. Both libraries provide the facility to create a new Excel workbook and sheets. Let's have a look at the code step-by-step. 4.1. A New Excel Workbook and Sheet using IronXL It could not be easier to create a new Excel Workbook using IronXL! Just one line of code! Yes, really. Add the following code to your static void Main function in the Program.cs file: // Create a new workbook in XLSX format WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX); // Create a new workbook in XLSX format WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX); ' Create a new workbook in XLSX format Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX) $vbLabelText $csharpLabel Both XLS (older Excel file version) and XLSX (current and newer file version) file formats can be created with IronXL. And, it’s even easier to create a default Worksheet: // Create a new worksheet var worksheet = workbook.CreateWorkSheet("IronXL Features"); // Create a new worksheet var worksheet = workbook.CreateWorkSheet("IronXL Features"); ' Create a new worksheet Dim worksheet = workbook.CreateWorkSheet("IronXL Features") $vbLabelText $csharpLabel You can now use the worksheet variable to set cell values and do almost everything an Excel file can do. 4.2. A New Excel Workbook and Sheet using Microsoft.Office.Interop.Excel In order to create an Excel file using Microsoft.Office.Interop, Microsoft Excel needs to be installed. So, we need to check if Microsoft Excel is installed on the host machine — if not, it will simply return an exception that needs to be handled. The following example code samples allow you to check for an Excel application installation and create Excel workbooks and sheets: Check Microsoft Excel installed: // Start Excel and get Application object. Excel.Application xlApp = new Excel.Application(); // Check if Excel is installed if (xlApp == null) { Console.WriteLine("Excel is not installed in the system..."); return; } // Start Excel and get Application object. Excel.Application xlApp = new Excel.Application(); // Check if Excel is installed if (xlApp == null) { Console.WriteLine("Excel is not installed in the system..."); return; } ' Start Excel and get Application object. Dim xlApp As New Excel.Application() ' Check if Excel is installed If xlApp Is Nothing Then Console.WriteLine("Excel is not installed in the system...") Return End If $vbLabelText $csharpLabel Create Workbook and Worksheet: // Create Workbook and Worksheet object misValue = System.Reflection.Missing.Value; Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(misValue); Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); // Create Workbook and Worksheet object misValue = System.Reflection.Missing.Value; Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(misValue); Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); ' Create Workbook and Worksheet Dim misValue As Object = System.Reflection.Missing.Value Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Add(misValue) Dim xlWorkSheet As Excel.Worksheet = CType(xlWorkBook.Worksheets.Item(1), Excel.Worksheet) $vbLabelText $csharpLabel So far, looking at both the codes, the advantage lies with IronXL over Office Interop Excel. IronXL uses just one line of code to be able to work with Excel files, with no fuss and additional dependencies. 5. Read Excel Files Both libraries can open and read existing Excel documents. Let's have a look at the sample code. 5.1. Read Excel Files using IronXL 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 Excel file (.xlsx). The following one-line code is used to open the file for reading: // 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 WorkSheet objects. These represent worksheets in the Excel document. If the workbook contains 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 Code for reading the cell values: // 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 Microsoft.Office.Interop.Excel This also uses one line of code to open a workbook and sheet. The code goes as follows: Excel.Application xlApp = new Excel.Application(); // Load WorkBook Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath); // Open Sheet for reading Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); Excel.Application xlApp = new Excel.Application(); // Load WorkBook Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath); // Open Sheet for reading Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); Dim xlApp As New Excel.Application() ' Load WorkBook Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Open(filePath) ' Open Sheet for reading Dim xlWorkSheet As Excel.Worksheet = CType(xlWorkBook.Worksheets.Item(1), Excel.Worksheet) $vbLabelText $csharpLabel Code for reading the cell values: // Get the entire range of cells Excel.Range xlRange = xlWorkSheet.UsedRange; // Reading first 10 rows with two columns for (int rowCount = 1; rowCount <= 10; rowCount++) { string firstValue = Convert.ToString((xlRange.Cells[rowCount, 1] as Excel.Range).Text); string secondValue = Convert.ToString((xlRange.Cells[rowCount, 2] as Excel.Range).Text); Console.WriteLine(firstValue + "\t" + secondValue); } // Get the entire range of cells Excel.Range xlRange = xlWorkSheet.UsedRange; // Reading first 10 rows with two columns for (int rowCount = 1; rowCount <= 10; rowCount++) { string firstValue = Convert.ToString((xlRange.Cells[rowCount, 1] as Excel.Range).Text); string secondValue = Convert.ToString((xlRange.Cells[rowCount, 2] as Excel.Range).Text); Console.WriteLine(firstValue + "\t" + secondValue); } Imports Microsoft.VisualBasic ' Get the entire range of cells Dim xlRange As Excel.Range = xlWorkSheet.UsedRange ' Reading first 10 rows with two columns For rowCount As Integer = 1 To 10 Dim firstValue As String = Convert.ToString((TryCast(xlRange.Cells(rowCount, 1), Excel.Range)).Text) Dim secondValue As String = Convert.ToString((TryCast(xlRange.Cells(rowCount, 2), Excel.Range)).Text) Console.WriteLine(firstValue & vbTab & secondValue) Next rowCount $vbLabelText $csharpLabel However, checking the Excel installation and creating its instance is an integral part. 6. Working with Range of Values in Excel Files 6.1. Working with Range of Values Using IronXL In IronXL, with one line of code, we can get the range of values of particular cells. Then, using a loop, each cell value can be read or edited. The code goes as follows: // Load the workbook and get the first worksheet WorkBook workbook = WorkBook.Load("test.xls"); WorkSheet sheet = workbook.WorkSheets.First(); // This is how we get range from Excel worksheet var range = sheet["A2:A8"]; // This is how we can iterate over our range and read or edit any cell foreach (var cell in range) { Console.WriteLine(cell.Value); } // Load the workbook and get the first worksheet WorkBook workbook = WorkBook.Load("test.xls"); WorkSheet sheet = workbook.WorkSheets.First(); // This is how we get range from Excel worksheet var range = sheet["A2:A8"]; // This is how we can iterate over our range and read or edit any cell foreach (var cell in range) { Console.WriteLine(cell.Value); } ' Load the workbook and get the first worksheet Dim workbook As WorkBook = WorkBook.Load("test.xls") Dim sheet As WorkSheet = workbook.WorkSheets.First() ' This is how we get range from Excel worksheet Dim range = sheet("A2:A8") ' This is how we can iterate over our range and read or edit any cell For Each cell In range Console.WriteLine(cell.Value) Next cell $vbLabelText $csharpLabel 6.2. Working with Range of Values Using Microsoft.Office.Interop.Excel This also uses one line of code to get the range of cell values. The cells can then be updated with the required values. The code goes as follows: Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath); Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); // Get range of values and updating their format var range = xlWorkSheet.get_Range("A1", "D1"); range.NumberFormat = "$0.00"; Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath); Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); // Get range of values and updating their format var range = xlWorkSheet.get_Range("A1", "D1"); range.NumberFormat = "$0.00"; Dim xlApp As New Excel.Application() Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Open(filePath) Dim xlWorkSheet As Excel.Worksheet = CType(xlWorkBook.Worksheets.Item(1), Excel.Worksheet) ' Get range of values and updating their format Dim range = xlWorkSheet.get_Range("A1", "D1") range.NumberFormat = "$0.00" $vbLabelText $csharpLabel 7. Working with Excel Formulas Excel formulas constitute the most important part of working with Excel files. Both libraries provide the facility to work with formulas and apply them to cells with ease. 7.1. Working with Excel Formulas using IronXL After loading the workbook and worksheet, the following code sample can be used to either make changes to formulas or apply them to specific cells. The code goes as follows: // Set Formulas worksheet["A1"].Formula = "Sum(B8:C12)"; worksheet["B8"].Formula = "=C9/C11"; worksheet["G30"].Formula = "Max(C3:C7)"; // Force recalculate 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 recalculate 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 recalculate all formula values in all sheets. workbook.EvaluateAll() $vbLabelText $csharpLabel You can also retrieve formulas and their values. // Get Formulas // 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 Formulas // 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 Formulas ' 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 7.2. Working with Excel Formulas using Microsoft.Office.Interop.Excel Similarly, after loading the workbook and worksheet, the following code can be used to work on Excel formulas. The code sample goes as follows: // Get range of values and updating their format var range1 = xlWorkSheet.get_Range("A1", "D1"); var range2 = xlWorkSheet.get_Range("C2", "C6"); // Set Formula range1.Formula = "=RAND()*100000"; range2.Formula = "=A2 & \" \" & B2"; // Get range of values and updating their format var range1 = xlWorkSheet.get_Range("A1", "D1"); var range2 = xlWorkSheet.get_Range("C2", "C6"); // Set Formula range1.Formula = "=RAND()*100000"; range2.Formula = "=A2 & \" \" & B2"; ' Get range of values and updating their format Dim range1 = xlWorkSheet.get_Range("A1", "D1") Dim range2 = xlWorkSheet.get_Range("C2", "C6") ' Set Formula range1.Formula = "=RAND()*100000" range2.Formula = "=A2 & "" "" & B2" $vbLabelText $csharpLabel 8. Licensing IronXL is an openly commercial C# Excel library. It is free for development and can always be licensed for commercial deployment. Licenses are available for single-project use, single developers, agencies, and global corporations, as well as SaaS and OEM redistribution. All licenses include a 30-day money-back guarantee, one year of product support and updates, validity for dev/staging/production, and also a permanent license (one-time purchase). The Lite package starts from $799. For Microsoft Office Interop Excel, 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 an Office application, Microsoft Office applications must be installed and licensed on the machine where the solution runs. The DLL will always be present on a machine where the Office application is installed, and it will be registered there. At the same time, the primary interop assembly (PIAs) necessary for .NET solutions using the "interop" will also be installed in the GAC. 9. Summary and Conclusion Summary Reading and creating Excel (XLS, XLSX, and CSV) files in C# and all other .NET languages is easy using the IronXL software library from Iron Software. IronXL does not require Excel to be installed on your server or Interop. IronXL provides a faster and more intuitive API than Microsoft.Office.Interop.Excel. IronXL works on .NET Core 2, Framework 4.5, Azure, Mono and, Mobile and Xamarin. Microsoft Office Interop (Excel Automation) is an option when creating/reading Excel files (XLS, XLSX, CSV) from C# or VB.NET applications. It requires all client machines to have the same version of Microsoft Excel installed, and it works only on Windows operating systems. Conclusion Here, IronXL has a clear advantage over Microsoft.Office.Interop.Excel as it does not require the Microsoft Office Excel application to be installed on the local or server machine in order to work. Moreover, when using Excel Automation, Microsoft Excel is loaded in the background, using a lot of MB and loading a large number of files and DLLs in comparison to IronXL. IronXL has a faster and more intuitive API than that of Microsoft.Office.Interop.Excel. Microsoft Office applications (including Excel) were designed as UI applications, and therefore the API is very slow. Microsoft does not recommend using Excel Automation (or any Office Interop) on the server. In this case, with all its robust features, IronXL is the best choice for integration into software applications. 请注意Microsoft Office is a registered trademark of its respective owner. This site is not affiliated with, endorsed by, or sponsored by Microsoft Office. All product names, logos, and brands are property of their respective owners. Comparisons are for informational purposes only and reflect publicly available information at the time of writing. 常见问题解答 如何在 C# 中创建和编辑 Excel 文件而无需安装 Microsoft Excel? 您可以使用 IronXL 在 C# 中创建和编辑 Excel 文件,无需在您的机器上安装 Microsoft Excel。IronXL 提供独立的库,支持各种 Excel 文件格式并支持多个平台。 使用 IronXL 进行 Excel 操作有哪些好处? IronXL 相比 Microsoft Office Interop 提供了多个优点,例如不需要安装 Excel,支持包括 .NET Core 和 Azure 在内的多个平台,并提供更快和更直观的 API。 我可以使用 IronXL 读取和写入不同的 Excel 文件格式吗? 是的,IronXL 支持读取和写入包括 XLS、XLSX、CSV 和 TSV 在内的各种 Excel 文件格式,满足不同应用需求的多样性。 IronXL 是否兼容 .NET Core 及其他环境? IronXL 完全兼容 .NET Core、.NET Framework、Xamarin、移动设备、Linux、macOS 和 Azure,为不同开发环境提供灵活性。 IronXL 如何处理 Excel 公式? IronXL 允许用户轻松设置和检索 Excel 公式。它在工作表被修改时自动重新计算公式,并支持保存更新的公式及其计算值。 使用 IronXL 在项目中时可获得哪些支持? IronXL 提供广泛的支持,包括在线 API 文档、入门代码片段、教程、GitHub 示例以及来自 Team Iron 的专业支持。 在服务器上使用 Microsoft Office Interop 进行 Excel 的局限性是什么? 由于性能问题和需要安装 Microsoft Excel 而导致的维护挑战,Microsoft 不推荐在服务器上使用 Excel 自动化或任何 Office Interop。 IronXL 与传统的 Excel interop 库相比如何提高性能? IronXL 提供比 Microsoft Office Interop Excel 更新更快和更高效的 API,而后者设计为 UI 应用程序,往往较慢且资源密集。 IronXL 适合在软件应用中商业使用吗? 是的,IronXL 可以通过多种许可选项进行商业用途,包括单项目、开发者和全球许可,所有这些都包括 30 天退款保证。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已更新六月 22, 2025 C# 开发人员使用 IronXL 的 Zip 存档教程 在本教程中,我们将探讨如何在C#中创建ZIP文件、从压缩文件中提取数据以及操作ZIP档案,使用相对路径。 阅读更多 已更新七月 28, 2025 比较三个开源 C# Excel 库 本文将探讨三个 C# 开源 Excel 库,旨在简化 .NET 环境中的 Excel 文件操作 阅读更多 已更新八月 4, 2025 EPPlus 读取 Excel 到 DataTable C#(IronXL 教程) EPPlus 是一个强大的开源库,用于在 C# 中创建和操作 Excel 文件。它提供了一个简单直观的 API,使开发人员能够以编程方式生成、读取和修改 Excel 电子表格。 阅读更多 IronXL与Epplus之间的比较IronXL 和 Aspose Cells 的比较
已更新八月 4, 2025 EPPlus 读取 Excel 到 DataTable C#(IronXL 教程) EPPlus 是一个强大的开源库,用于在 C# 中创建和操作 Excel 文件。它提供了一个简单直观的 API,使开发人员能够以编程方式生成、读取和修改 Excel 电子表格。 阅读更多