跳過到頁腳內容
使用 IRONXL

如何在 C# 中開啟 Excel 文件

本教學將使用 IronXL 作為主要工具,在 C# 專案中開啟和讀取 Excel 檔案。

IronXL Excel庫

IronXL是一個 .NET 函式庫,它優先考慮使用者的易用性、準確性和速度。 它可以幫助您以閃電般的速度開啟、讀取、建立和編輯 Excel 文件,並且不會出現任何錯誤。 它無需 MS Office Interop 即可運行,這使其成為開發人員的強大工具。

IronXL 與所有 .NET Framework 以及 Linux、MacOS、Docker、Azure 和 AWS 相容。 它可用於創建控制台應用程式、Web 應用程式和桌面應用程序,例如 Blazor 和 MAUI 等現代 Web 應用程式。它支援多種工作簿格式,例如 XLS 和 XLSX 檔案、XSLT 和 XLSM、CSV 和 TSV。

IronXL 的一些重要特性

如何在C#中開啟Excel檔案?

先決條件

若要在 C# 應用程式中使用 IronXL,請在本機上安裝下列元件:

  1. Visual Studio - 它是用於開發 C# .NET 應用程式的官方 IDE。 您可以從微軟網站下載並安裝 Visual Studio。 您也可以使用Jetbrains ReSharper 和 Rider。
  2. IronXL - 它是一個 Excel 庫,可以幫助在 C# 中處理給定路徑下的 Excel 工作表。 必須先將其安裝到您的 C# 應用程式中才能使用。 您可以從NuGet 網站下載,也可以從 Visual Studio 工具中的"管理 NuGet 套件"下載。 您也可以直接下載.NET Excel DLL檔。

添加必要的命名空間

安裝 Visual Studio 和 IronXL 後,在 C# 檔案頂部新增以下程式碼行,以新增必要的 IronXL 命名空間:

// Add reference to the IronXL library
using IronXL;
// Add reference to the IronXL library
using IronXL;
' Add reference to the IronXL library
Imports IronXL
$vbLabelText   $csharpLabel

在 C# 中開啟一個現有的 Excel 文件

Excel 檔案(也稱為工作簿)由多個工作表組成,每個工作表都包含儲存格值。 要開啟並讀取 Excel 文件,請使用WorkBook類別的Load方法載入它。

// Supported Excel spreadsheet formats for reading include: XLSX, XLS, CSV, and TSV
WorkBook workbook = WorkBook.Load("test.xlsx");
// Supported Excel spreadsheet formats for reading include: XLSX, XLS, CSV, and TSV
WorkBook workbook = WorkBook.Load("test.xlsx");
' Supported Excel spreadsheet formats for reading include: XLSX, XLS, CSV, and TSV
Dim workbook As WorkBook = WorkBook.Load("test.xlsx")
$vbLabelText   $csharpLabel

這將工作簿初始化為WorkBook實例。 要開啟特定的WorkSheet ,請從WorkSheets集合中檢索它:

// Access the first worksheet in the workbook
WorkSheet sheet = workbook.WorkSheets.First();
// Access the first worksheet in the workbook
WorkSheet sheet = workbook.WorkSheets.First();
' Access the first worksheet in the workbook
Dim sheet As WorkSheet = workbook.WorkSheets.First()
$vbLabelText   $csharpLabel

這將存取 Excel 文件中的第一個工作表,以便進行讀取和寫入操作。

如何在 C# 中開啟 Excel 文件,圖 1:Excel 文件 Excel 檔案

用 C# 讀取 Excel 文件

Excel 檔案開啟後,即可讀取資料。 使用 IronXL 在 C# 中讀取 Excel 檔案中的資料非常簡單。 您可以透過指定儲存格參考來讀取儲存格值。

以下程式碼用於取得儲存格的值:

// Select the cell using Excel notation and retrieve its integer value
int cellValue = sheet["C2"].IntValue;

// Display the value in the console
Console.WriteLine(cellValue);
// Select the cell using Excel notation and retrieve its integer value
int cellValue = sheet["C2"].IntValue;

// Display the value in the console
Console.WriteLine(cellValue);
' Select the cell using Excel notation and retrieve its integer value
Dim cellValue As Integer = sheet("C2").IntValue

' Display the value in the console
Console.WriteLine(cellValue)
$vbLabelText   $csharpLabel

輸出內容如下:

如何在 C# 中開啟 Excel 文件,圖 2:讀取 Excel 讀取 Excel

若要讀取一系列儲存格中的數據,請使用循環遍歷指定的範圍:

