使用IRONXL

如何在 C# 中打開 Excel 文件並寫入數據

已更新 2024年3月31日
分享:

本文將探討IronXL庫,以展示如何在C#控制台應用程序中打開Microsoft Excel文件並向其寫入數據。

IronXL - 一個 Excel 函式庫

IronXL 是一个 .NET Excel 函式庫,可在 C# 應用程序中創建、讀取和編輯 Excel 檔案。它提供卓越的性能和準確的輸出。該函式庫支持 所有 Excel 活頁簿文件格式,包括 XLS、XLSX、XLSM、CSV 和 TSV。此外,它允許以 JSON、HTML、二進制、字節數組等格式保存或導出數據。 資料集(DataSet)、或資料表(DataTable)透過IronXL,開發人員可以順利地處理工作表和單元格範圍。它提供了編輯公式並在工作表內輕鬆重新計算的功能。 排序資料 基於範圍、列或行是很簡單的。該庫提供了修改佈局的功能,例如 凍結窗格, 自動調整行/列,和 添加/刪除行/列鐵XL也是 啟用保護 Excel 文件具有使用者密碼和編輯權限的保護。另一個顯著的功能是能夠從 Excel 工作表中添加、移除和提取圖片。該程式庫提供了廣泛的 Excel 功能,支持各種 儲存格數據格式這些功能使 IronXL 成為最易於使用的處理 Excel 文件的 API 之一。

IronXL 的一個顯著優勢是它不需要在機器上安裝 Microsoft Excel,這樣就不需要 Office Interop 或任何其他依賴項。它兼容多個平台,支持 .NET 7、6 和 5。它還兼容 .NET Core 2 和 3,以及 .NET Framework 4.5 及更高版本,用於處理 Excel 試算表。

創建控制台應用程式

建議使用最新版本的 Visual Studio IDE 來創建應用程式。Visual Studio 是 C# 開發的官方 IDE,假設您已經安裝了它。如果您尚未安裝 Visual Studio,您可以從官方網站下載。 Microsoft Visual Studio 網站請按照以下步驟創建一個名為 "DemoApp" 的新項目。

  1. 打開 Visual Studio 並點擊創建新項目

    如何在 C# 中打開 Excel 文件並寫入數據,圖 1:新項目

    新專案

  2. 選擇控制台應用程式並點擊下一步

    如何在 C# 中開啟 Excel 文件並寫入數據,圖 2:新專案類型

    新專案類型

  3. 輸入專案名稱

    如何在C#中打開Excel文件並寫入數據,圖3:新專案名稱

    **新專案名稱**
  4. 選擇 .NET 版本。選擇穩定版本 .NET 6.0。

    如何在 C# 中打開 Excel 文件並寫入數據,圖 4:新專案附加資訊

    新專案附加資訊

安裝 IronXL 庫

一旦創建了項目,就需要在項目中安裝 IronXL 庫以便使用。按照以下步驟安裝它。

  1. 從解決方案資源管理器或工具中打開管理 NuGet 套件。

    如何在 C# 中打開 Excel 文件並寫入數據,圖 5:NuGet 套件管理器

    NuGet 套件管理器

  2. 瀏覽 IronXL Library 並選擇目前的專案。點擊安裝。

    如何在 C# 中開啟 Excel 文件並寫入數據,圖 6:在 NuGet 套件管理器 UI 中搜索並安裝 IronXL 套件

    在 NuGet 套件管理器 UI 中搜索和安裝 IronXL 套件

Program.cs 文件的頂部添加以下命名空間

using IronXL;
using IronXL;
Imports IronXL
VB   C#

在C#中打開現有的Excel文件

IronXL 提供了打開現有Excel文件的功能,或您可以創建新的Excel文件。此範例將使用 C# IronXL 打開一個現有文件。

// Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.Load("sample.xlsx");
// Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.Load("sample.xlsx");
' Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
Dim workBook As WorkBook = WorkBook.Load("sample.xlsx")
VB   C#

現在,讓我們選擇其第一個工作表。您可以通過索引號或名稱選擇工作表。DefaultWorkSheet 屬性可以幫助獲取第一個工作表。

// Select worksheet at index 0
WorkSheet workSheet = workBook.WorkSheets [0];

// Select worksheet by name 
WorkSheet ws = wb.GetWorkSheet("Sheet1"); 

// Get any existing worksheet
WorkSheet firstSheet = workBook.DefaultWorkSheet;
// Select worksheet at index 0
WorkSheet workSheet = workBook.WorkSheets [0];

// Select worksheet by name 
WorkSheet ws = wb.GetWorkSheet("Sheet1"); 

// Get any existing worksheet
WorkSheet firstSheet = workBook.DefaultWorkSheet;
' Select worksheet at index 0
Dim workSheet As WorkSheet = workBook.WorkSheets (0)

' Select worksheet by name 
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")

' Get any existing worksheet
Dim firstSheet As WorkSheet = workBook.DefaultWorkSheet
VB   C#

上面的代碼從 Excel 工作簿中獲取第一個工作表。要使用數據創建新的 Excel 文件,請檢查此 程式碼範例頁面現在,讓我們使用 IronXL 物件庫將資料寫入 Excel 文件。

將資料寫入 Excel 檔案 in C

使用 IronXL 將資料寫入 Excel 檔案非常簡單。有多種方式可以達成此目的,但最簡單的方法是使用 Excel 單元格參考。

// Access A1 cell and write the value
workSheet ["A1"].Value = "Value using cell reference";
// Access A1 cell and write the value
workSheet ["A1"].Value = "Value using cell reference";
' Access A1 cell and write the value
workSheet ("A1").Value = "Value using cell reference"
VB   C#

