使用 IRONXL 在 .NET Core 上使用 Excel Jordi Bardia 更新:7月 28, 2025 下載 IronXL NuGet 下載 DLL 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 .NET Core Excel 概述 在當今時代,我們需要一種更好的方法來在 .NET Core 應用程式中使用 Excel 電子表格。 在接下來的教學中,我們將學習如何在 .NET Core Excel 專案中存取電子表格並使用 C# 修改其值。 .NET Core Excel 編輯 下載 IronXL 庫 為單元格區域賦值 使用使用者輸入編輯儲存格 使用靜態值編輯多個儲存格 步驟 1 1. 下載 IronXL 庫 想要在 .NET Core 中輕鬆處理 Excel 文件,不妨試試 IronXL。 下載 IronXL DLL或使用 NuGet 安裝,即可在開發專案中免費使用。 # Install IronXL using the .NET CLI dotnet add package IronXL.Excel # Install IronXL using the .NET CLI dotnet add package IronXL.Excel SHELL 操作指南 2. .NET Core Excel 編輯項目 現在你已經下載了 IronXL,讓我們開始吧。 在專案中載入 Excel 文件,並存取需要編輯資料和進行更改的WorkSheet 。 3. 編輯特定儲存格值 若要編輯 Excel 文件,請將IronXL引用新增至您的專案中,並using IronXL匯入庫。 3.1. 載入範例文件 在下列範例中,我們的 Excel 檔案名稱是sample.xlsx ,它位於專案的bin> Debug> netcoreapp3.1資料夾中。 我們將使用此程式碼編輯sample.xlsx檔案A1儲存格中的值new value 。 // Anchor: Load a sample file using IronXL; static void Main(string[] args) { // Load the Excel workbook WorkBook wb = WorkBook.Load("sample.xlsx"); // Get the first worksheet named "Sheet1" WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Access cell A1 and set its value to "new value" ws["A1"].Value = "new value"; // Save the changes to the Excel workbook wb.SaveAs("sample.xlsx"); } // Anchor: Load a sample file using IronXL; static void Main(string[] args) { // Load the Excel workbook WorkBook wb = WorkBook.Load("sample.xlsx"); // Get the first worksheet named "Sheet1" WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Access cell A1 and set its value to "new value" ws["A1"].Value = "new value"; // Save the changes to the Excel workbook wb.SaveAs("sample.xlsx"); } ' Anchor: Load a sample file Imports IronXL Shared Sub Main(ByVal args() As String) ' Load the Excel workbook Dim wb As WorkBook = WorkBook.Load("sample.xlsx") ' Get the first worksheet named "Sheet1" Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") ' Access cell A1 and set its value to "new value" ws("A1").Value = "new value" ' Save the changes to the Excel workbook wb.SaveAs("sample.xlsx") End Sub $vbLabelText $csharpLabel 4. 為多個單元格賦值 使用冒號" :可以非常輕鬆地一次編輯多個單元格並分配靜態值。 它的左側表示特定列的起始儲存格,右側表示特定列的最後一個儲存格。 sheet[From:To] 這將編輯column A列中A1到A9單元格的new value 。 // Anchor: Assign Value to Multiple Cells using IronXL; static void Main(string[] args) { WorkBook wb = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Set the value "new value" for cells from A1 to A9 ws["A1:A9"].Value = "new value"; wb.SaveAs("sample.xlsx"); } // Anchor: Assign Value to Multiple Cells using IronXL; static void Main(string[] args) { WorkBook wb = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Set the value "new value" for cells from A1 to A9 ws["A1:A9"].Value = "new value"; wb.SaveAs("sample.xlsx"); } ' Anchor: Assign Value to Multiple Cells Imports IronXL Shared Sub Main(ByVal args() As String) Dim wb As WorkBook = WorkBook.Load("sample.xlsx") Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") ' Set the value "new value" for cells from A1 to A9 ws("A1:A9").Value = "new value" wb.SaveAs("sample.xlsx") End Sub $vbLabelText $csharpLabel 5. 使用使用者輸入編輯儲存格 這裡還有另一種方法,我們可以從使用者那裡獲取數值並編輯 Excel 檔案。 // Anchor: Edit Cells with User Inputs using System; using IronXL; static void Main(string[] args) { string _from, _to, newValue; // Capture user inputs Console.Write("Enter Starting Cell: "); _from = Console.ReadLine(); Console.Write("Enter Last Cell: "); _to = Console.ReadLine(); Console.Write("Enter value: "); newValue = Console.ReadLine(); // Load the Excel workbook and access the worksheet WorkBook wb = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Assign the user-entered value to the specified cell range ws[_from + ":" + _to].Value = newValue; // Save changes to the workbook wb.SaveAs("sample.xlsx"); Console.WriteLine("Successfully Changed...!"); Console.ReadKey(); } // Anchor: Edit Cells with User Inputs using System; using IronXL; static void Main(string[] args) { string _from, _to, newValue; // Capture user inputs Console.Write("Enter Starting Cell: "); _from = Console.ReadLine(); Console.Write("Enter Last Cell: "); _to = Console.ReadLine(); Console.Write("Enter value: "); newValue = Console.ReadLine(); // Load the Excel workbook and access the worksheet WorkBook wb = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Assign the user-entered value to the specified cell range ws[_from + ":" + _to].Value = newValue; // Save changes to the workbook wb.SaveAs("sample.xlsx"); Console.WriteLine("Successfully Changed...!"); Console.ReadKey(); } ' Anchor: Edit Cells with User Inputs Imports System Imports IronXL Shared Sub Main(ByVal args() As String) Dim _from, _to, newValue As String ' Capture user inputs Console.Write("Enter Starting Cell: ") _from = Console.ReadLine() Console.Write("Enter Last Cell: ") _to = Console.ReadLine() Console.Write("Enter value: ") newValue = Console.ReadLine() ' Load the Excel workbook and access the worksheet Dim wb As WorkBook = WorkBook.Load("sample.xlsx") Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") ' Assign the user-entered value to the specified cell range ws(_from & ":" & _to).Value = newValue ' Save changes to the workbook wb.SaveAs("sample.xlsx") Console.WriteLine("Successfully Changed...!") Console.ReadKey() End Sub $vbLabelText $csharpLabel 上述程式碼將顯示一個控制台供使用者輸入,然後以輸入的值更新指定的 Excel 儲存格。 在 .NET Core 中使用 Excel,圖 1:具有使用者輸入的控制台應用程式 UI 帶有用戶輸入的控制台應用程式使用者介面 Excel表格中B4儲存格到B9儲存格的值發生了變化,如圖所示: 在 .NET Core 中使用 Excel,圖 2:新值填入在 B4 到 B9 之間 新值填入 B4 到 B9 之間。 6. 編輯多個具有靜態值的儲存格 編輯多個單元格並分配動態值非常容易。 讓我們來看下面的例子: // Anchor: Edit Multiple Cells with Static Value using IronXL; static void Main(string[] args) { WorkBook wb = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Ensure 'from' and 'to' are defined for the intended cell range int from = 1; int to = 9; // Iterate over a range of cells and update them with dynamic values for (int i = from; i <= to; i++) { ws["A" + i].Value = "Value" + i; } // Save the changes to the Excel file wb.SaveAs("sample.xlsx"); } // Anchor: Edit Multiple Cells with Static Value using IronXL; static void Main(string[] args) { WorkBook wb = WorkBook.Load("sample.xlsx"); WorkSheet ws = wb.GetWorkSheet("Sheet1"); // Ensure 'from' and 'to' are defined for the intended cell range int from = 1; int to = 9; // Iterate over a range of cells and update them with dynamic values for (int i = from; i <= to; i++) { ws["A" + i].Value = "Value" + i; } // Save the changes to the Excel file wb.SaveAs("sample.xlsx"); } ' Anchor: Edit Multiple Cells with Static Value Imports IronXL Shared Sub Main(ByVal args() As String) Dim wb As WorkBook = WorkBook.Load("sample.xlsx") Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") ' Ensure 'from' and 'to' are defined for the intended cell range Dim from As Integer = 1 Dim [to] As Integer = 9 ' Iterate over a range of cells and update them with dynamic values For i As Integer = From To [to] ws("A" & i).Value = "Value" & i Next i ' Save the changes to the Excel file wb.SaveAs("sample.xlsx") End Sub $vbLabelText $csharpLabel 7. 深入閱讀 Excel 檔案教學 如果您想了解更多關於如何使用 C# 讀取 Excel 文件的信息,請深入學習本教程,其中包含更多詳細信息、多個項目和程式碼範例。 教程快速存取 查閱 API 參考文檔 IronXL 提供了文檔,其中包含所有命名空間、功能集、方法欄位、類別和枚舉。 API 參考
發表日期 12月 19, 2025 如何使用 C# Interop 與 IronXL 在 Excel 中建立資料透視表 在無需 Office 依賴的情況下在 C# 中構建 Excel 樞紐分析表。IronXL 對無需 Excel Interop 的樞紐型報告創建提供了強大的數據操作功能。 閱讀更多
發表日期 12月 18, 2025 使用 IronXL 將 C# DataGridView 匯出到 Excel,並帶有列標題 學習如何在 C# 教程中使用 IronXL library 將 DataGridView 資料匯出為 Excel 同時保留列標題。分步教學。 閱讀更多
發表日期 12月 18, 2025 如何在 C# 中使用 IronXL 創建 Excel 生成報告 使用 IronXL 在 C# 中創建 Excel 報告生成。學習構建擁有格式化公式和數據庫集成的專業報告。 閱讀更多