与其他组件比较 IronXL 和 NPOI 的比较 Curtis Chau 已更新:七月 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 As you may know, Excel is probably the most used spreadsheet application in the world. Users include both developers and the general population, but it is developers who will probably be more engaged by this article. Initially, there weren’t many options for developers to work with Excel across their various applications. However, there were VBA (Visual Basic for Applications) built into the Office Suite with which you could manipulate any Office product according to your needs. Developers used VBA in Excel to customize their interactions with Excel and its data, even its visual capabilities. But, eventually this wasn’t enough. Developers wanted more, and so they began using Office.Interop. In this article, I will compare two very good Excel libraries that do not depend on Office.Interop to work with Excel. They are: IronXL NPOI You will see and learn what each Excel library can do, and then compare the two through code examples and step-by-step tutorials. Although this article cannot cover all features of both Excel libraries, it will explain the most common and most popular uses of each. How to Use NPOI for Excel in C# Install C# library to read Excel with NPOI Instantiate XSSFWorkbook object to store Excel file Get specific worksheet with GetSheetAt method Access each row by passing row index to GetRow method Access each cell in the row with GetCell method What is NPOI? NPOI is the .NET version of the POI Java project at http://poi.apache.org/. POI is an open-source project that can help you read/write xls, doc, ppt files. It has a wide range of applications. For example, you can use it to: Generate an Excel report without Microsoft Office suite installed on your server and more efficient than calling Microsoft Excel ActiveX in the background Extract text from Office documents to help you implement a full-text indexing feature (most of the time this feature is used to create search engines) Extract images from Office documents Generate Excel sheets that contain formulas NPOI and Excel NPOI is a C# port of the POI Java project by Apache. It is free and open-source. Also, it doesn't need Interop, meaning that users will not need to have Excel installed to have the developer's app work with it. IronXL and Excel IronXL is an Excel API for VB and C#. With IronXL you can read, edit, and create Excel spreadsheet files in .NET. Common Library Features for the NPOI and IronXL Excel Libraries NPOI IronXL Cell Ranges Cell Ranges Cell styling (Border, Color, Fill, Font, Number, Alignments) Cell visual styles Font, Size, Background pattern, Border, Alignment, and Number formats. Formula calculation Formulas Data Validation Data Validation Conditional formatting Conditional formatting Images Images Charts Charts Table 1 - Feature Comparison Installation of IronXL and NPOI You can install both libraries by downloading them manually, via NuGet, or with the NuGet Package Manager in Visual Studio. Here is a quick overview. NPOI Installation Installing NPOI with NuGet To install NPOI through NuGet, open the Visual Studio developer command prompt and enter the following: Install-Package NPOI -Version x.x.x Figure 1 - NuGet NPOI Installation Visual Studio NuGet Package Manager and NPOI Use the following steps to install IronXL or NPOI via the NuGet Package Manager in Visual Studio: Right-click the project in the Solution Explorer Select Manage NuGet Packages Browse for your Package Click Install Figure 2 - NuGet Package Manager for NPOI IronXL Installation Downloading IronXL To download IronXL, navigate to the following URL and click the "Download" button. Figure 3 - Download IronXL Installing IronXL with NuGet To install IronXL through NuGet, open the Visual Studio developer command prompt and enter the following: Install-Package IronXL.Excel -Version x.x.x Figure 4 - NuGet IronXL Installation Visual Studio NuGet Package Manager and IronXL Use the following steps to install IronXL via the NuGet Package Manager in Visual Studio: Right-click the project in the Solution Explorer Select Manage NuGet Packages Browse for your Package Click Install Figure 5 - NuGet Package Manager for IronXL Reading from and writing to an Excel file with NPOI and IronXL Reading an Excel file with NPOI The following code demonstrates how to read an Excel file and display its contents with NPOI. Add the following code and include the necessary namespaces: using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System.Data; using System.IO; using System.Collections.Generic; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System.Data; using System.IO; using System.Collections.Generic; Imports NPOI.SS.UserModel Imports NPOI.XSSF.UserModel Imports System.Data Imports System.IO Imports System.Collections.Generic $vbLabelText $csharpLabel The below code reads an existing Excel file and displays it inside a data grid view. public void ReadExcelNPOI() { DataTable dtTable = new DataTable(); List<string> lstRows = new List<string>(); ISheet objWorksheet; string strPath = @"c:\temp\NPOI_Test.XLSX"; // Use FileStream to open the Excel file using (var fStream = new FileStream(strPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { fStream.Position = 0; XSSFWorkbook objWorkbook = new XSSFWorkbook(fStream); objWorksheet = objWorkbook.GetSheetAt(0); IRow objHeader = objWorksheet.GetRow(0); int countCells = objHeader.LastCellNum; // Add columns to the DataTable based on the header row of Excel for (int j = 0; j < countCells; j++) { ICell objCell = objHeader.GetCell(j); if (objCell == null || string.IsNullOrWhiteSpace(objCell.ToString())) continue; { dtTable.Columns.Add(objCell.ToString()); } } // Add rows to the DataTable, looping through each row and cell for (int i = (objWorksheet.FirstRowNum + 1); i <= objWorksheet.LastRowNum; i++) { IRow objRow = objWorksheet.GetRow(i); if (objRow == null || objRow.Cells.All(d => d.CellType == CellType.Blank)) continue; for (int j = objRow.FirstCellNum; j < countCells; j++) { ICell cell = objRow.GetCell(j); if (cell != null && !string.IsNullOrEmpty(cell.ToString()) && !string.IsNullOrWhiteSpace(cell.ToString())) { lstRows.Add(cell.ToString()); } } if (lstRows.Count > 0) dtTable.Rows.Add(lstRows.ToArray()); lstRows.Clear(); } } // Assuming dataGridView1 is a DataGridView control on a Form dataGridView1.DataSource = dtTable; } private void button1_Click(object sender, EventArgs e) { ReadExcelNPOI(); } public void ReadExcelNPOI() { DataTable dtTable = new DataTable(); List<string> lstRows = new List<string>(); ISheet objWorksheet; string strPath = @"c:\temp\NPOI_Test.XLSX"; // Use FileStream to open the Excel file using (var fStream = new FileStream(strPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { fStream.Position = 0; XSSFWorkbook objWorkbook = new XSSFWorkbook(fStream); objWorksheet = objWorkbook.GetSheetAt(0); IRow objHeader = objWorksheet.GetRow(0); int countCells = objHeader.LastCellNum; // Add columns to the DataTable based on the header row of Excel for (int j = 0; j < countCells; j++) { ICell objCell = objHeader.GetCell(j); if (objCell == null || string.IsNullOrWhiteSpace(objCell.ToString())) continue; { dtTable.Columns.Add(objCell.ToString()); } } // Add rows to the DataTable, looping through each row and cell for (int i = (objWorksheet.FirstRowNum + 1); i <= objWorksheet.LastRowNum; i++) { IRow objRow = objWorksheet.GetRow(i); if (objRow == null || objRow.Cells.All(d => d.CellType == CellType.Blank)) continue; for (int j = objRow.FirstCellNum; j < countCells; j++) { ICell cell = objRow.GetCell(j); if (cell != null && !string.IsNullOrEmpty(cell.ToString()) && !string.IsNullOrWhiteSpace(cell.ToString())) { lstRows.Add(cell.ToString()); } } if (lstRows.Count > 0) dtTable.Rows.Add(lstRows.ToArray()); lstRows.Clear(); } } // Assuming dataGridView1 is a DataGridView control on a Form dataGridView1.DataSource = dtTable; } private void button1_Click(object sender, EventArgs e) { ReadExcelNPOI(); } Public Sub ReadExcelNPOI() Dim dtTable As New DataTable() Dim lstRows As New List(Of String)() Dim objWorksheet As ISheet Dim strPath As String = "c:\temp\NPOI_Test.XLSX" ' Use FileStream to open the Excel file Using fStream = New FileStream(strPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) fStream.Position = 0 Dim objWorkbook As New XSSFWorkbook(fStream) objWorksheet = objWorkbook.GetSheetAt(0) Dim objHeader As IRow = objWorksheet.GetRow(0) Dim countCells As Integer = objHeader.LastCellNum ' Add columns to the DataTable based on the header row of Excel For j As Integer = 0 To countCells - 1 Dim objCell As ICell = objHeader.GetCell(j) If objCell Is Nothing OrElse String.IsNullOrWhiteSpace(DirectCast(objCell, Object).ToString()) Then Continue For End If If True Then dtTable.Columns.Add(DirectCast(objCell, Object).ToString()) End If Next j ' Add rows to the DataTable, looping through each row and cell For i As Integer = (objWorksheet.FirstRowNum + 1) To objWorksheet.LastRowNum Dim objRow As IRow = objWorksheet.GetRow(i) If objRow Is Nothing OrElse objRow.Cells.All(Function(d) d.CellType = CellType.Blank) Then Continue For End If For j As Integer = objRow.FirstCellNum To countCells - 1 Dim cell As ICell = objRow.GetCell(j) If cell IsNot Nothing AndAlso Not String.IsNullOrEmpty(DirectCast(cell, Object).ToString()) AndAlso Not String.IsNullOrWhiteSpace(DirectCast(cell, Object).ToString()) Then lstRows.Add(DirectCast(cell, Object).ToString()) End If Next j If lstRows.Count > 0 Then dtTable.Rows.Add(lstRows.ToArray()) End If lstRows.Clear() Next i End Using ' Assuming dataGridView1 is a DataGridView control on a Form dataGridView1.DataSource = dtTable End Sub Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) ReadExcelNPOI() End Sub $vbLabelText $csharpLabel Reading an Excel file with IronXL The following code demonstrates how to read an Excel file and display it inside a data grid view with IronXL. Add the following code and include the namespace: using IronXL; using System.Data; using IronXL; using System.Data; Imports IronXL Imports System.Data $vbLabelText $csharpLabel Notice the inclusion of IronXL. This is necessary for IronXL to work. Add the next few lines: private void button2_Click(object sender, EventArgs e) { // Load the Excel workbook string strPath = @"c:\temp\NPOI_Test.XLSX"; WorkBook workbook = WorkBook.Load(strPath); // Access the default worksheet WorkSheet sheet = workbook.DefaultWorkSheet; // Convert the worksheet to a DataTable var dtTable = sheet.ToDataTable(true); // Assuming dataGridView1 is a DataGridView control on a Form dataGridView1.DataSource = dtTable; } private void button2_Click(object sender, EventArgs e) { // Load the Excel workbook string strPath = @"c:\temp\NPOI_Test.XLSX"; WorkBook workbook = WorkBook.Load(strPath); // Access the default worksheet WorkSheet sheet = workbook.DefaultWorkSheet; // Convert the worksheet to a DataTable var dtTable = sheet.ToDataTable(true); // Assuming dataGridView1 is a DataGridView control on a Form dataGridView1.DataSource = dtTable; } Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) ' Load the Excel workbook Dim strPath As String = "c:\temp\NPOI_Test.XLSX" Dim workbook As WorkBook = WorkBook.Load(strPath) ' Access the default worksheet Dim sheet As WorkSheet = workbook.DefaultWorkSheet ' Convert the worksheet to a DataTable Dim dtTable = sheet.ToDataTable(True) ' Assuming dataGridView1 is a DataGridView control on a Form dataGridView1.DataSource = dtTable End Sub $vbLabelText $csharpLabel As you can see, this all adds up to a lot less work. Conclusion So why IronXL? As you see, IronXL has an easy to use API, that keeps your code clean. Libraries that use IronXL boast clean, readable code which is an investment that pays off by reducing time and effort needed to understand what the code does, allowing it to be changed without breaking things. IronXL is more intuitive all-around while being as powerful as NPOI. And, of course, with a license key you get access to the most important advantage of paid software: you get our professional support, consistent and reliable update schedules, and ability to request features from Iron Software support agents who are happy to assist you. This is something open source just can’t provide. Downloads This project is available on GitHub: IronXL vs NPOI Example 请注意NPOI is a registered trademark of its respective owner. This site is not affiliated with, endorsed by, or sponsored by NPOI. 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. 常见问题解答 使用 Office.Interop 进行 C# 中 Excel 操作的替代方案是什么? IronXL 是 C# 中 Excel 操作使用 Office.Interop 的替代方案。它提供了一个用户友好的 API,使开发人员可以创建、读取和编辑 Excel 文件,而无需安装 Microsoft Office。 如何在 C# 中将 Excel 文件转换为 PDF? 您可以通过在 C# 中使用 IronXL 加载 Excel 工作簿来将 Excel 文件转换为 PDF,然后使用 WorkBook.SaveAs 方法将文件保存为 PDF 格式。 使用 IronXL 进行 Excel 文件操作的主要好处是什么? IronXL 提供了更直观的 API,用于更轻松的代码维护、专业支持、定期更新,以及无需安装 Microsoft Office 即可处理 Excel 文件,这使其成为开发人员的强大选择。 可以通过 NuGet 安装 IronXL 吗? 是的,可以通过 NuGet 安装 IronXL。打开 Visual Studio 开发者命令提示符,输入 Install-Package IronXL.Excel -Version x.x.x。 使用 IronXL 可以执行哪些常见的 Excel 相关任务? 使用 IronXL,开发人员可以执行读取和写入 Excel 文件、样式化单元格、使用公式、验证数据、应用条件格式以及处理图像和图表等任务。 如何在没有 Microsoft Office 的情况下用 C# 读取 Excel 文件? 使用 IronXL,您可以通过 WorkBook.Load 加载工作簿来读取 C# 中的 Excel 文件,访问工作表,并使用直接的方法遍历行和单元格。 为什么选择 IronXL 而不是开源替代方案进行 Excel 操作? IronXL 提供了更用户友好的 API、专业支持、定期更新和持续的功能请求,这些优势比开源替代方案如 NPOI 更为明显。 NPOI 与 IronXL 在 Excel 文件操作上有什么不同? NPOI 是一个基于 Apache POI 项目的开源库,用于在没有 Microsoft Office 的情况下处理 Excel 文件。IronXL 通过提供更直观的 API、专业支持和定期更新,使其适合商业应用。 使用 IronXL 将 Excel 工作表转换为 DataTable 的过程是什么? 要使用 IronXL 将 Excel 工作表转换为 DataTable,加载工作簿并使用可用方法将其转换为 DataTable 以供进一步使用。 IronXL 如何在 .NET 应用程序中增强 Excel 文件管理? IronXL 通过提供干净直观的 API,方便执行读取、编辑和创建 Excel 文件的任务,在 .NET 应用程序中增强了 Excel 文件管理,而无需依赖于 Microsoft Office 的安装。 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 和 CsvHelper 的比较IronXL 和 Aspose Cells 的比较
已更新八月 4, 2025 EPPlus 读取 Excel 到 DataTable C#(IronXL 教程) EPPlus 是一个强大的开源库,用于在 C# 中创建和操作 Excel 文件。它提供了一个简单直观的 API,使开发人员能够以编程方式生成、读取和修改 Excel 电子表格。 阅读更多