跳過到頁腳內容
使用 IRONXL

在 .NET Core 上使用 Excel

.NET Core Excel 概述

在當今時代,我們需要一種更好的方法來在 .NET Core 應用程式中使用 Excel 電子表格。 在接下來的教學中,我們將學習如何在 .NET Core Excel 專案中存取電子表格並使用 C# 修改其值。

.NET Core Excel 編輯

  • 下載 IronXL 庫
  • 為單元格區域賦值
  • 使用使用者輸入編輯儲存格
  • 使用靜態值編輯多個儲存格
How To Work related to .NET Core Excel 概述

步驟 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列中A1A9單元格的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 文件的信息,請深入學習本教程,其中包含更多詳細信息、多個項目和程式碼範例。


教程快速存取

Documentation related to 教程快速存取

查閱 API 參考文檔

IronXL 提供了文檔,其中包含所有命名空間、功能集、方法欄位、類別和枚舉。

API 參考

常見問題解答

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

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,您可以同時編輯 Excel 表單中的單元格範圍。使用語法 ws["A1:A9"].Value = "new value"; 來為多個儲存格指定值,其中 ws 是一個 WorkSheet 物件。

在 .NET Core 中編輯 Excel 檔案時,該如何處理使用者輸入?

IronXL 可透過控制台或使用者介面擷取使用者的輸入,以處理使用者的輸入,然後可用於定義 Excel 試算表中的儲存格範圍和更新值。

在 .NET Core 中,Excel 的操作使用何種程式語言?

C# 用於在 .NET Core 應用程式中使用 IronXL library 程式化操作 Excel 檔案。

是否有在 .NET Core 中處理 Excel 檔案的教學?

是的,使用 C# 與 IronXL.Excel 閱讀和操作 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 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。