使用 IRONXL 在 C# 中无需 Microsoft Office 处理 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 Introduction to IronXL IronXL library serves as an alternative to traditional Excel Interop in C#, offering a more streamlined and efficient approach to handling Excel files. It is a useful Excel API for developers managing Microsoft Excel data in .NET applications, functioning independently of Microsoft Office. It offers a straightforward way to manage Excel files in various formats like XLSX, XLS, and CSV. Why Choose IronXL? The absence of a requirement for MS Office installation on the server makes IronXL an optimal solution in various server environments. This makes it a go-to solution for server environments where installing Microsoft Office is not feasible due to licensing or server performance constraints. Key Features of IronXL The key features of IronXL are as follows: Reading and Writing Excel Files: IronXL can create and manipulate Excel files. It supports both classic XLS and modern XLSX file formats, ensuring compatibility with different versions of Excel. Exporting Data: IronXL can export data to and from Excel files to other formats efficiently like XML files, CSV, and other formats. Support for .NET Core and Framework: IronXL works seamlessly with both .NET Core and .NET Framework, making it a versatile choice for various development environments. Ease of Use: With IronXL, developers can perform complex Excel operations with minimal lines of code, enhancing productivity and efficiency. Installation and Setup To start using IronXL, you need to install the NuGet package. Here's how you can do it: Via NuGet Package Manager: Search for IronXL in the NuGet Packages Manager and install it. Using Package Manager Console: Run the command Install-Package IronXL.Excel in the Package Manager Console in Visual Studio. Working with IronXL IronXL empowers C# developers with the ability to interact with Excel files efficiently. This section will delve into the various operations you can perform with IronXL, providing detailed guidance and code examples. Creating a New Excel File Creating a new Excel file is a common task in data manipulation and reporting. With IronXL, you can effortlessly create Excel files with a few lines of code, add worksheets, and populate them with data. The following code shows how we can create an Excel file using IronXL: using IronXL; // Initialize a new Workbook var workbook = new WorkBook(); // Add a new Worksheet named "Sales Data" var worksheet = workbook.CreateWorkSheet("Sales Data"); // Populate the Worksheet with data worksheet["A1"].Value = "Month"; worksheet["B1"].Value = "Sales"; worksheet["A2"].Value = "January"; worksheet["B2"].Value = 5000; worksheet["A3"].Value = "February"; worksheet["B3"].Value = 6000; // Save the Workbook as an Excel file workbook.SaveAs("SalesReport.xlsx"); using IronXL; // Initialize a new Workbook var workbook = new WorkBook(); // Add a new Worksheet named "Sales Data" var worksheet = workbook.CreateWorkSheet("Sales Data"); // Populate the Worksheet with data worksheet["A1"].Value = "Month"; worksheet["B1"].Value = "Sales"; worksheet["A2"].Value = "January"; worksheet["B2"].Value = 5000; worksheet["A3"].Value = "February"; worksheet["B3"].Value = 6000; // Save the Workbook as an Excel file workbook.SaveAs("SalesReport.xlsx"); Imports IronXL ' Initialize a new Workbook Private workbook = New WorkBook() ' Add a new Worksheet named "Sales Data" Private worksheet = workbook.CreateWorkSheet("Sales Data") ' Populate the Worksheet with data Private worksheet("A1").Value = "Month" Private worksheet("B1").Value = "Sales" Private worksheet("A2").Value = "January" Private worksheet("B2").Value = 5000 Private worksheet("A3").Value = "February" Private worksheet("B3").Value = 6000 ' Save the Workbook as an Excel file workbook.SaveAs("SalesReport.xlsx") $vbLabelText $csharpLabel This code creates a workbook, adds a worksheet, and populates it with sales data before saving it as an Excel file. IronXL supports standard Excel notation, enabling developers to reference cells like 'A1' for reading and manipulating data. Reading Excel Files Reading and extracting information from Excel files is crucial for data processing applications. IronXL allows you to open existing Excel files and read their content without Excel interop. Here’s a code example: using IronXL; // Load an existing Excel file var workbook = WorkBook.Load("FinancialData.xlsx"); // Access a specific worksheet by name var worksheet = workbook.GetWorkSheet("Quarterly Report"); // Read values from specific cells string quarter = worksheet["A2"].StringValue; double revenue = worksheet["B2"].DoubleValue; double expenses = worksheet["C2"].DoubleValue; // You can also iterate over rows and columns if needed foreach (var row in worksheet.Rows) { foreach (var cell in row) { Console.WriteLine(cell.Value); } } using IronXL; // Load an existing Excel file var workbook = WorkBook.Load("FinancialData.xlsx"); // Access a specific worksheet by name var worksheet = workbook.GetWorkSheet("Quarterly Report"); // Read values from specific cells string quarter = worksheet["A2"].StringValue; double revenue = worksheet["B2"].DoubleValue; double expenses = worksheet["C2"].DoubleValue; // You can also iterate over rows and columns if needed foreach (var row in worksheet.Rows) { foreach (var cell in row) { Console.WriteLine(cell.Value); } } Imports IronXL ' Load an existing Excel file Private workbook = WorkBook.Load("FinancialData.xlsx") ' Access a specific worksheet by name Private worksheet = workbook.GetWorkSheet("Quarterly Report") ' Read values from specific cells Private quarter As String = worksheet("A2").StringValue Private revenue As Double = worksheet("B2").DoubleValue Private expenses As Double = worksheet("C2").DoubleValue ' You can also iterate over rows and columns if needed For Each row In worksheet.Rows For Each cell In row Console.WriteLine(cell.Value) Next cell Next row $vbLabelText $csharpLabel This code demonstrates how to load an Excel file, access a specific worksheet, and read values from cells, including iterating over rows and columns. Writing Data to Excel Files Modifying Excel files is a frequent requirement. IronXL allows you to write and update data in Excel files with ease. The following example illustrates adding and updating data in an Excel file: using IronXL; // Load an existing Excel file var workbook = WorkBook.Load("EmployeeData.xlsx"); var worksheet = workbook.DefaultWorkSheet; // Adding new data to cells worksheet["A4"].Value = "John Doe"; worksheet["B4"].Value = "Sales"; worksheet["C4"].Value = 45000; // Updating existing data worksheet["C2"].Value = 50000; // Update salary of an existing employee // Save the changes to a new file workbook.SaveAs("UpdatedEmployeeData.xlsx"); using IronXL; // Load an existing Excel file var workbook = WorkBook.Load("EmployeeData.xlsx"); var worksheet = workbook.DefaultWorkSheet; // Adding new data to cells worksheet["A4"].Value = "John Doe"; worksheet["B4"].Value = "Sales"; worksheet["C4"].Value = 45000; // Updating existing data worksheet["C2"].Value = 50000; // Update salary of an existing employee // Save the changes to a new file workbook.SaveAs("UpdatedEmployeeData.xlsx"); Imports IronXL ' Load an existing Excel file Private workbook = WorkBook.Load("EmployeeData.xlsx") Private worksheet = workbook.DefaultWorkSheet ' Adding new data to cells Private worksheet("A4").Value = "John Doe" Private worksheet("B4").Value = "Sales" Private worksheet("C4").Value = 45000 ' Updating existing data Private worksheet("C2").Value = 50000 ' Update salary of an existing employee ' Save the changes to a new file workbook.SaveAs("UpdatedEmployeeData.xlsx") $vbLabelText $csharpLabel In this example, new data is added to an Excel file, and existing data is updated, showcasing the flexibility of IronXL in data manipulation. Exporting Excel Data Exporting data from Excel files to other formats is often required for data analysis and reporting. IronXL simplifies this process. Here’s how you can export data from an Excel file to a CSV format: using IronXL; // Load the Excel file var workbook = WorkBook.Load("ProjectData.xlsx"); // Export the entire Workbook or a specific Worksheet to CSV workbook.SaveAsCsv("ProjectData.csv"); // You can also export to other formats like JSON workbook.SaveAsJson("ProjectData.json"); using IronXL; // Load the Excel file var workbook = WorkBook.Load("ProjectData.xlsx"); // Export the entire Workbook or a specific Worksheet to CSV workbook.SaveAsCsv("ProjectData.csv"); // You can also export to other formats like JSON workbook.SaveAsJson("ProjectData.json"); Imports IronXL ' Load the Excel file Private workbook = WorkBook.Load("ProjectData.xlsx") ' Export the entire Workbook or a specific Worksheet to CSV workbook.SaveAsCsv("ProjectData.csv") ' You can also export to other formats like JSON workbook.SaveAsJson("ProjectData.json") $vbLabelText $csharpLabel This code snippet demonstrates the conversion of Excel files to CSV and JSON formats, highlighting IronXL's versatility in handling different file formats. Advanced Features of IronXL IronXL isn't just about basic Excel operations; it comes packed with advanced features that cater to complex data manipulation and formatting needs. Let's explore some of these capabilities in more detail. Working with Data Tables Converting between Excel sheets and .NET data tables is a feature that enhances the flexibility of data handling in IronXL. This functionality is particularly useful for scenarios involving bulk data operations or when interfacing with databases. using IronXL; using System.Data; // Load an existing Excel file var workbook = WorkBook.Load("EmployeeRecords.xlsx"); var worksheet = workbook.DefaultWorkSheet; // Convert the Worksheet to a DataTable DataTable dataTable = worksheet.ToDataTable(true); using IronXL; using System.Data; // Load an existing Excel file var workbook = WorkBook.Load("EmployeeRecords.xlsx"); var worksheet = workbook.DefaultWorkSheet; // Convert the Worksheet to a DataTable DataTable dataTable = worksheet.ToDataTable(true); Imports IronXL Imports System.Data ' Load an existing Excel file Private workbook = WorkBook.Load("EmployeeRecords.xlsx") Private worksheet = workbook.DefaultWorkSheet ' Convert the Worksheet to a DataTable Private dataTable As DataTable = worksheet.ToDataTable(True) $vbLabelText $csharpLabel Performance Optimization for Large Excel Files Handling large Excel files efficiently is a key feature of IronXL, making it suitable for enterprise-level applications dealing with substantial amounts of data. IronXL is designed to minimize memory usage and optimize performance when working with large Excel files. It achieves this through efficient data handling algorithms and by allowing selective reading of worksheets and cells, thereby reducing the load on system resources. Handling Excel File Formats IronXL supports various Excel file formats, ensuring compatibility across different versions of Excel and other spreadsheet software. using IronXL; // Creating a workbook var workbook = new WorkBook(); // You can save the Workbook in various formats workbook.SaveAs("ExcelDocument.xlsx"); // XLSX format workbook.SaveAs("ExcelDocument.xls"); // XLS format workbook.SaveAsCsv("ExcelDocument.csv"); // CSV format using IronXL; // Creating a workbook var workbook = new WorkBook(); // You can save the Workbook in various formats workbook.SaveAs("ExcelDocument.xlsx"); // XLSX format workbook.SaveAs("ExcelDocument.xls"); // XLS format workbook.SaveAsCsv("ExcelDocument.csv"); // CSV format Imports IronXL ' Creating a workbook Private workbook = New WorkBook() ' You can save the Workbook in various formats workbook.SaveAs("ExcelDocument.xlsx") ' XLSX format workbook.SaveAs("ExcelDocument.xls") ' XLS format workbook.SaveAsCsv("ExcelDocument.csv") ' CSV format $vbLabelText $csharpLabel This example shows how IronXL can create and save documents in XLSX, XLS, and CSV formats, providing flexibility in how data is stored and shared. Conclusion IronXL is a powerful C# library for working with Excel files without the need for Microsoft Office. Its ability to read, write, and export Excel files, combined with its ease of use and support for both .NET Core and .NET Framework, makes it an excellent choice for developers dealing with Excel data in their applications. Whether you are creating new Excel documents, processing existing files, or exporting data to different formats, IronXL offers a robust and efficient solution. For more detailed documentation, code examples, and support, visit the IronXL official website or check out their GitHub repository. With IronXL, managing Excel files in C# becomes a streamlined and hassle-free process. IronXL offers a free trial for users to explore its features and capabilities before making a purchase. This trial period allows developers to test the library in their environments, ensuring it meets their specific needs for Excel file manipulation in .NET applications. After the trial, licenses for IronXL start at $799, providing full access to all its advanced features and functionalities. 常见问题解答 IronXL如何在没有Microsoft Office的情况下工作? IronXL被设计用于在.NET应用程序中管理Excel文件,而无需Microsoft Office。它提供了一个简化的API,使开发人员能够在不依赖Office Interop的情况下执行Excel操作。 在服务器环境中使用IronXL进行Excel操作有什么好处? IronXL非常适合服务器环境,因为它消除了对Microsoft Office的需求,从而避免了许可或性能问题。它允许在不能安装Office的环境中无缝操作Excel文件。 我如何使用IronXL读取和写入Excel文件? 使用IronXL,您可以通过初始化一个新的Workbook,访问Worksheet对象,并使用方法从Excel单元读取或写入数据来读取和写入Excel文件。 IronXL支持哪些Excel文件格式? IronXL支持多种Excel文件格式,包括XLSX、XLS和CSV。它还允许将数据导出为XML和JSON格式,增强了数据互操作性。 我如何在我的.NET项目中安装IronXL? 您可以通过在NuGet包管理器中搜索'IronXL',或在包管理器控制台中执行命令Install-Package IronXL.Excel来安装IronXL。 IronXL是否兼容.NET Core和.NET Framework? 是的,IronXL兼容.NET Core和.NET Framework,使其成为各种开发环境中的灵活选项。 IronXL 能高效处理大型 Excel 文件吗? IronXL经过优化,能够通过最小化内存使用和提供数据的选择性读取和写入方法来有效地处理大型Excel文件。 如何使用IronXL将Excel数据导出为其他格式? IronXL允许您将Excel数据导出为CSV和JSON格式,这有助于跨不同平台的数据分析和报告。 IronXL为Excel数据操作提供了哪些高级功能? IronXL提供了将Excel表格转换为.NET数据表的高级功能,并通过有效的数据处理算法优化大数据集的性能。 IronXL 是否提供免费试用版? 是的,IronXL为新用户提供免费试用。这使您可以在决定购买许可证以获得完整访问权限之前探索其功能。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已发布十月 27, 2025 如何在 C# 中创建 Excel 数据透视表 学习通过这个清晰的分步指南使用C# Interop和IronXL在Excel中创建数据透视表。 阅读更多 已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多 已发布十月 27, 2025 如何在.NET Core中使用CSV Reader与IronXL 学习通过实际示例有效地使用IronXL作为.NET Core的CSV读取器。 阅读更多 C# Excel 自动化(开发者教程)如何在 C# 中无需 oledb 将 Ex...
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多