如何在 C# 中開啟 Excel 文件和撰寫資料
本文將探索IronXL程式庫,以展示如何在C#主控台應用程式中打開Microsoft Excel文件並寫入資料。
IronXL - 一個Excel程式庫
IronXL 是一個.NET Excel程式庫,便於在C#應用程式中建立、讀取和編輯Excel文件。 它提供卓越的性能和精確的輸出。 該程式庫支持所有Excel工作簿文件格式,包括XLS、XLSX、XLSM、CSV和TSV。此外,它允許將資料以JSON、HTML、二進位、字節陣列、DataSet或DataTable格式儲存或導出。
使用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並點擊建立新專案
新專案 -
選擇主控台應用程式並點擊下一步
新專案型別 -
輸入項目的名稱
新專案名稱 -
選擇.NET版本。 選擇穩定版本.NET 6.0。
新專案其它資訊
安裝IronXL程式庫
專案建立後,需要在專案中安裝IronXL程式庫以便使用。 按照以下步驟安裝。
-
打開管理NuGet套件方案,可以從方案總管或工具中進行操作。
NuGet套件管理器 -
瀏覽IronXL程式庫並選擇當前專案。 點擊安裝。
在NuGet包管理器UI中搜尋和安裝IronXL包
在Program.cs文件的頂部新增以下命名空間
using IronXL;
using IronXL;
Imports IronXL
Open an Existing Excel File in C
IronXL提供了打開現有Excel文件或建立新Excel文件的功能。此範例將使用IronXL在C#中打開一個現有文件。
// 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文件。
Write Data to Excel File 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"
還可以將資料寫入一個單元格範圍。 以下程式碼將資料從單元格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")
Save an Excel File in C
本節說明如何將新寫入的內容儲存到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文件資料的詳細資訊,請查看此範例。
輸出
文件的輸出是:
輸出Excel文件
總結
本文展示了如何在C#中使用IronXL將資料寫入Excel文件。 IronXL提供了便捷的功能來處理現有的Excel文件。 它還允許您建立新的Excel文件並使用簡單語法寫入資料。 IronXL還可以在不安裝Microsoft Excel應用程式的情況下用於讀取Excel文件。 要從Excel文件中讀取資料,您可以查看此程式碼範例頁面。
IronXL對開發免費,商業用途可以授權。 您也可以嘗試使用IronXL免費試用進行商業用途。
常見問題
如何在C#中不使用Interop開啟Excel檔案?
您可以使用IronXL程式庫在C#中不使用Interop開啟Excel檔案。IronXL使您能夠有效地處理Excel檔案,而不需要安裝Microsoft Excel,提高了性能和相容性。
如何使用C#在Excel檔案中的特定單元格寫入資料?
使用IronXL,您可以通過存取工作簿、選擇工作表,然後使用單元格參照將值分配到目標單元格,來在Excel檔案中的特定單元格寫入資料。
使用IronXL操作Excel檔案比使用Office Interop有什麼優勢?
IronXL提供了多項優勢,包括不依賴於Microsoft Excel安裝、改進的性能、跨平台相容性,以及對XLS、XLSX和CSV等多種Excel檔案格式的支持。
我可以使用這個程式庫編輯Excel檔案中的公式嗎?
可以,IronXL允許您編輯Excel檔案中的公式。您可以使用簡單的語法操作現有的公式或插入新的公式,該程式庫會高效地處理這些公式。
IronXL如何支持不同的Excel檔案格式?
IronXL支持多種Excel檔案格式,例如XLS、XLSX、XLSM、CSV和TSV,使您能夠在C#應用程式中無縫地建立、讀取和編輯這些檔案。
是否可以使用IronXL保護Excel檔案?
是的,IronXL提供了保護Excel檔案的功能,通過設置使用者密碼和權限來保證資料安全並控製存取。
如何將IronXL程式庫整合到我的C#專案中?
要將IronXL整合到您的C#專案中,您可以在Visual Studio中使用NuGet套件管理器。搜索IronXL並將其新增到專案中以開始使用其功能。
IronXL 支持哪些平台?
IronXL支持多個平台,包括.NET 5、6、7,以及.NET Core和Framework,使其成為各種開發環境中的多功能選擇。
我可以免費使用IronXL嗎?
IronXL可免費用於開發目的。然而,用於商業用途需要授權。可用於在商業環境中測試該程式庫功能的免費試用版。
如何使用IronXL將Excel資料導出為JSON格式?
IronXL允許您將Excel資料導出為JSON格式,方法是將工作表或特定資料範圍轉換為JSON字串,然後可以在需要JSON資料的應用程式中使用。




