如何在 VB.NET 中建立和讀取 Excel 檔案

VB.NET 讀取並建立 Excel 檔案(程式碼範例教學)

This article was translated from English: Does it need improvement?
Translated
View the article in English

開發人員需要一種流暢、簡單的方法來存取 VB .NET Excel 檔案。 在本教程中,我們將使用 IronXL 讀取 VB dotnet Excel 文件,並存取所有資料以供專案使用。 我們將學習如何建立各種格式( .xls.xlsx.csv.tsv )的電子表格,以及如何使用 VB.NET Excel 程式設定儲存格樣式和插入資料。


步驟 1

1. Excel for VB.NET 函式庫

使用DLL 下載NuGet取得 IronXL Excel for VB.NET 函式庫。 IronXL 是我們在 VB.NET 專案中快速存取 Excel 資料的第一步,也是我們將在本教學中使用的工具(開發免費)。

Install-Package IronXL.Excel

操作指南

2. 使用 VB.NET 建立 Excel 文件

IronXL 提供了在 VB.NET 專案中建立 Excel ( .xlsx格式) 檔案的最簡單方法。 之後,我們可以插入數據,也可以設定單元格屬性,例如字體樣式或邊框。

2.1 建立 Excel 文件

我們先來建立一個工作簿:

' Create a new Excel workbook with the default format (.xlsx)
Dim wb As New WorkBook
' Create a new Excel workbook with the default format (.xlsx)
Dim wb As New WorkBook
VB .NET

以上程式碼用於建立一個新的Excel檔案。預設情況下,其副檔名為.xlsx

2.2 建立 XLS 文件

如果您想建立副檔名為.xls文件,可以使用以下程式碼:

' Create a new Excel workbook with .xls format
Dim wb As New WorkBook(ExcelFileFormat.XLS)
' Create a new Excel workbook with .xls format
Dim wb As New WorkBook(ExcelFileFormat.XLS)
VB .NET

2.3 建立工作表

建立工作簿後,可以如下建立 Excel 工作表:

' Create a new worksheet named "Sheet1" in the workbook
Dim ws1 As WorkSheet = wb.CreateWorkSheet("Sheet1")
' Create a new worksheet named "Sheet1" in the workbook
Dim ws1 As WorkSheet = wb.CreateWorkSheet("Sheet1")
VB .NET

上述程式碼將在工作簿wb中建立一個名為Sheet1的新工作表ws1

2.4 建立多個工作表

可以用同樣的方法建立任意數量的工作表:

' Create additional worksheets
Dim ws2 As WorkSheet = wb.CreateWorkSheet("Sheet2")
Dim ws3 As WorkSheet = wb.CreateWorkSheet("Sheet3")
' Create additional worksheets
Dim ws2 As WorkSheet = wb.CreateWorkSheet("Sheet2")
Dim ws3 As WorkSheet = wb.CreateWorkSheet("Sheet3")
VB .NET

3. 將資料插入工作表

3.1. 將資料插入儲存格

現在我們可以輕鬆地按如下方式將資料插入工作表單元格:

' Insert a value into a specific cell
worksheet("CellAddress").Value = "MyValue"
' Insert a value into a specific cell
worksheet("CellAddress").Value = "MyValue"
VB .NET

例如,可以將工作表ws1中的資料插入如下:

' Insert "Hello World" into cell A1 of the worksheet
ws1("A1").Value = "Hello World"
' Insert "Hello World" into cell A1 of the worksheet
ws1("A1").Value = "Hello World"
VB .NET

上述程式碼將在工作表ws1A1儲存格中寫入Hello World

3.2. 將資料插入範圍

也可以使用 range 函數將資料寫入多個單元格,如下所示:

' Insert "NewValue" into the range from cell A3 to A8
ws1("A3:A8").Value = "NewValue"
' Insert "NewValue" into the range from cell A3 to A8
ws1("A3:A8").Value = "NewValue"
VB .NET

3.3 建立和編輯工作表示例

