C# 打開 Excel 工作表
了解如何使用 C# 開啟 Excel 工作表功能來處理 Excel 試算表,並開啟所有文件類型,包括(.xls
、.csv
、.tsv
和 .xlsx
)。 要打開 Excel 工作表,讀取其數據並以程式化方式進行操作對於許多開發應用程式的人來說是必不可少的。 這是為每位想要方法更少行數且響應時間更快的開發者提供的解決方案。
如何在 C# 中開啟 Excel 文件
How to Open Excel Worksheet in C#
安裝 Excel 函式庫以讀取 Excel 檔案。
將現有的 Excel 文件加載到一個
Workbook
對象中。設定預設的 Excel 工作表。
從 Excel 工作簿讀取值。
- 按照相應的處理值並顯示。
第一步
存取 Excel C# 程式庫
通過DLL 存取 Excel C# 函式庫或使用您偏好的NuGet 管理器來安裝。 一旦您訪問了IronXL庫並將其添加到您的項目中,您就可以使用以下所有功能來打開Excel工作表C#。
Install-Package IronXL.Excel
如何操作教程
2. 載入 Excel 檔案
使用 IronXL 的 WorkBook.Load()
函數將 Excel 文件載入專案。 此功能需要一個字符串參數,即要打開的 Excel 文件的路徑。 請看這裡:
WorkBook wb = WorkBook.Load("Path");//Excel file path
WorkBook wb = WorkBook.Load("Path");//Excel file path
Dim wb As WorkBook = WorkBook.Load("Path") 'Excel file path
指定路徑的 Excel 文件將加載到wb
中。 現在,我們需要指定將要打開的 Excel 工作表。
3. 打開 Excel 工作表
若要打開 Excel 文件中指定的WorkSheet
,IronXL 提供了WorkBook.GetWorkSheet()
函數。 使用這個,我們可以輕鬆地按其名稱打開工作表:
WorkSheet ws = WorkBook.GetWorkSheet("SheetName");
WorkSheet ws = WorkBook.GetWorkSheet("SheetName");
Dim ws As WorkSheet = WorkBook.GetWorkSheet("SheetName")
指定的WorkSheet
將會在ws
中打開,並包含其所有數據。 還有一些其他方法可以開啟 Excel 文件中的特定WorkSheet
:
/**
Open Excel Worksheet
anchor-open-excel-worksheet
**/
//by sheet index
WorkSheet ws = wb.WorkSheets [0];
//for the default
WorkSheet ws = wb.DefaultWorkSheet;
//for the first sheet:
WorkSheet ws = wb.WorkSheets.First();
//for the first or default sheet:
WorkSheet ws = wb.WorkSheets.FirstOrDefault();
/**
Open Excel Worksheet
anchor-open-excel-worksheet
**/
//by sheet index
WorkSheet ws = wb.WorkSheets [0];
//for the default
WorkSheet ws = wb.DefaultWorkSheet;
//for the first sheet:
WorkSheet ws = wb.WorkSheets.First();
//for the first or default sheet:
WorkSheet ws = wb.WorkSheets.FirstOrDefault();
'''
'''Open Excel Worksheet
'''anchor-open-excel-worksheet
'''*
'by sheet index
Dim ws As WorkSheet = wb.WorkSheets (0)
'for the default
Dim ws As WorkSheet = wb.DefaultWorkSheet
'for the first sheet:
Dim ws As WorkSheet = wb.WorkSheets.First()
'for the first or default sheet:
Dim ws As WorkSheet = wb.WorkSheets.FirstOrDefault()
現在,我們只需要從已打開的 Excel WorkSheet
獲取數據。
4. 從工作表中獲取數據
我們可以透過以下方式從打開的 Excel WorkSheet
中獲取資料:
取得 Excel
WorkSheet
的特定儲存格值。在特定範圍內
獲取
資料。從
WorkSheet
取得所有資料。讓我們逐一看看如何通過這些例子以不同的方式獲取數據:
4.1. 獲取特定單元格的值
從 Excel WorkSheet
獲取數據的第一種方法是獲取特定的單元格值。 可以這樣訪問:
string val = ws ["Cell Address"].ToString();
string val = ws ["Cell Address"].ToString();
Dim val As String = ws ("Cell Address").ToString()
ws
是 Excel 文件的WorkSheet
,如以下範例所示。 可以通過指定「行索引」和「列索引」來訪問特定單元格的值。
string val=ws.Rows [RowIndex].Columns [ColumnIndex].Value.ToString();
string val=ws.Rows [RowIndex].Columns [ColumnIndex].Value.ToString();
Dim val As String=ws.Rows (RowIndex).Columns (ColumnIndex).Value.ToString()
讓我們看看如何在C#項目中打開Excel文件並通過兩種方式獲取特定單元格值的示例:
/**
Get Cell Value
anchor-get-specific-cell-value
**/
using IronXL;
static void Main(string [] args)
{
//Load Excel file
WorkBook wb = WorkBook.Load("sample.xlsx");
//Open WorkSheet
WorkSheet ws = wb.GetWorkSheet("Sheet1");
//Get value By Cell Address
Int32 int_val= ws ["C6"].Int32Value;
//Get value by Row and Column Address
string str_val=ws.Rows [3].Columns [1].Value.ToString();
Console.WriteLine("Getting Value by Cell Address: {0}",int_val);
Console.WriteLine("Getting Value by Row and Column Indexes: {0}",str_val);
Console.ReadKey();
}
/**
Get Cell Value
anchor-get-specific-cell-value
**/
using IronXL;
static void Main(string [] args)
{
//Load Excel file
WorkBook wb = WorkBook.Load("sample.xlsx");
//Open WorkSheet
WorkSheet ws = wb.GetWorkSheet("Sheet1");
//Get value By Cell Address
Int32 int_val= ws ["C6"].Int32Value;
//Get value by Row and Column Address
string str_val=ws.Rows [3].Columns [1].Value.ToString();
Console.WriteLine("Getting Value by Cell Address: {0}",int_val);
Console.WriteLine("Getting Value by Row and Column Indexes: {0}",str_val);
Console.ReadKey();
}
'''
'''Get Cell Value
'''anchor-get-specific-cell-value
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
'Load Excel file
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
'Open WorkSheet
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
'Get value By Cell Address
Dim int_val As Int32= ws ("C6").Int32Value
'Get value by Row and Column Address
Dim str_val As String=ws.Rows (3).Columns (1).Value.ToString()
Console.WriteLine("Getting Value by Cell Address: {0}",int_val)
Console.WriteLine("Getting Value by Row and Column Indexes: {0}",str_val)
Console.ReadKey()
End Sub
此程式碼顯示以下輸出:
Excel 文件 sample.xlsx
中 row [3].Column [1]
和 C6
儲存格的值:
行和列的索引從0
開始。
打開 Excel WorkSheets
並獲取特定的呼叫數據,您可以從現已開啟的 Excel 工作表中閱讀更多有關如何在C# 中讀取 Excel 數據的資訊。
4.2. 從特定範圍取得數據
現在讓我們看看如何使用 IronXL 從打開的 Excel WorkSheet
中獲取特定範圍內的數據。
IronXL 提供了一種智能方式來獲取特定範圍的數據。 我們只需指定 from
到 to
的值:
WorkSheet ["From Cell Address : To Cell Address"];
WorkSheet ["From Cell Address : To Cell Address"];
WorkSheet ("From Cell Address : To Cell Address")
讓我們看看如何使用範圍從打開的 Excel WorkSheet
中獲取數據的示例:
/**
Get Data from Range
anchor-get-data-from-specific-range
**/
using IronXL;
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
//specify the range
foreach (var cell in ws ["B2:B10"])
{
Console.WriteLine("value is: {0}", cell.Text);
}
Console.ReadKey();
}
/**
Get Data from Range
anchor-get-data-from-specific-range
**/
using IronXL;
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
//specify the range
foreach (var cell in ws ["B2:B10"])
{
Console.WriteLine("value is: {0}", cell.Text);
}
Console.ReadKey();
}
'''
'''Get Data from Range
'''anchor-get-data-from-specific-range
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
'specify the range
For Each cell In ws ("B2:B10")
Console.WriteLine("value is: {0}", cell.Text)
Next cell
Console.ReadKey()
End Sub
上述代碼將從B2
到B10
提取資料,如下所示:
我們可以看到 Excel 檔案 sample.xlsx
中,從 B2
到 B10
的數值:
4.3. 從行中獲取數據
我們還可以描述特定行的範圍。 例如:
WorkSheet ["A1:E1"]
WorkSheet ["A1:E1"]
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'WorkSheet ["A1:E1"]
這將顯示從A1
到E1
的所有值。 閱讀更多有關C# Excel 範圍以及如何處理不同的行和列標識。
4.4. 從工作表獲取所有數據
使用 IronXL 輕鬆獲取打開的 Excel 工作表中的所有單元格數據。 對於這個任務,我們需要通過行和列索引訪問每個單元格的值。 讓我們看看以下示例,其中我們將遍歷所有WorkSheet
單元格並訪問其值。
在此範例中,基本上有兩個迴圈在運作:一個用於遍歷 Excel WorkSheet
的每一行,另一個則是用於遍歷特定行的每一列。 這樣每個單元格的值就可以輕鬆訪問了。
/**
Get All Data
anchor-get-all-data-from-worksheet
**/
using IronXL;
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample2.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
//access all rows of open Excel WorkSheet
for (int i = 0; i < ws.Rows.Count(); i++)
{
//access all columns of specific row
for (int j = 0; j < ws.Columns.Count(); j++)
{
//Access each cell for specified column
Console.WriteLine(ws.Rows [i].Columns [j].Value.ToString());
}
}
Console.ReadKey();
}
/**
Get All Data
anchor-get-all-data-from-worksheet
**/
using IronXL;
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample2.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
//access all rows of open Excel WorkSheet
for (int i = 0; i < ws.Rows.Count(); i++)
{
//access all columns of specific row
for (int j = 0; j < ws.Columns.Count(); j++)
{
//Access each cell for specified column
Console.WriteLine(ws.Rows [i].Columns [j].Value.ToString());
}
}
Console.ReadKey();
}
'''
'''Get All Data
'''anchor-get-all-data-from-worksheet
'''*
Imports IronXL
Shared Sub Main(ByVal args() As String)
Dim wb As WorkBook = WorkBook.Load("sample2.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
'access all rows of open Excel WorkSheet
For i As Integer = 0 To ws.Rows.Count() - 1
'access all columns of specific row
For j As Integer = 0 To ws.Columns.Count() - 1
'Access each cell for specified column
Console.WriteLine(ws.Rows (i).Columns (j).Value.ToString())
Next j
Next i
Console.ReadKey()
End Sub
上述程式碼的輸出將顯示完整打開的 Excel WorkSheet
的每個儲存格值。