也可以將資料寫入一系列的儲存格。以下程式碼將資料寫入從 B1 到 B5 的儲存格。

workSheet ["B1:B5"].Value = "Range value";
workSheet ["B1:B5"].Value = "Range value";
workSheet ("B1:B5").Value = "Range value"
VB   C#

我們也可以使用 for 迴圈來填充範圍,使其具有動態特性。代碼如下:

//specify range in which we want to write the values
for (int i = 1; i <= 5; i++)
{
    //write the Dynamic value in one row
    workSheet ["C" + i].Value = "Value: " + i;

    //write the Dynamic value in another row
    ws ["D" + i].Value = "Value: " + i;
}
//specify range in which we want to write the values
for (int i = 1; i <= 5; i++)
{
    //write the Dynamic value in one row
    workSheet ["C" + i].Value = "Value: " + i;

    //write the Dynamic value in another row
    ws ["D" + i].Value = "Value: " + i;
}
'specify range in which we want to write the values
For i As Integer = 1 To 5
	'write the Dynamic value in one row
	workSheet ("C" & i).Value = "Value: " & i

	'write the Dynamic value in another row
	ws ("D" & i).Value = "Value: " & i
Next i
VB   C#

另一種將數據寫入 Excel 文件的方法是使用 取代 方法。

workSheet ["D5"].Replace("Value: 5", "Replaced Value");
workSheet ["D5"].Replace("Value: 5", "Replaced Value");
workSheet ("D5").Replace("Value: 5", "Replaced Value")
VB   C#

在C#中保存Excel文件

本節說明如何將新寫入的內容保存到Excel文件中。

workBook.SaveAs("sample.xlsx");
workBook.SaveAs("sample.xlsx");
workBook.SaveAs("sample.xlsx")
VB   C#

代碼如下:

using System;
using IronXL;

static void Main(string [] args)
{
    // Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
    WorkBook workBook = WorkBook.Load("sample.xlsx");

    // Select worksheet at index 0
    WorkSheet workSheet = workBook.WorkSheets [0];

    // Access A1 cell and write the value
    workSheet ["A1"].Value = "Value using cell reference";

    workSheet ["B1:B5"].Value = "Range value";

    //specify range in which we want to write the values
    for (int i = 1; i <= 5; i++)
    {
        //write the Dynamic value in one row
        workSheet ["C" + i].Value = "Value: " + i;

        //write the Dynamic value in another row
        workSheet ["D" + i].Value = "Value: " + i;
    }

    workSheet ["D5"].Replace("Value: 5", "Replaced Value");

    workBook.SaveAs("sample.xlsx");
    Console.WriteLine("successfully written in Excel File");
}
using System;
using IronXL;

static void Main(string [] args)
{
    // Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
    WorkBook workBook = WorkBook.Load("sample.xlsx");

    // Select worksheet at index 0
    WorkSheet workSheet = workBook.WorkSheets [0];

    // Access A1 cell and write the value
    workSheet ["A1"].Value = "Value using cell reference";

    workSheet ["B1:B5"].Value = "Range value";

    //specify range in which we want to write the values
    for (int i = 1; i <= 5; i++)
    {
        //write the Dynamic value in one row
        workSheet ["C" + i].Value = "Value: " + i;

        //write the Dynamic value in another row
        workSheet ["D" + i].Value = "Value: " + i;
    }

    workSheet ["D5"].Replace("Value: 5", "Replaced Value");

    workBook.SaveAs("sample.xlsx");
    Console.WriteLine("successfully written in Excel File");
}
Imports System
Imports IronXL

Shared Sub Main(ByVal args() As String)
	' Supported spreadsheet formats for reading XLSX, XLS, XLSM, XLTX, CSV and TSV
	Dim workBook As WorkBook = WorkBook.Load("sample.xlsx")

	' Select worksheet at index 0
	Dim workSheet As WorkSheet = workBook.WorkSheets (0)

	' Access A1 cell and write the value
	workSheet ("A1").Value = "Value using cell reference"

	workSheet ("B1:B5").Value = "Range value"

	'specify range in which we want to write the values
	For i As Integer = 1 To 5
		'write the Dynamic value in one row
		workSheet ("C" & i).Value = "Value: " & i

		'write the Dynamic value in another row
		workSheet ("D" & i).Value = "Value: " & i
	Next i

	workSheet ("D5").Replace("Value: 5", "Replaced Value")

	workBook.SaveAs("sample.xlsx")
	Console.WriteLine("successfully written in Excel File")
End Sub
VB   C#

有關如何在C#中讀取Excel文件數據的更多詳細信息,請查看 這個範例.

輸出

文件的輸出是:

如何在 C# 中打開 Excel 文件並寫入數據,圖 7:輸出 Excel 文件

輸出的 Excel 文件

概要

本文展示了如何使用IronXL在C#中將資料寫入Excel文件。IronXL提供了輕鬆處理現有Excel文件的功能。它還允許您創建新的Excel文件並以簡單語法寫入數據。即使沒有安裝Microsoft Excel應用程序,也可以使用IronXL讀取Excel文件。要從Excel文件中讀取數據,您可以參考這個 程式碼範例頁面鐵XL(IronXL)在開發中是免費的,並且可以商業使用授權。您還可以免費試用鐵XL(IronXL) 試用 商業用途。

< 上一頁
如何使用 C# 禁用 Excel 中的受保护视图
下一個 >
如何在C#中將海量數據從DataTable匯出到Excel

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 1,023,839 查看許可證 >