// Iterate through a range of cells and display their address and text content
foreach (var cell in sheet["A2:A6"])
{
    Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
// Iterate through a range of cells and display their address and text content
foreach (var cell in sheet["A2:A6"])
{
    Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
' Iterate through a range of cells and display their address and text content
For Each cell In sheet("A2:A6")
	Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)
Next cell
$vbLabelText   $csharpLabel

存取儲存格區域A2:A6中的每個值並將其列印到控制台。

如何在 C# 中開啟 Excel 文件,圖 3:讀取儲存格區域 讀取單元格範圍

有關更詳細的讀寫範例,請查看C# 中的 Excel 讀取教學

建立新工作簿

IronXL 也方便建立新的工作簿以儲存和擷取資料。

只需一行程式碼即可建立一個新的 Excel 檔案:

// Create a new workbook with the XLSX format
WorkBook workBook = new WorkBook(ExcelFileFormat.XLSX);
// Create a new workbook with the XLSX format
WorkBook workBook = new WorkBook(ExcelFileFormat.XLSX);
' Create a new workbook with the XLSX format
Dim workBook As New WorkBook(ExcelFileFormat.XLSX)
$vbLabelText   $csharpLabel

接下來,建立一個工作表並在其中新增資料。

建立新工作表

// Create a worksheet named "GDPByCountry" in the workbook
WorkSheet workSheet = workBook.CreateWorkSheet("GDPByCountry");
// Create a worksheet named "GDPByCountry" in the workbook
WorkSheet workSheet = workBook.CreateWorkSheet("GDPByCountry");
' Create a worksheet named "GDPByCountry" in the workbook
Dim workSheet As WorkSheet = workBook.CreateWorkSheet("GDPByCountry")
$vbLabelText   $csharpLabel

這段程式碼會在工作簿中新增一個名為"GDPByCountry"的工作表,讓您可以新增儲存格值。

若要為特定儲存格設定值,請使用下列程式碼:

// Set the value of cell A1 to "Example"
workSheet["A1"].Value = "Example";
// Set the value of cell A1 to "Example"
workSheet["A1"].Value = "Example";
' Set the value of cell A1 to "Example"
workSheet("A1").Value = "Example"
$vbLabelText   $csharpLabel

最終輸出結果為:

如何在 C# 中開啟 Excel 文件,圖 4:向儲存格新增值 向單元格添加值

概括

本文示範如何使用 IronXL 在 C# 中開啟和讀取 Excel 文件,例如 XLS 和 XLSX 文件。 IronXL 執行與 Excel 相關的任務時,不需要在系統上安裝 Microsoft Excel。

IronXL 提供了一個全面的 Excel 相關任務程式設計解決方案,包括公式計算、字串排序、修剪、尋找和取代、合併和取消合併儲存檔案等等。 您也可以設定儲存格資料格式。

IronXL 提供30 天免費試用,並可授權用於商業用途。 IronXL 的 Lite 套餐起價為$799 。

常見問題解答

不使用 Interop,如何在 C# 中開啟 Excel 檔案?

您可以利用 IronXL.Excel 函式庫,在不使用 Interop 的情況下以 C# 開啟 Excel 檔案。使用 WorkBook.Load 方法可將 Excel 檔案載入 WorkBook 範例中,這可讓您存取和處理檔案中的資料。

此 C# Excel 函式庫相容哪些檔案格式?

IronXL 支援多種 Excel 檔案格式,包括 XLS、XLSX、CSV 和 TSV。這可讓開發人員在 C# 應用程式中靈活地開啟、讀取和寫入這些格式。

我可以使用這個函式庫在 C# 中編輯 Excel 檔案嗎?

是的,您可以使用 IronXL 編輯 Excel 檔案。載入工作簿後,您可以修改資料、新增工作表,然後將變更儲存回檔案或以各種格式匯出。

我該如何安裝這個函式庫,以便在我的 C# 專案中使用?

若要在您的 C# 專案中安裝 IronXL,您可以使用 Visual Studio 中的 NuGet Package Manager 來新增函式庫。另外,您也可以下載 .NET Excel DLL 並在專案中引用它。

是否可以使用這個函式庫加密 Excel 檔案?

是的,IronXL.Excel 允許您加密和解密 Excel 檔案。您可以使用密碼保護您的 Excel 文件,以在檔案操作時保護敏感資料。

這個函式庫是否支援 Excel 表單中的公式重新計算?

IronXL.Excel 支援自動公式重新計算,確保資料的任何變更都會自動更新公式,就像在 Excel 中一樣。

如何使用此庫讀取 Excel 工作表中特定的儲存格值?

若要使用 IronXL 讀取特定的儲存格值,您可以使用 Excel 符號參照儲存格。例如,sheet["A1"].StringValue 將擷取 A1 單元格的字串值。

這個函式庫可以跨不同作業系統使用嗎?

是的,IronXL 與多種作業系統相容,包括 Windows、Linux 和 macOS。它也支援在 Docker、Azure 和 AWS 環境中部署。

與 MS Office Interop 相比,使用此程式庫有何優勢?

與 MS Office Interop 相比,IronXL.Excel 具有多項優勢,例如不需要在系統上安裝 Excel、在伺服器環境中表現更佳,以及易於與現代 .NET 應用程式搭配使用。

此 C# Excel 函式庫是否提供免費試用?

是的,IronXL 提供 30 天免費試用,讓您在決定為專案申請商業授權之前,先測試其特色與功能。

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