跳至页脚内容
使用 IRONXL

在 .NET Core 中处理 Excel

.NET Core Excel Overview

In this modern era, we need a better way to work with Excel Spreadsheets in our .NET Core applications. In the following tutorial, we will learn how to access spreadsheets in .NET Core Excel projects and modify their values using C#.

.NET Core Excel Editing

  • Download the IronXL Library
  • Assign values to a cell range
  • Edit cells using user inputs
  • Edit multiple cells using a static value
How To Work related to .NET Core Excel Overview

Step 1

1. Download IronXL Library

For an easy way to work with Excel files in .NET Core, try IronXL. Download IronXL DLL or install with NuGet for free use in development projects.

# Install IronXL using the .NET CLI
dotnet add package IronXL.Excel
# Install IronXL using the .NET CLI
dotnet add package IronXL.Excel
SHELL

How to Tutorial

2. .NET Core Excel Editing Project

Now that you've downloaded IronXL, let's get started. Load an Excel file in the project and access the WorkSheet where data needs to be edited and changes made.


3. Edit Specific Cell Value

For editing Excel files, add the reference IronXL to your project and import the library by using IronXL.

3.1. Load a Sample File

In the following case, our Excel file name is sample.xlsx and it exists in the bin> Debug> netcoreapp3.1 folder of the project. We will use this code to edit the value new value in cell A1 of 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
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. Assign Value to Multiple Cells

It is very easy to edit multiple cells and assign static values at a time by using a colon :. Its left side indicates the starting cell and the right side indicates the last cell of a specific column.

sheet[From:To]

This will edit new value from cell A1 to A9 of column A.

// 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. Edit Cells with User Inputs

Here is an alternative case where we can take the values from the users and edit the Excel file.

// 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

The above code will display a console for user inputs and then update the specified Excel cells with the entered value.

Work with Excel in .NET Core, Figure 1: Console Application UI with user input Console Application UI with user input

Values changed from B4 to B9 in ExcelSheet, as can be seen:

Work with Excel in .NET Core, Figure 2: The new value is filled from B4 to B9 The new value is filled from B4 to B9


6. Edit Multiple Cells with Static Value

It is very easy to edit multiple cells and assign dynamic values. Let's see the following example:

// 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. Read Excel Files In-Depth Tutorial

Dive in with further details and multiple projects and code examples if you want to learn more about how to Read Excel Files C# with this tutorial.


Tutorial Quick Access

Documentation related to Tutorial Quick Access

Investigate the API Reference

Documentation is provided for IronXL, featuring all namespaces, feature sets, methods fields, classes, and enums.

API Reference

常见问题解答

使用 Excel 在 .NET Core 应用程序中的目的是什么?

Excel 在 .NET Core 应用程序中用于高效的数据管理和操作。IronXL 允许开发者使用 C# 以编程方式加载、编辑和保存 Excel 文件,从而提高生产力和数据处理能力。

如何在 .NET Core 项目中安装 Excel 库?

您可以使用以下命令通过 NuGet 包管理器在 .NET Core 项目中安装 IronXL 库:dotnet add package IronXL.Excel。或者,您可以直接从 IronXL 网站下载 DLL 文件。

在 .NET Core 中加载 Excel 文件的步骤是什么?

要在 .NET Core 中使用 IronXL 加载 Excel 文件,请使用 WorkBook.Load 方法。例如,WorkBook wb = WorkBook.Load("sample.xlsx"); 将加载名为 'sample.xlsx' 的 Excel 工作簿。

我可以在 .NET Core 中编辑 Excel 表格的一系列单元格吗?

是的,使用 IronXL,您可以同时编辑 Excel 表格中的一系列单元格。使用语法 ws["A1:A9"].Value = "new value"; 将一个值分配给多个单元格,其中 ws 是一个 WorkSheet 对象。

在 .NET Core 中编辑 Excel 文件时如何处理用户输入?

IronXL 允许通过控制台或用户界面捕获用户输入,然后可以用来定义 Excel 电子表格中更新的单元格范围和值。

在 .NET Core 中用于 Excel 操作的编程语言是什么?

使用 C# 可以在 .NET Core 应用程序中以编程方式使用 IronXL 库操作 Excel 文件。

是否有使用 .NET Core 处理 Excel 文件的教程?

是的,可以使用 C# 和 IronXL 阅读和操作 Excel 文件的全面教程。额外资源和示例项目可以在 IronXL 网站上找到。

在 .NET Core 中使用 Excel 库的兼容性要求是什么?

IronXL 支持.NET Core 的多个版本。有关详细的兼容性信息,请参阅 IronXL 网站上的文档。

在哪里可以访问 Excel 库的 API 文档?

IronXL 的 API 文档可在线查看,提供所有命名空间、方法和功能的详情。请访问 IronXL 网站以获取此资源。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。