使用 IRONXL 在 .NET Core 中处理 Excel Curtis Chau 已更新:七月 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 单元格。 带有用户输入的控制台应用程序界面 可以看到,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 文件 如果您想了解更多关于如何 用 C# 读取 Excel 文件,请通过进一步的细节和多个项目以及代码示例深入研究本教程。 教程快速访问 查阅 API 参考文档 IronXL 提供了文档,其中包含所有命名空间、功能集、方法字段、类和枚举。 API 参考
已发布十二月 19, 2025 如何使用 C# Interop 与 IronXL.Excel 在 Excel 中创建透视表 无需 Office 依赖在 C# 中构建 Excel 数据透视表。IronXL 提供强大的数据处理功能,用于创建透视风格的报告,无需 Excel Interop 复杂化。 阅读更多
已发布十二月 18, 2025 C# 使用 IronXL.Excel 将带列标题的 DataGridView 导出到 Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多
已发布十二月 18, 2025 如何在 C# 中使用 IronXL 创建 Excel 报告 在 C# 中使用 IronXL 进行 Excel 报告生成。学习构建具有格式、公式和数据库集成的专业报告。 阅读更多