IronXL 开始 A Guide to Reading and Writing Excel Files in C# Curtis Chau 已更新:六月 10, 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 This article was translated from English: Does it need improvement? Translated View the article in English Reading and creating Excel (XLS, XLSX, and CSV) files in C# and other .NET languages is easy using the IronXL software library from Iron Software. IronXL does not require Excel Interop to be installed on your server. IronXL provides a faster and more intuitive API than Microsoft.Office.Interop.Excel. IronXL works on the following platforms: .NET Framework 4.6.2 and above for Windows and Azure .NET Core 2 and above for Windows, Linux, MacOS, and Azure .NET 5, .NET 6, .NET 7, .NET 8, Mono, Maui, and Xamarin Install IronXL Firstly install IronXL, using our NuGet package or by downloading the DLL. IronXL classes can be found in the IronXL namespace. The easiest way to install IronXL is using the NuGet Package Manager for Visual-Studio: The package name is IronXL.Excel. Install-Package IronXL.Excel https://www.nuget.org/packages/ironxl.excel/ Reading an Excel Document With IronXL, extracting data from an Excel file can be done in just a few lines of code. :path=/static-assets/excel/content-code-examples/get-started/get-started-1.cs using IronXL; // Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV WorkBook workBook = WorkBook.Load("data.xlsx"); WorkSheet workSheet = workBook.WorkSheets.First(); // Select cells easily in Excel notation and return the calculated value, date, text or formula int cellValue = workSheet["A2"].IntValue; // Read from Ranges of cells elegantly. foreach (var cell in workSheet["A2:B10"]) { Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text); } Imports IronXL ' Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV Private workBook As WorkBook = WorkBook.Load("data.xlsx") Private workSheet As WorkSheet = workBook.WorkSheets.First() ' Select cells easily in Excel notation and return the calculated value, date, text or formula Private cellValue As Integer = workSheet("A2").IntValue ' Read from Ranges of cells elegantly. For Each cell In workSheet("A2:B10") Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text) Next cell $vbLabelText $csharpLabel Creating New Excel Documents IronXL offers a quick and easy interface for generating Excel documents using C# or VB.NET. :path=/static-assets/excel/content-code-examples/get-started/get-started-2.cs using IronXL; // Create new Excel WorkBook document. WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX); workBook.Metadata.Author = "IronXL"; // Add a blank WorkSheet WorkSheet workSheet = workBook.CreateWorkSheet("main_sheet"); // 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; // Save the excel file workBook.SaveAs("NewExcelFile.xlsx"); Imports IronXL ' Create new Excel WorkBook document. Private workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX) workBook.Metadata.Author = "IronXL" ' Add a blank WorkSheet Dim workSheet As WorkSheet = workBook.CreateWorkSheet("main_sheet") ' 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 ' Save the excel file workBook.SaveAs("NewExcelFile.xlsx") $vbLabelText $csharpLabel Exporting as CSV, XLS, XLSX, JSON or XML IronXL also allows you to save or export data to a variety of popular structured spreadsheet formats. :path=/static-assets/excel/content-code-examples/get-started/get-started-3.cs // Export to many formats with fluent saving workSheet.SaveAs("NewExcelFile.xls"); workSheet.SaveAs("NewExcelFile.xlsx"); workSheet.SaveAsCsv("NewExcelFile.csv"); workSheet.SaveAsJson("NewExcelFile.json"); workSheet.SaveAsXml("NewExcelFile.xml"); ' Export to many formats with fluent saving workSheet.SaveAs("NewExcelFile.xls") workSheet.SaveAs("NewExcelFile.xlsx") workSheet.SaveAsCsv("NewExcelFile.csv") workSheet.SaveAsJson("NewExcelFile.json") workSheet.SaveAsXml("NewExcelFile.xml") $vbLabelText $csharpLabel Styling Cells and Ranges You can apply formatting to Excel cells and ranges using the IronXL.Range.Style object. :path=/static-assets/excel/content-code-examples/get-started/get-started-4.cs // Set cell's value and styles workSheet["A1"].Value = "Hello World"; workSheet["A2"].Style.BottomBorder.SetColor("#ff6600"); workSheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double; ' Set cell's value and styles workSheet("A1").Value = "Hello World" workSheet("A2").Style.BottomBorder.SetColor("#ff6600") workSheet("A2").Style.BottomBorder.Type = IronXL.Styles.BorderType.Double $vbLabelText $csharpLabel Sorting Ranges With IronXL, you can sort a range of Excel cells easily using the Range object. :path=/static-assets/excel/content-code-examples/get-started/get-started-5.cs using IronXL; WorkBook workBook = WorkBook.Load("test.xls"); WorkSheet workSheet = workBook.WorkSheets.First(); // This is how we get range from Excel worksheet Range range = workSheet["A2:A8"]; // Sort the range in the sheet range.SortAscending(); workBook.Save(); Imports IronXL Private workBook As WorkBook = WorkBook.Load("test.xls") Private workSheet As WorkSheet = workBook.WorkSheets.First() ' This is how we get range from Excel worksheet Private range As Range = workSheet("A2:A8") ' Sort the range in the sheet range.SortAscending() workBook.Save() $vbLabelText $csharpLabel Editing Formulas Modifying an Excel formula is as simple as assigning a value that begins with an "=" sign. The formula will be calculated instantly. :path=/static-assets/excel/content-code-examples/get-started/get-started-6.cs // Set a formula workSheet["A1"].Value = "=SUM(A2:A10)"; // Get the calculated value decimal sum = workSheet["A1"].DecimalValue; ' Set a formula workSheet("A1").Value = "=SUM(A2:A10)" ' Get the calculated value Dim sum As Decimal = workSheet("A1").DecimalValue $vbLabelText $csharpLabel Why Choose IronXL? IronXL offers a developer-friendly API for reading and writing Excel documents in .NET. It works without requiring Microsoft Excel or Excel Interop to be installed on the server, making Excel file handling fast, lightweight, and hassle-free. Moving Forward To explore more features and capabilities, we recommend reviewing the .NET API Reference formatted similarly to MSDN documentation. Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 准备开始了吗? Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:1,686,155 查看许可证