跳過到頁腳內容
使用 IRONXL

在 .NET Core 上使用 Excel

.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"> How To Work related to .NET Core Excel 概述

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 AA1A9new 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:帶有用戶輸入的控制台應用程序界面 帶有用戶輸入的控制台應用程序界面

在 ExcelSheet 中,值已從 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 文件

如果您想了解更多關於如何使用此教程閱讀 Excel 文件 C#,可深入了解更多細節和多個項目及代碼示例。


class="tutorial-segment-title">教程快速訪問

class="tutorial-section">
class="row">
class="col-sm-4">
class="tutorial-image"> Documentation related to class=教程快速訪問" class="img-responsive add-shadow img-responsive img-popup" src="/img/svgs/documentation.svg" loading="lazy">
class="col-sm-8">

調查 API 參考文獻

為 IronXL 提供文檔, 特徵所有命名空間、功能集、方法字段、類和枚舉。

API 參考

常見問題解答

在 .NET Core 應用中使用 Excel 的目的何在?

Excel 在 .NET Core 應用中用於高效的數據管理和操作。IronXL 允許開發者使用 C# 以程式化方式載入、編輯和保存 Excel 文件,提高生產力和數據處理能力。

如何在 .NET Core 專案中安裝 Excel 庫?

您可以在 .NET Core 專案中使用 NuGet 包管理器與命令 dotnet add package IronXL.Excel 安裝 IronXL 庫。或者,您可以從 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 應用中以程序化方式處理 Excel 文件,使用 IronXL 庫。

是否有關於在 .NET Core 中操作 Excel 文件的教程?

有,用 C# 與 IronXL 讀取和操作 Excel 文件的綜合教程可用。IronXL 網站提供其他資源和示例專案。

.NET Core 中使用 Excel 庫的兼容性要求是什麼?

IronXL 支援各個版本的 .NET Core。詳細的兼容性信息可以在 IronXL 文檔中找到。

在哪裡可以訪問 Excel 庫的 API 文檔?

IronXL 提供在線的 API 文檔,提供所有命名空間、方法及功能的詳細信息。訪問 IronXL 網站來取得此資源。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。