IronXL 開始使用 A Guide to Reading and Writing Excel Files in C# Curtis Chau 更新日期:6月 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 使用來自 Iron Software 的 IronXL 軟體庫在 C# 和其他 .NET 語言中讀取和創建 Excel (XLS, XLSX 和 CSV) 文件非常簡單。 IronXL 不需要在您的伺服器上安裝 Excel Interop。 IronXL 提供比 Microsoft.Office.Interop.Excel 更快速且直觀的 API。 IronXL 適用於以下平台: Windows 和 Azure 上的 .NET Framework 4.6.2 及更高版本 Windows、Linux、MacOS 和 Azure 上的 .NET Core 2 及更高版本 .NET 5、.NET 6、.NET 7、.NET 8、Mono、Maui 和 Xamarin 安裝 IronXL Firstly install IronXL, using our NuGet package or by downloading the DLL. IronXL classes can be found in the IronXL namespace. 最簡單的安裝 IronXL 的方式是使用 Visual-Studio 的 NuGet 套件管理器: 套件名稱是 IronXL.Excel。 Install-Package IronXL.Excel https://www.nuget.org/packages/ironxl.excel/ 讀取 Excel 文件 使用 IronXL,從 Excel 文件中提取數據可以在短短幾行代碼內完成。 :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 創建新的 Excel 文件 IronXL 提供了一個快速簡便的介面,用於使用 C# 或 VB.NET 生成 Excel 文件。 :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 導出為 CSV、XLS、XLSX、JSON 或 XML IronXL 還允許您將數據保存或導出到各種流行的結構化電子表格格式。 :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 設置儲存格和範圍的樣式 您可以使用 IronXL.Range.Style 物件將格式應用到 Excel 儲存格和範圍。 :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 排序範圍 使用 IronXL,您可以輕鬆使用 Range 物件對一個 Excel 儲存格範圍進行排序。 :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 編輯公式 修改 Excel 公式就像賦值一個以 "=" 符號開頭的值一樣簡單。 公式將立即被計算。 :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 為什麼選擇 IronXL? IronXL 提供了一個對開發人員友好的 API,用於在 .NET 中讀寫 Excel 文件。 它不需要在伺服器上安裝 Microsoft Excel 或 Excel Interop,使 Excel 文件處理快速、輕量且無憂無慮。 前進的方向 要探索更多功能和能力,我們建議查看.NET API 參考,格式類似於 MSDN 文檔。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 準備好開始了嗎? Nuget 下載 1,686,155 | 版本: 2025.11 剛剛發布 免費 NuGet 下載 總下載量:1,686,155 查看許可證