使用 IRONXL 在 .NET Core 上使用 Excel Jordi Bardia 更新日期:7月 28, 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 .NET Core Excel 概述 在這個現代時代,我們需要一種更好的方式在 .NET Core 應用程序中處理 Excel 試算表。 在接下來的教程中,我們將學習如何在 .NET Core Excel 項目中訪問試算表並使用 C# 修改它們的值。 class="learnn-how-section"> class="row"> class="col-sm-6"> .NET Core Excel 編輯 下載 IronXL 庫 為一個儲存格範圍分配值 使用用戶輸入編輯儲存格 使用靜態值編輯多個儲存格 class="col-sm-6"> class="download-card"> class="tutorial-segment-title">步驟 1 1. 下載 IronXL 庫 為了在 .NET Core 中更方便地處理 Excel 文件,試試 IronXL。 Download IronXL DLL or 使用 NuGet 安裝以在開發項目中免費使用。 # Install IronXL using the .NET CLI dotnet add package IronXL.Excel # Install IronXL using the .NET CLI dotnet add package IronXL.Excel SHELL class="tutorial-segment-title">如何教程 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 儲存格。 帶有用戶輸入的控制台應用程序界面 在 ExcelSheet 中,值已從 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 文件 如果您想了解更多關於如何使用此教程閱讀 Excel 文件 C#,可深入了解更多細節和多個項目及代碼示例。 class="tutorial-segment-title">教程快速訪問 class="tutorial-section"> class="row"> class="col-sm-4"> class="tutorial-image"> 教程快速訪問" class="img-responsive add-shadow img-responsive img-popup" src="/img/svgs/documentation.svg" loading="lazy"> class="col-sm-8"> 調查 API 參考文獻 為 IronXL 提供文檔, 特徵所有命名空間、功能集、方法字段、類和枚舉。 API 參考
發表日期 10月 27, 2025 如何在 C# 中將 DataGridView 匯出為 Excel 並保留列標題 學習如何在 C# 教程中使用 IronXL library 將 DataGridView 資料匯出為 Excel 同時保留列標題。分步教學。 閱讀更多