如何在 C# 中開啟 Excel 文件和撰寫數據
本文將探討 IronXL 庫,以示範如何在 C# 控制台應用程式中開啟 Microsoft Excel 檔案並向其中寫入資料。
IronXL - 一個 Excel 庫
IronXL是一個 .NET Excel 函式庫,它簡化了在 C# 應用程式中建立、讀取和編輯 Excel 檔案的過程。 它具有卓越的性能和精準的輸出。 此程式庫支援所有Excel工作簿檔案類型,包括XLS、XLSX、XLSM、CSV和TSV。此外,它還允許將資料保存或匯出為JSON、HTML、二進位、位元組數組、資料集或資料表等格式。
借助 IronXL,開發人員可以無縫地處理工作表和單元格區域。 它提供了在工作表中編輯公式並輕鬆重新計算公式的功能。 按範圍、列或行對資料進行排序非常簡單。 該庫提供了修改佈局的功能,例如凍結窗格、自動調整行/列大小以及新增/刪除行/列。
IronXL 也支援使用使用者密碼和編輯權限來保護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"的新專案。
開啟 Visual Studio,然後按一下"建立新專案"
選擇"控制台應用程式",然後按一下"下一步"。
輸入項目名稱
4.選擇 .NET 版本。 選擇穩定版本 .NET 6.0。
[如何在 C# 中開啟 Excel 檔案並寫入數據,圖 4:新專案附加信息](/static-assets/excel/blog/open-excel-file-write-data-csharp-tutorial/open-excel-file-write-data-csharp-tutorial-4.webp)
**新項目補充訊息**安裝 IronXL 庫
專案建立完成後,需要將 IronXL 庫安裝到專案中才能使用它。 按照以下步驟安裝。
從解決方案資源管理器或工具開啟"管理解決方案的 NuGet 套件"。
如何在 C# 中開啟 Excel 檔案並寫入數據,圖 5:NuGet 套件管理器 NuGet 套件管理器
瀏覽 IronXL 庫並選擇目前項目。 點擊安裝。
如何在 C# 中開啟 Excel 檔案並寫入數據,圖 6:在 NuGet 套件管理器 UI 中搜尋並安裝 IronXL 套件 在 NuGet 套件管理器 UI 中搜尋並安裝 IronXL 套件。
在Program.cs檔案頂部新增以下命名空間
using IronXL;using IronXL;Imports IronXL在 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")現在,讓我們選擇它的第一個工作表。 您可以按索引號或名稱選擇工作表。 DefaultWorkSheet屬性可以幫助取得第一個工作表。
// Select worksheet at index 0
WorkSheet workSheet = workBook.WorkSheets[0];
// Select worksheet by name
WorkSheet ws = workBook.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 = workBook.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 = workBook.GetWorkSheet("Sheet1")
' Get any existing worksheet
Dim firstSheet As WorkSheet = workBook.DefaultWorkSheet上面的程式碼從 Excel 工作簿中取得第一個工作表。 若要建立包含資料的新 Excel 文件,請查看此程式碼範例頁面。
現在,讓我們使用 IronXL 物件庫將資料寫入 Excel 檔案。
使用 C# 將資料寫入 Excel 文件
使用 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"也可以將資料寫入一系列儲存格。 以下程式碼將儲存格 B1 到 B5 中的資料寫入資料。
// Write the same value to cells from B1 to B5
workSheet["B1:B5"].Value = "Range value";// Write the same value to cells from B1 to B5
workSheet["B1:B5"].Value = "Range value";' Write the same value to cells from B1 to B5
workSheet("B1:B5").Value = "Range value"我們也可以使用for迴圈來填滿範圍,使其成為動態的。 代碼如下:
// Specify range in which we want to write the values
for (int i = 1; i <= 5; i++)
{
// Write the Dynamic value in column C
workSheet["C" + i].Value = "Value: " + i;
// Write the Dynamic value in column D
workSheet["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 column C
workSheet["C" + i].Value = "Value: " + i;
// Write the Dynamic value in column D
workSheet["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 column C
workSheet("C" & i).Value = "Value: " & i
' Write the Dynamic value in column D
workSheet("D" & i).Value = "Value: " & i
Next i將資料寫入 Excel 檔案的另一種方法是使用Replace方法。
// Replace the value in cell D5
workSheet["D5"].Replace("Value: 5", "Replaced Value");// Replace the value in cell D5
workSheet["D5"].Replace("Value: 5", "Replaced Value");' Replace the value in cell D5
workSheet("D5").Replace("Value: 5", "Replaced Value")用 C# 儲存 Excel 文件
本節介紹如何儲存包含新寫入內容的 Excel 檔案。
// Save the updated Excel workbook to a file
workBook.SaveAs("sample.xlsx");// Save the updated Excel workbook to a file
workBook.SaveAs("sample.xlsx");' Save the updated Excel workbook to a file
workBook.SaveAs("sample.xlsx")完整的程式碼如下:
using System;
using IronXL;
class Program
{
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";
// Write the same value to cells from B1 to B5
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 column C
workSheet["C" + i].Value = "Value: " + i;
// Write the Dynamic value in column D
workSheet["D" + i].Value = "Value: " + i;
}
// Replace the value in cell D5
workSheet["D5"].Replace("Value: 5", "Replaced Value");
// Save the updated Excel workbook to a file
workBook.SaveAs("sample.xlsx");
Console.WriteLine("Successfully written to Excel File");
}
}using System;
using IronXL;
class Program
{
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";
// Write the same value to cells from B1 to B5
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 column C
workSheet["C" + i].Value = "Value: " + i;
// Write the Dynamic value in column D
workSheet["D" + i].Value = "Value: " + i;
}
// Replace the value in cell D5
workSheet["D5"].Replace("Value: 5", "Replaced Value");
// Save the updated Excel workbook to a file
workBook.SaveAs("sample.xlsx");
Console.WriteLine("Successfully written to Excel File");
}
}Imports System
Imports IronXL
Friend Class Program
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"
' Write the same value to cells from B1 to B5
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 column C
workSheet("C" & i).Value = "Value: " & i
' Write the Dynamic value in column D
workSheet("D" & i).Value = "Value: " & i
Next i
' Replace the value in cell D5
workSheet("D5").Replace("Value: 5", "Replaced Value")
' Save the updated Excel workbook to a file
workBook.SaveAs("sample.xlsx")
Console.WriteLine("Successfully written to Excel File")
End Sub
End Class有關如何在 C# 中讀取 Excel 文件資料的更多詳細信息,請參閱此範例。
輸出
文件的輸出結果為:
如何在 C# 中開啟 Excel 文件並寫入數據,圖 7:輸出的 Excel 文件 輸出的 Excel 文件
摘要
本文示範如何使用 IronXL 在 C# 中將資料寫入 Excel 檔案。 IronXL 提供了輕鬆處理現有 Excel 檔案的功能。 它還允許您建立新的 Excel 文件,並使用簡單的語法向其中寫入資料。 即使沒有安裝 Microsoft Excel 應用程序,IronXL 也可以讀取 Excel 檔案。 要從 Excel 檔案讀取數據,您可以查看此程式碼範例頁面。
IronXL 可免費用於開發,並可授權用於商業用途。 您也可以嘗試 IronXL 的商業用途免費試用版。
常見問題解答
不使用 Interop,如何在 C# 中開啟 Excel 檔案?
您可以利用 IronXL.Excel 函式庫,在不使用 Interop 的情況下以 C# 開啟 Excel 檔案。IronXL 可讓您有效率地處理 Excel 檔案,而無需安裝 Microsoft Excel,提升效能與相容性。
如何使用 C# 將資料寫入 Excel 檔案中的特定儲存格?
使用 IronXL.Excel,您可以透過存取工作簿、選取工作表,然後使用儲存格引用將資料寫入 Excel 檔案中的特定儲存格,並將值指定到所需的儲存格。
與 Office Interop 相比,使用 IronXL 進行 Excel 檔案操作有哪些優勢?
與 Office Interop 相比,IronXL.Excel 具備多項優勢,包括不依賴 Microsoft Excel 安裝、效能提升、跨平台相容性,以及支援 XLS、XLSX 和 CSV 等多種 Excel 檔案格式。
我可以使用這個函式庫編輯 Excel 檔案中的公式嗎?
是的,IronXL.Excel 允許您編輯 Excel 檔案中的公式。您可以使用直接的語法操作現有公式或插入新公式,程式庫會有效率地處理這些公式。
IronXL 如何支援不同的 Excel 檔案格式?
IronXL 支援多種 Excel 檔案格式,例如 XLS、XLSX、XLSM、CSV 和 TSV,讓您能夠在 C# 應用程式中無縫建立、讀取和編輯這些檔案。
是否可以使用 IronXL 保護 Excel 檔案?
是的,IronXL.Excel 提供透過設定使用者密碼和權限來保護 Excel 檔案的功能,可確保資料安全並控制存取權限。
如何將 IronXL 函式庫整合到我的 C# 專案中?
要將 IronXL 整合到您的 C# 專案中,您可以使用 Visual Studio 中的 NuGet Package Manager。搜尋 IronXL,並將其新增至您的專案,即可開始使用其功能。
IronXL 支援哪些平台?
IronXL 支援多種平台,包括 .NET 5、6 和 7,以及 .NET Core 和 Framework,使其成為各種開發環境的多用途選擇。
我可以免費使用 IronXL 嗎?
IronXL 對於開發用途是免費的。然而,若要用於商業用途,則必須取得授權。我們提供免費試用版,以便在商業環境中測試該函式庫的功能。
如何使用 IronXL 將 Excel 資料匯出為 JSON?
IronXL.Excel 可讓您將 Excel 資料匯出為 JSON 格式,方法是將工作表或特定資料範圍轉換為 JSON 字串,然後將其用於需要 JSON 資料的應用程式中。