我們將建立一個新的 Excel 檔案Sample.xlsx ,並在其中插入一些數據,以展示我們上面學到的程式碼。

' Import IronXL namespace for Excel operations
Imports IronXL

' Main subroutine to create and edit Excel
Sub Main()
    ' Create a new workbook in XLSX format
    Dim wb As New WorkBook(ExcelFileFormat.XLSX)
    ' Create a worksheet named "Sheet1"
    Dim ws1 As WorkSheet = wb.CreateWorkSheet("Sheet1")
    ' Insert data into cells
    ws1("A1").Value = "Hello"
    ws1("A2").Value = "World"
    ' Insert a range of values
    ws1("B1:B8").Value = "RangeValue"
    ' Save the workbook as "Sample.xlsx"
    wb.SaveAs("Sample.xlsx")
End Sub
' Import IronXL namespace for Excel operations
Imports IronXL

' Main subroutine to create and edit Excel
Sub Main()
    ' Create a new workbook in XLSX format
    Dim wb As New WorkBook(ExcelFileFormat.XLSX)
    ' Create a worksheet named "Sheet1"
    Dim ws1 As WorkSheet = wb.CreateWorkSheet("Sheet1")
    ' Insert data into cells
    ws1("A1").Value = "Hello"
    ws1("A2").Value = "World"
    ' Insert a range of values
    ws1("B1:B8").Value = "RangeValue"
    ' Save the workbook as "Sample.xlsx"
    wb.SaveAs("Sample.xlsx")
End Sub
VB .NET

注意:預設情況下,新的 Excel 檔案將在專案的bin\Debug資料夾中建立。 如果要建立自訂路徑下的新文件,請使用:

wb.SaveAs(@"E:\IronXL\Sample.xlsx")
wb.SaveAs(@"E:\IronXL\Sample.xlsx")
VB .NET

以下是我們新建的Excel檔案Sample.xlsx的螢幕截圖:

Doc5 1 related to 3.3 建立和編輯工作表示例

很明顯,在 VB.NET 應用程式中使用IronXL建立 Excel 檔案是多麼簡單。


4. 在VB.NET中讀取Excel文件

IronXL 也提供了一個簡單的方法,可以在 VB .NET 專案中讀取 Excel ( .xlsx ) 檔案。 為此,只需獲取 Excel 文檔,將其加載到您的專案中,讀取其數據,並根據您的需求使用它即可。

請遵循以下步驟:

4.1. 在專案中存取 Excel 文件

WorkBook是 IronXL 的一個類,其物件提供對 Excel 檔案及其功能的完全存取權。 例如,如果我們想存取 Excel 文件,只需使用:

' Load the Excel file "sample.xlsx" into a workbook
Dim wb As WorkBook = WorkBook.Load("sample.xlsx") 'Excel file path
' Load the Excel file "sample.xlsx" into a workbook
Dim wb As WorkBook = WorkBook.Load("sample.xlsx") 'Excel file path
VB .NET

在上面的程式碼中, WorkBook.Load()函數將sample.xlsx載入到wb中。 透過存取 Excel 檔案的特定工作表,可以對wb執行任何類型的功能。

4.2. 存取特定工作表

要存取 Excel 中的特定工作表,可以使用WorkSheet類,該類別可以透過以下幾種不同的方式使用:

按圖紙名稱

' Access worksheet by name
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") 'by sheet name
' Access worksheet by name
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") 'by sheet name
VB .NET

按頁索引

' Access worksheet by index
Dim ws As WorkSheet = wb.WorkSheets(0) 'by sheet index
' Access worksheet by index
Dim ws As WorkSheet = wb.WorkSheets(0) 'by sheet index
VB .NET

預設工作表

' Access the default worksheet
Dim ws As WorkSheet = wb.DefaultWorkSheet() 'for the default sheet
' Access the default worksheet
Dim ws As WorkSheet = wb.DefaultWorkSheet() 'for the default sheet
VB .NET

