IronXL 開始使用 VB.NET Excel 文件 VB .NET Read & Create Excel Files (Code Example Tutorial) Curtis Chau 更新日期:7月 23, 2025 Download IronXL NuGet 下載 DLL 下載 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 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 編程設置單元格樣式並插入數據。 class="hsg-featured-snippet"> 如何在 VB.NET 中讀取 Excel 文件 下載 VB.NET 讀取 Excel C# 庫 在 VB.NET 中創建 Excel 文件 將數據插入工作表 在 VB.NET 中讀取 Excel 文件 從工作表中訪問數據 對數據執行功能 class="tutorial-segment-title">步驟 1 1. VB.NET 的 Excel 庫 Get the IronXL Excel for VB.NET Library using DLL Download or NuGet. IronXL 是我們快速訪問 VB.NET 項目中 Excel 數據的第一步,也是我們將用於本教程的工具(開發免費)。 Install-Package IronXL.Excel class="tutorial-segment-title">如何教程 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. 創建工作表 創建完 WorkBook 後,可以按照以下步驟創建 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 上述代碼將在 WorkBookwb中創建名為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 上述代碼將在工作表ws1的單元格A1中寫入Hello World。 3.2. 將數據插入範圍 還可以使用範圍功能將數據寫入許多單元格,如下所示: ' 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的截圖: class="center-image-wrapper"> 可以清楚地看到,在 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. 從工作表中訪問數據 可以通過以下方式從 ExcelSheetws中訪問數據: ' 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 它會顯示從單元格A2到A10的值。 以下提供了上述討論的代碼示例。 ' 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 這將顯示以下輸出: class="center-image-wrapper"> 我們可以看到 Excel 文件Sample.xlsx的截圖: class="center-image-wrapper"> 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 此代碼將為我們顯示如下顯示: class="center-image-wrapper"> 以及此 Excel 文件Sample.xlsx: class="center-image-wrapper"> 在鏈接的文章中,您可以了解更多有關如何讀取 Excel的信息。 class="tutorial-segment-title">教程快速訪問 class="tutorial-section"> class="row"> class="col-sm-8"> 文檔 API 參考 訪問 IronXL 的文檔 API 參考以及使用 VB.NET 項目中 Excel 的簡單方法。查找功能、函數、類等的列表。 文檔 API 參考 class="col-sm-4"> class="tutorial-image"> 教程快速訪問" class="img-responsive add-shadow img-responsive img-popup" src="/img/svgs/documentation.svg" loading="lazy"> 常見問題解答 如何在不使用 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 時,您可以建立和操作 .xls、.xlsx、.csv 和 .tsv 等格式的 Excel 檔案。 如何在VB.NET中對Excel資料進行計算? IronXL 可讓您直接對儲存格區域執行求和、求最小值和求最大值等計算。例如,使用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)按索引存取工作表。 使用 VB.NET 中的 IronXL 建立 Excel 檔案時,預設會儲存在哪裡? 預設情況下,使用 IronXL 在 VB.NET 中建立的 Excel 檔案會儲存在專案的「bin\Debug」資料夾中,除非使用SaveAs方法指定了不同的路徑。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 準備好開始了嗎? Nuget 下載 1,686,155 | 版本: 2025.11 剛剛發布 免費 NuGet 下載 總下載量:1,686,155 查看許可證