使用 IRONXL 如何在 C# 中開啟 Excel 檔案 Jordi Bardia 更新:2026年1月19日 下載 IronXL NuGet 下載 DLL 下載 開始免費試用 LLM副本 LLM副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 IronXL讓 C# 開發人員無需安裝 Microsoft Office 即可開啟、讀取和操作 Excel 檔案。 只需使用 WorkBook.Load() 載入工作簿,存取工作表,並使用 sheet["A1"] 等直覺式語法讀取儲存格值。 本教學探討如何在 C# 專案中使用IronXL開啟和讀取 Excel 文件,為初級開發人員提供處理 Excel 資料的全面範例和最佳實務。 IronXL Excel Library是什麼? IronXL是一個.NET函式庫,它優先考慮易用性、準確性和速度。 它可以幫助您有效率地開啟、讀取、建立和編輯Excel 文件,而無需 MS Office Interop,因此對於希望在 C# 中使用 Excel 而無需 Interop 的開發人員來說,這是一個實用的選擇。 IronXL與所有.NET Framework 以及Linux 、 macOS 、 Docker 、 Azure和AWS相容。 您可以使用它來建立控制台應用程式、Web 應用程式和桌面應用程序,例如Blazor和.NET MAUI,用於開發現代 Web 應用程式。它支援不同的工作簿格式,例如 XLS 和 XLSX 檔案、XSLT 和 XLSM、CSV 和 TSV。 IronXL的主要特色是什麼? 使用LoadSpreadsheets開啟、讀取和搜尋 XLS/XLSX/CSV/TSV 格式的資料。 使用"儲存並匯出"功能將 Excel 工作表匯出為多種格式。 使用安全功能對檔案進行加密和解密,並設定密碼。 透過資料集集成,將 Excel 表格作為 DataSet 和 DataTable 物件使用。 Excel公式自動重新計算,支援數學函數。 使用直覺的範圍語法編輯電子表格數據,例如 WorkSheet["A1:B10"]。 -對儲存格區域、列和行進行排序。 設定儲存格樣式,包括字型、背景、邊框、對齊方式和數字格式。 如何在 C# 中開啟 Excel 檔案? 開始之前我需要準備什麼? 若要在 C# 應用程式中使用IronXL ,請在本機上安裝下列元件: Visual Studio - 用於開發 C# .NET應用程式的官方 IDE。 您可以從微軟網站下載並安裝 Visual Studio。 您也可以使用JetBrains ReSharper & Rider 。 有關更多設定指導,請參閱入門概述。 IronXL - 幫助在 C# 中處理 Excel 表格的 Excel 庫。 必須先將其安裝到您的 C# 應用程式中才能使用。 您可以從NuGet網站下載,也可以從 Visual Studio 中的"管理NuGet套件"下載。 您也可以直接下載.NET Excel DLL檔。 有關許可實施,請參閱"使用許可證密鑰" 。 我應該導入哪些命名空間? 安裝 Visual Studio 和IronXL後,在 C# 檔案頂部新增以下程式碼行,以新增必要的IronXL命名空間: // Add reference to the IronXL library using IronXL; // Add reference to the IronXL library using IronXL; $vbLabelText $csharpLabel 要使用特定的 Excel 格式或進階功能,您可能還需要: using IronXl.Formatting; // For cell styling using IronXl.Drawing; // For images and charts using System.Data; // For DataSet/DataTable operations using IronXl.Formatting; // For cell styling using IronXl.Drawing; // For images and charts using System.Data; // For DataSet/DataTable operations $vbLabelText $csharpLabel 如何載入現有的Excel檔案? Excel 檔案(也稱為工作簿)由多個工作表組成,每個工作表都包含儲存格值。 要開啟和讀取 Excel 文件,請使用WorkBook類的Load方法載入它。 LoadSpreadsheets功能支援多種格式。 // Supported Excel spreadsheet formats for reading include: XLSX, XLS, CSV, and TSV WorkBook workbook = WorkBook.Load("test.xlsx"); // You can also load from streams for web applications // using (var stream = File.OpenRead("test.xlsx")) // { // WorkBook workbook = WorkBook.Load(stream); // } // Supported Excel spreadsheet formats for reading include: XLSX, XLS, CSV, and TSV WorkBook workbook = WorkBook.Load("test.xlsx"); // You can also load from streams for web applications // using (var stream = File.OpenRead("test.xlsx")) // { // WorkBook workbook = WorkBook.Load(stream); // } $vbLabelText $csharpLabel 這將工作簿初始化為 WorkBook 實例。 要開啟特定的WorkSheet ,請從 WorkSheets 集合中檢索它。 《管理工作表》指南提供了有關工作表操作的更多詳細資訊: // Access the first worksheet in the workbook WorkSheet sheet = workbook.WorkSheets.First(); // Alternative ways to access worksheets WorkSheet sheetByIndex = workbook.WorkSheets[0]; // By index WorkSheet sheetByName = workbook.GetWorkSheet("Sheet1"); // By name // Access the first worksheet in the workbook WorkSheet sheet = workbook.WorkSheets.First(); // Alternative ways to access worksheets WorkSheet sheetByIndex = workbook.WorkSheets[0]; // By index WorkSheet sheetByName = workbook.GetWorkSheet("Sheet1"); // By name $vbLabelText $csharpLabel 這將存取 Excel 文件中的第一個工作表,可以進行讀取和寫入操作。 Excel 檔案 如何讀取Excel單元格中的資料? Excel 檔案開啟後,即可讀取資料。 使用IronXL在 C# 中讀取 Excel 檔案中的資料非常簡單。 您可以使用"選擇範圍"功能指定儲存格參考來讀取儲存格值。 以下程式碼用於取得儲存格的值: // Select the cell using Excel notation and retrieve its integer value int cellValue = sheet["C2"].IntValue; // You can also retrieve values in different formats string textValue = sheet["C2"].StringValue; decimal decimalValue = sheet["C2"].DecimalValue; DateTime dateValue = sheet["C2"].DateTimeValue; bool boolValue = sheet["C2"].BoolValue; // Display the value in the console Console.WriteLine($"Cell C2 contains: {cellValue}"); // Check if cell is empty before reading if (!sheet["C2"].IsEmpty) { Console.WriteLine($"Cell value: {sheet["C2"].Value}"); } // Select the cell using Excel notation and retrieve its integer value int cellValue = sheet["C2"].IntValue; // You can also retrieve values in different formats string textValue = sheet["C2"].StringValue; decimal decimalValue = sheet["C2"].DecimalValue; DateTime dateValue = sheet["C2"].DateTimeValue; bool boolValue = sheet["C2"].BoolValue; // Display the value in the console Console.WriteLine($"Cell C2 contains: {cellValue}"); // Check if cell is empty before reading if (!sheet["C2"].IsEmpty) { Console.WriteLine($"Cell value: {sheet["C2"].Value}"); } $vbLabelText $csharpLabel 輸出結果如下: 讀取 Excel 若要從一系列儲存格讀取數據,請使用循環遍歷指定的範圍。 "選擇 Excel 區域"範例提供了更多模式: // Iterate through a range of cells and display their address and text content foreach (var cell in sheet["A2:A6"]) { Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text); } // Read an entire column foreach (var cell in sheet.GetColumn(0)) // Column A { if (!cell.IsEmpty) { Console.WriteLine($"Column A value: {cell.Text}"); } } // Read an entire row foreach (var cell in sheet.GetRow(1)) // Row 2 { Console.WriteLine($"Row 2 value: {cell.Text}"); } // Iterate through a range of cells and display their address and text content foreach (var cell in sheet["A2:A6"]) { Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text); } // Read an entire column foreach (var cell in sheet.GetColumn(0)) // Column A { if (!cell.IsEmpty) { Console.WriteLine($"Column A value: {cell.Text}"); } } // Read an entire row foreach (var cell in sheet.GetRow(1)) // Row 2 { Console.WriteLine($"Row 2 value: {cell.Text}"); } $vbLabelText $csharpLabel 儲存格區域 A2:A6 中的每個值都會被存取並列印到控制台。 讀取單元格範圍 有關更詳細的讀寫範例,請查看C# 中的 Excel 讀取教學。 您也可以將 Excel 資料轉換為資料表,以便更輕鬆地進行操作: // Convert worksheet to DataTable for easier data manipulation DataTable dataTable = sheet.ToDataTable(true); // true = first row contains headers // Access data using DataTable methods foreach (DataRow row in dataTable.Rows) { Console.WriteLine($"Employee: {row["Name"]}, Salary: {row["Salary"]}"); } // Convert worksheet to DataTable for easier data manipulation DataTable dataTable = sheet.ToDataTable(true); // true = first row contains headers // Access data using DataTable methods foreach (DataRow row in dataTable.Rows) { Console.WriteLine($"Employee: {row["Name"]}, Salary: {row["Salary"]}"); } $vbLabelText $csharpLabel 如何建立新的Excel檔案? IronXL也方便建立新的工作簿以儲存和擷取資料。 《創建電子表格》指南提供了一個全面的範例。 只需一行程式碼即可建立一個新的 Excel 檔案: // Create a new workbook with the XLSX format WorkBook workBook = new WorkBook(ExcelFileFormat.XLSX); // Alternative: Create with XLS format for compatibility WorkBook xlsWorkBook = new WorkBook(ExcelFileFormat.XLS); // Set workbook metadata workBook.Metadata.Title = "Employee Data"; workBook.Metadata.Author = "Your Name"; workBook.Metadata.Keywords = "employees, salary, data"; // Create a new workbook with the XLSX format WorkBook workBook = new WorkBook(ExcelFileFormat.XLSX); // Alternative: Create with XLS format for compatibility WorkBook xlsWorkBook = new WorkBook(ExcelFileFormat.XLS); // Set workbook metadata workBook.Metadata.Title = "Employee Data"; workBook.Metadata.Author = "Your Name"; workBook.Metadata.Keywords = "employees, salary, data"; $vbLabelText $csharpLabel 接下來,建立一個工作表並在其中新增資料。 如需更進階的建立模式,請參閱建立新的 Excel 檔案。 如何在工作簿中新增工作表? // Create a worksheet named "GDPByCountry" in the workbook WorkSheet workSheet = workBook.CreateWorkSheet("GDPByCountry"); // Create multiple worksheets at once WorkSheet sheet2 = workBook.CreateWorkSheet("PopulationData"); WorkSheet sheet3 = workBook.CreateWorkSheet("Summary"); // Copy an existing worksheet WorkSheet copiedSheet = workSheet.CopySheet("GDPByCountryCopy"); // Create a worksheet named "GDPByCountry" in the workbook WorkSheet workSheet = workBook.CreateWorkSheet("GDPByCountry"); // Create multiple worksheets at once WorkSheet sheet2 = workBook.CreateWorkSheet("PopulationData"); WorkSheet sheet3 = workBook.CreateWorkSheet("Summary"); // Copy an existing worksheet WorkSheet copiedSheet = workSheet.CopySheet("GDPByCountryCopy"); $vbLabelText $csharpLabel 這段程式碼會在工作簿中新增一個名為"GDPByCountry"的工作表,讓您可以新增儲存格值。 了解更多關於管理工作表和複製工作表的資訊。 若要為特定儲存格設定值,請使用下列程式碼: // Set the value of cell A1 to "Example" workSheet["A1"].Value = "Example"; // Add different types of data workSheet["A2"].Value = 12345; // Integer workSheet["A3"].Value = 99.99m; // Decimal workSheet["A4"].Value = DateTime.Now; // Date workSheet["A5"].Value = true; // Boolean // Add formulas workSheet["B1"].Formula = "=SUM(A2:A3)"; // Set multiple cells at once using a range workSheet["C1:C5"].Value = "Bulk Value"; // Save the workbook workBook.SaveAs("output.xlsx"); // Set the value of cell A1 to "Example" workSheet["A1"].Value = "Example"; // Add different types of data workSheet["A2"].Value = 12345; // Integer workSheet["A3"].Value = 99.99m; // Decimal workSheet["A4"].Value = DateTime.Now; // Date workSheet["A5"].Value = true; // Boolean // Add formulas workSheet["B1"].Formula = "=SUM(A2:A3)"; // Set multiple cells at once using a range workSheet["C1:C5"].Value = "Bulk Value"; // Save the workbook workBook.SaveAs("output.xlsx"); $vbLabelText $csharpLabel 最終輸出結果為: 向單元格添加值 使用不同的Excel格式 IronXL支援多種 Excel 格式。 以下是如何處理不同文件類型的方法: // Convert between formats WorkBook workbook = WorkBook.Load("data.csv"); workbook.SaveAs("data.xlsx"); // Convert CSV to XLSX // Export to different formats workbook.SaveAsCsv("output.csv", ";"); // CSV with semicolon delimiter workbook.SaveAsJson("output.json"); // Export as JSON workbook.SaveAsXml("output.xml"); // Export as XML // Convert between formats WorkBook workbook = WorkBook.Load("data.csv"); workbook.SaveAs("data.xlsx"); // Convert CSV to XLSX // Export to different formats workbook.SaveAsCsv("output.csv", ";"); // CSV with semicolon delimiter workbook.SaveAsJson("output.json"); // Export as JSON workbook.SaveAsXml("output.xml"); // Export as XML $vbLabelText $csharpLabel 了解更多關於轉換電子表格檔案類型以及如何將 XLSX 轉換為 CSV、JSON、XML 的資訊。 錯誤處理和最佳實踐 處理Excel檔案時,要實作適當的錯誤處理: try { WorkBook workbook = WorkBook.Load("test.xlsx"); WorkSheet sheet = workbook.GetWorkSheet("Sheet1"); // Check if sheet exists if (sheet == null) { Console.WriteLine("Worksheet not found!"); return; } // Process data var value = sheet["A1"].Value; } catch (Exception ex) { Console.WriteLine($"Error reading Excel file: {ex.Message}"); } try { WorkBook workbook = WorkBook.Load("test.xlsx"); WorkSheet sheet = workbook.GetWorkSheet("Sheet1"); // Check if sheet exists if (sheet == null) { Console.WriteLine("Worksheet not found!"); return; } // Process data var value = sheet["A1"].Value; } catch (Exception ex) { Console.WriteLine($"Error reading Excel file: {ex.Message}"); } $vbLabelText $csharpLabel 對於生產應用,請考慮設定日誌記錄並實現適當的錯誤處理模式。 我們學到了什麼? 本文示範如何使用IronXL在 C# 中開啟和讀取 Excel 文件,例如 XLS 和 XLSX 文件。 IronXL不需要在系統上安裝 Microsoft Excel 即可執行與 Excel 相關的任務,因此非常適合Docker 部署和Azure 函數。 IronXL提供了一個全面的 Excel 相關任務編程解決方案,包括公式計算、字串排序、修剪、尋找和替換、合併和取消合併、保存文件等等。 您也可以設定儲存格資料格式、使用條件格式以及 建立圖表。 若要了解進階功能,請探索分組和取消分組、命名區域、超連結以及保護 Excel 檔案。 完整的API 參考文件提供了所有功能的詳細文件。 IronXL提供30 天免費試用,並可授權用於商業用途。 IronXL 的 Lite 套餐從 $799 開始。 如需更多資源,請造訪教學課程部分或瀏覽常見場景的程式碼範例。 常見問題解答 如何在 C# 中不使用 Interop 的情況下開啟 Excel 文件? 您可以使用 IronXL 庫在 C# 中開啟 Excel 文件,而不需要使用 Interop。使用 WorkBook.Load 方法將 Excel 文件載入 WorkBook 實例中,這樣就可以訪問和操作文件中的數據。 該 C# Excel 庫兼容哪些文件格式? IronXL 支持多種 Excel 文件格式,包括 XLS、XLSX、CSV 和 TSV。這允許開發人員在其 C# 應用程序中靈活地開啟、讀取和寫入這些格式。 我可以在 C# 中使用此庫編輯 Excel 文件嗎? 是的,您可以使用 IronXL 編輯 Excel 文件。載入工作簿後,您可以修改數據、添加新工作表,然後將更改保存回文件或以各種格式導出。 如何在我的 C# 項目中安裝此庫? 要在您的 C# 項目中安裝 IronXL,可以使用 Visual Studio 中的 NuGet 套件管理器來添加此庫。或者,您可以下載 .NET Excel DLL 並在項目中引用它。 是否可以使用此庫加密 Excel 文件? 是的,IronXL 允許您加密和解密 Excel 文件。您可以在文件操作過程中使用密碼保護 Excel 文檔以保護敏感數據。 此庫支持 Excel 表中的公式重新計算嗎? IronXL 支持自動公式重新計算,確保對數據的任何更改都會自動更新公式,就像在 Excel 中一樣。 如何使用此庫讀取 Excel 工作表中的特定單元格值? 要使用 IronXL 讀取特定單元格的值,您可以使用 Excel 標記法引用單元格。例如,sheet["A1"].StringValue 將檢索單元格 A1 的字符串值。 此庫可以在不同的操作系統上使用嗎? 是的,IronXL 與多個操作系統兼容,包括 Windows、Linux 和 macOS。它還支持在 Docker、Azure 和 AWS 環境中的部署。 使用此庫相較於 MS Office Interop 有什麼優勢? IronXL 相較於 MS Office Interop 有多個優勢,如不需要在系統上安裝 Excel、在伺服器環境中具有更好的性能,以及在現代 .NET 應用中易於使用。 此 C# Excel 庫是否提供免費試用? 是的,IronXL 提供 30 天免費試用,允許您在決定為項目購買商業授權之前測試其功能和特性。 Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担產品测测试,產品開發和研究的责任時,Jordi 為持续的產品改進增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 更新2026年3月1日 在ASP.NET C#中下載Excel文件:將數據匯出到XLSX,CSV和更多格式 使用C#和IronXL在ASP.NET Core中下載Excel文件。從MVC控制器中匯出數據到XLSX, CSV和XML,附有MemoryStream和File()返回。包括代碼範例。 閱讀更多 更新2026年3月1日 如何在Blazor中使用IronXL匯出Excel文件 學習如何在Blazor伺服器應用程式中使用IronXL将數據匯出到Excel。此指南涵蓋專案設置、服務設計、條件格式化、多表報告和錯誤處理,附有完整的C#代碼範例。 閱讀更多 更新2026年2月27日 如何在C#中而不是使用StreamReader來讀取Excel文件 了解StreamReader為何無法讀取Excel文件,並學習如何使用IronXL加載來自磁盤或內存流的XLSX和XLS工作簿。 閱讀更多 如何在 C# 中讀取 Excel 文件如何在 Excel 中將表格轉換...
更新2026年3月1日 在ASP.NET C#中下載Excel文件:將數據匯出到XLSX,CSV和更多格式 使用C#和IronXL在ASP.NET Core中下載Excel文件。從MVC控制器中匯出數據到XLSX, CSV和XML,附有MemoryStream和File()返回。包括代碼範例。 閱讀更多
更新2026年3月1日 如何在Blazor中使用IronXL匯出Excel文件 學習如何在Blazor伺服器應用程式中使用IronXL将數據匯出到Excel。此指南涵蓋專案設置、服務設計、條件格式化、多表報告和錯誤處理,附有完整的C#代碼範例。 閱讀更多
更新2026年2月27日 如何在C#中而不是使用StreamReader來讀取Excel文件 了解StreamReader為何無法讀取Excel文件,並學習如何使用IronXL加載來自磁盤或內存流的XLSX和XLS工作簿。 閱讀更多