第一頁

' Access the first worksheet in the workbook
Dim sheet As WorkSheet = wb.WorkSheets.FirstOrDefault() 'for the first sheet
' Access the first worksheet in the workbook
Dim sheet As WorkSheet = wb.WorkSheets.FirstOrDefault() 'for the first sheet
VB .NET

取得 Excel 表格ws後,您可以從 Excel 檔案的相應工作表中取得任何類型的數據,並執行所有 Excel 功能。


5. 從工作表存取數據

可以透過以下方式從 Excel 表格ws存取資料:

' Retrieve values from specific cells
Dim int_Value As Integer = ws("A2").IntValue 'for integer
Dim str_value As String = ws("A2").ToString() 'for string
' Retrieve values from specific cells
Dim int_Value As Integer = ws("A2").IntValue 'for integer
Dim str_value As String = ws("A2").ToString() 'for string
VB .NET

5.1. 特定列的數據

也可以透過以下方式從特定列的多個儲存格中取得資料:

' Loop through cells in a specific range and print their values
For Each cell In ws("A2:A10")
    Console.WriteLine("value is: {0}", cell.Text)
Next cell
' Loop through cells in a specific range and print their values
For Each cell In ws("A2:A10")
    Console.WriteLine("value is: {0}", cell.Text)
Next cell
VB .NET

它將顯示單元格A2A10中的值。 下面給出上述討論的程式碼範例。

' Example: Load and display values from a column
Imports IronXL

Sub Main()
    ' Load the workbook from file
    Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
    ' Get the first worksheet
    Dim ws As WorkSheet = wb.WorkSheets.FirstOrDefault()
    ' Loop through cells in range A2:A10
    For Each cell In ws("A2:A10")
        Console.WriteLine("value is: {0}", cell.Text)
    Next
    Console.ReadKey()
End Sub
' Example: Load and display values from a column
Imports IronXL

Sub Main()
    ' Load the workbook from file
    Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
    ' Get the first worksheet
    Dim ws As WorkSheet = wb.WorkSheets.FirstOrDefault()
    ' Loop through cells in range A2:A10
    For Each cell In ws("A2:A10")
        Console.WriteLine("value is: {0}", cell.Text)
    Next
    Console.ReadKey()
End Sub
VB .NET

這將顯示以下輸出:

Doc3 Input1 related to 5.1. 特定列的數據

我們可以看到Excel檔案Sample.xlsx的螢幕截圖:

Doc3 1 related to 5.1. 特定列的數據

6. 對資料執行函數

透過應用 SUM、MIN 或 MAX 等聚合函數,可以輕鬆地從 Excel 工作表中存取篩選後的數據,方法如下:

' Aggregate functions on a range of data
Dim sum As Decimal = ws("From:To").Sum()
Dim min As Decimal = ws("From:To").Min()
Dim max As Decimal = ws("From:To").Max()
' Aggregate functions on a range of data
Dim sum As Decimal = ws("From:To").Sum()
Dim min As Decimal = ws("From:To").Min()
Dim max As Decimal = ws("From:To").Max()
VB .NET

您可以點擊此處閱讀更多關於Excel聚合函數的資訊

' Example: Apply functions to data
Imports IronXL

Sub Main()
    ' Load the workbook
    Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
    ' Get the first worksheet
    Dim ws As WorkSheet = wb.WorkSheets.FirstOrDefault()

    ' Perform aggregate calculations
    Dim sum As Decimal = ws("G2:G10").Sum()
    Dim min As Decimal = ws("G2:G10").Min()
    Dim max As Decimal = ws("G2:G10").Max()

    ' Print the results
    Console.WriteLine("Sum is: {0}", sum)
    Console.WriteLine("Min is: {0}", min)
    Console.WriteLine("Max is: {0}", max)
    Console.ReadKey()
End Sub
' Example: Apply functions to data
Imports IronXL

Sub Main()
    ' Load the workbook
    Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
    ' Get the first worksheet
    Dim ws As WorkSheet = wb.WorkSheets.FirstOrDefault()

    ' Perform aggregate calculations
    Dim sum As Decimal = ws("G2:G10").Sum()
    Dim min As Decimal = ws("G2:G10").Min()
    Dim max As Decimal = ws("G2:G10").Max()

    ' Print the results
    Console.WriteLine("Sum is: {0}", sum)
    Console.WriteLine("Min is: {0}", min)
    Console.WriteLine("Max is: {0}", max)
    Console.ReadKey()
End Sub
VB .NET

這段程式碼會顯示如下內容:

Doc3 Output2 related to 6. 對資料執行函數

還有這個 Excel 檔Sample.xlsx

Doc3 2 related to 6. 對資料執行函數

您可以透過連結的文章了解更多關於如何閱讀Excel的資訊


教程快速存取

文件 API 參考

存取 IronXL 的 API 文件參考,了解如何在 VB.NET 專案中輕鬆使用 Excel。尋找功能、函數、類別等列表。

文件 API 參考
Documentation related to 教程快速存取

常見問題解答

不使用 Interop,如何在 VB.NET 中讀取 Excel 檔案?

若要在 VB.NET 中讀取 Excel 檔案而不使用 Interop,您可以使用 IronXL 的 WorkBook.Load 方法來載入檔案。載入後,您可以使用 WorkSheet 類別存取特定工作表的資料。

在 VB.NET 中建立 Excel 檔案的步驟為何?

在 VB.NET 中,若要使用 IronXL 建立 Excel 檔案,請先實體化一個新的 WorkBook 物件。使用 CreateWorkSheet 方法新增工作表,並透過設定儲存格值來填充資料。

是否可以在 VB.NET 中為 Excel 處理不同的檔案格式?

是的,在 VB.NET 中使用 IronXL for .NET 工作時,您可以建立和處理 Excel 檔案,格式包括 .xls、.xlsx、.csv 和 .tsv。

如何在 VB.NET 中執行 Excel 資料的計算?

IronXL 允許您直接在儲存格範圍上執行 Sum、Min 和 Max 等計算。例如,使用 ws('A1:A10').Sum() 計算該範圍內的數值總和。

我可以使用 VB.NET 將資料插入 Excel 工作表中的特定儲存格嗎?

是的,在 VB.NET 中使用 IronXL,您可以透過設定儲存格的 Value 屬性,例如 ws1('A1').Value = 'Hello World',將資料插入特定的儲存格中。

如何在 VB.NET 中將 Excel 工作簿儲存至特定路徑?

要使用 IronXL 將 Excel 工作簿儲存至特定路徑,請使用 SaveAs 方法,並輸入所需的檔案路徑,例如:wb.SaveAs('E:\IronXL\Sample.xlsx')

我該如何下載用於 VB.NET 開發的 Excel 函式庫?

您可以透過 NuGet 執行指令 dotnet add package IronXL.Excel 下載適用於 VB.NET 的 IronXL Excel 函式庫,或從 IronXL 網站下載 DLL。

我可以使用 VB.NET 在單一 Excel 檔案中建立多個工作表嗎?

是的,使用 VB.NET 中的 IronXL,您可以在一個 WorkBook 物件上多次調用 CreateWorkSheet 方法,在一個 Excel 檔案中建立多個工作表。

如何使用 VB.NET 透過 Excel 檔案中的名稱或索引存取特定工作表?

使用 VB.NET 中的 IronXL,您可以使用 wb.GetWorkSheet('SheetName') 透過名稱存取工作表,或使用 wb.WorkSheets(index) 透過索引存取工作表。

使用 IronXL for .NET 在 VB.NET 中建立 Excel 檔案時,預設會儲存在哪裡?

預設情況下,除非使用 SaveAs 方法指定不同路徑,否則使用 IronXL for .NET 建立的 Excel 檔案會儲存在專案的 'bin\Debug' 資料夾。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 1,738,553 | Version: 2025.11 剛發表