如何使用 VB.NET 將資料集匯出到 Excel:IronXL 完整指南
在 VB.NET 中將資料集匯出到 Excel 可以讓開發人員將結構化資料轉換為專業的電子表格檔案。 無論您是在桌面應用程式中使用 DataGrid,還是從系統資料庫中提取信息,IronXL 都提供了一個直觀的 API,可以簡化此工作流程。 它允許您建立、編輯和操作 Excel 文件,並匯出數據,而無需在使用者電腦上安裝 Microsoft Office。
為什麼選擇 IronXL 匯出資料?
對於需要高效處理大型資料集的開發人員來說,這個函式庫是理想之選。 與標準 COM 互通不同,IronXL 在資料量增加時不會延遲。 下面,我們將詳細介紹如何設定項目並執行匯出操作。
立即開始免費試用,探索 IronXL 如何簡化 .NET Framework 專案中的資料集和資料表操作。
在VB.NET中,將資料集匯出到Excel的最佳方法是什麼?
在VB.NET中,將資料集匯出到 Excel 最有效的方法是使用 IronXL 的WorkBook和WorkSheet類別。 該程式庫雖然支援現代的 XLSX 格式,但也支援傳統的 XLS 和通用的 CSV 格式,從而確保與其他應用程式的兼容性。
首先,透過 Visual Studio 中的 NuGet 套件管理器安裝 IronXL,方法是搜尋"IronXL.Excel"或在套件管理器控制台中執行Install-Package IronXL.Excel 。
在這個例子中,我們建立一個工作簿實例來保存我們的資料。
Imports IronXL
Imports System.Data
' Create a new DataTable object with sample data
Dim dt As New DataTable("Employees")
dt.Columns.Add("ID", GetType(Integer))
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Department", GetType(String))
' Add rows to the DataTable
dt.Rows.Add(1, "John Smith", "Engineering")
dt.Rows.Add(2, "Sarah Jones", "Marketing")
dt.Rows.Add(3, "Mike Wilson", "Sales")
' Create a new workbook and worksheet
Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
Dim worksheet As WorkSheet = workbook.CreateWorkSheet("Employees")
' Add column headers to the first worksheet
For col As Integer = 0 To dt.Columns.Count - 1
worksheet.SetCellValue(0, col, dt.Columns(col).ColumnName)
Next
' Export data from DataTable to Excel worksheet
For row As Integer = 0 To dt.Rows.Count - 1
For col As Integer = 0 To dt.Columns.Count - 1
worksheet.SetCellValue(row + 1, col, dt.Rows(row)(col).ToString())
Next
Next
' Save the Excel file
workbook.SaveAs("EmployeeData.xlsx")Imports IronXL
Imports System.Data
' Create a new DataTable object with sample data
Dim dt As New DataTable("Employees")
dt.Columns.Add("ID", GetType(Integer))
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Department", GetType(String))
' Add rows to the DataTable
dt.Rows.Add(1, "John Smith", "Engineering")
dt.Rows.Add(2, "Sarah Jones", "Marketing")
dt.Rows.Add(3, "Mike Wilson", "Sales")
' Create a new workbook and worksheet
Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
Dim worksheet As WorkSheet = workbook.CreateWorkSheet("Employees")
' Add column headers to the first worksheet
For col As Integer = 0 To dt.Columns.Count - 1
worksheet.SetCellValue(0, col, dt.Columns(col).ColumnName)
Next
' Export data from DataTable to Excel worksheet
For row As Integer = 0 To dt.Rows.Count - 1
For col As Integer = 0 To dt.Columns.Count - 1
worksheet.SetCellValue(row + 1, col, dt.Rows(row)(col).ToString())
Next
Next
' Save the Excel file
workbook.SaveAs("EmployeeData.xlsx")輸出
如何使用 VB.NET 將資料集匯出到 Excel:IronXL 完整指南:圖 1 - VB.NET 中將簡單 DataTable 匯出到 Excel 的輸出範例
這段 VB 程式碼創建了一個 DataTable 對象,用行填充它,然後遍歷每個單元格以填充 Excel 工作表。 此處預設使用SetCellValue方法,以確保資料在 Excel 工作表中的精確放置。
如何將資料表匯出到多個Excel工作表中?
當使用包含多個表格的複雜資料集設計器佈局時,每個 DataTable 都可以匯出到單一 Excel 工作簿中的單獨 Excel 工作表中。 當需要向使用者展示分類資訊且資訊匯總在單一文件中時,這種方法尤其有用。
| 特徵 | 描述 |
|---|---|
| 格式支援 | 原生支援 XLSX 檔案格式、XLS 和 CSV 檔案格式。 |
| 可擴展性 | 能夠處理大型資料集,而不會出現記憶體峰值。 |
| 獨立 | 對Excel系統無任何要求。 |
Imports IronXL
Imports System.Data
' Create a new Dataset with multiple tables
Dim ds As New DataSet("CompanyData")
' First DataTable - Products
Dim dtProducts As New DataTable("Products")
dtProducts.Columns.Add("ProductID", GetType(Integer))
dtProducts.Columns.Add("ProductName", GetType(String))
dtProducts.Columns.Add("Price", GetType(Decimal))
dtProducts.Rows.Add(101, "Widget A", 29.99D)
dtProducts.Rows.Add(102, "Widget B", 49.99D)
ds.Tables.Add(dtProducts)
' Second DataTable - Orders
Dim dtOrders As New DataTable("Orders")
dtOrders.Columns.Add("OrderID", GetType(Integer))
dtOrders.Columns.Add("Customer", GetType(String))
dtOrders.Columns.Add("Total", GetType(Decimal))
dtOrders.Rows.Add(1001, "Acme Corp", 599.90D)
dtOrders.Rows.Add(1002, "Tech Inc", 299.95D)
ds.Tables.Add(dtOrders)
' Create workbook and export each DataTable to separate sheets
Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
For Each table As DataTable In ds.Tables
Dim sheet As WorkSheet = workbook.CreateWorkSheet(table.TableName)
' Write column headers
For col As Integer = 0 To table.Columns.Count - 1
sheet.SetCellValue(0, col, table.Columns(col).ColumnName)
Next
' Write data rows
For row As Integer = 0 To table.Rows.Count - 1
For col As Integer = 0 To table.Columns.Count - 1
sheet.SetCellValue(row + 1, col, table.Rows(row)(col).ToString())
Next
Next
Next
workbook.SaveAs("CompanyReport.xlsx")Imports IronXL
Imports System.Data
' Create a new Dataset with multiple tables
Dim ds As New DataSet("CompanyData")
' First DataTable - Products
Dim dtProducts As New DataTable("Products")
dtProducts.Columns.Add("ProductID", GetType(Integer))
dtProducts.Columns.Add("ProductName", GetType(String))
dtProducts.Columns.Add("Price", GetType(Decimal))
dtProducts.Rows.Add(101, "Widget A", 29.99D)
dtProducts.Rows.Add(102, "Widget B", 49.99D)
ds.Tables.Add(dtProducts)
' Second DataTable - Orders
Dim dtOrders As New DataTable("Orders")
dtOrders.Columns.Add("OrderID", GetType(Integer))
dtOrders.Columns.Add("Customer", GetType(String))
dtOrders.Columns.Add("Total", GetType(Decimal))
dtOrders.Rows.Add(1001, "Acme Corp", 599.90D)
dtOrders.Rows.Add(1002, "Tech Inc", 299.95D)
ds.Tables.Add(dtOrders)
' Create workbook and export each DataTable to separate sheets
Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
For Each table As DataTable In ds.Tables
Dim sheet As WorkSheet = workbook.CreateWorkSheet(table.TableName)
' Write column headers
For col As Integer = 0 To table.Columns.Count - 1
sheet.SetCellValue(0, col, table.Columns(col).ColumnName)
Next
' Write data rows
For row As Integer = 0 To table.Rows.Count - 1
For col As Integer = 0 To table.Columns.Count - 1
sheet.SetCellValue(row + 1, col, table.Rows(row)(col).ToString())
Next
Next
Next
workbook.SaveAs("CompanyReport.xlsx")輸出
如何使用 VB.NET 將資料集匯出到 Excel:IronXL 完整指南:圖 2 - 產生的 Excel 文件,其中匯出的資料集以單獨的工作表形式呈現
此程式碼循環遍歷資料集中的每個表,建立相應的工作表,並將所有列標題和資料傳輸到該工作表。
如何將資料庫查詢結果匯出到 Excel 文件?
連接到資料庫資料來源並將結果直接匯出到新的 Excel 檔案是一種常見需求。 將SqlDataAdapter與 IronXL 結合使用,可以建立從 SQL 系統到電子表格的無縫管道。
1.初始化連線:設定連接字串。 2.填充資料集:使用適配器將資料拉取到資料集設計器。 3.對應到 IronXL:建立一個工作簿實例,並遍歷結果以根據需要操作 Excel 檔案。
Imports IronXL
Imports System.Data
Imports System.Data.SqlClient
' Define connection string and SQL query
Dim connectionString As String = "Server=localhost;Database=SalesDB;Integrated Security=True"
Dim query As String = "SELECT CustomerID, CompanyName, ContactName, Country FROM Customers"
' Create Dataset and fill from database
Dim ds As New DataSet()
Using connection As New SqlConnection(connectionString)
Dim adapter As New SqlDataAdapter(query, connection)
adapter.Fill(ds, "Customers")
End Using
' Access the DataTable exported from the database
Dim dt As DataTable = ds.Tables("Customers")
' Create Excel file and export the DataTable
Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
Dim worksheet As WorkSheet = workbook.CreateWorkSheet("CustomerData")
' Write column headers with formatting
For col As Integer = 0 To dt.Columns.Count - 1
worksheet.SetCellValue(0, col, dt.Columns(col).ColumnName)
worksheet.Rows(0).Style.Font.Bold = True
Next
' Execute data transfer to worksheet cells
For row As Integer = 0 To dt.Rows.Count - 1
For col As Integer = 0 To dt.Columns.Count - 1
Dim cellValue As Object = dt.Rows(row)(col)
worksheet.SetCellValue(row + 1, col, If(cellValue Is DBNull.Value, "", cellValue.ToString()))
Next
Next
' Save output file
workbook.SaveAs("CustomerExport.xlsx")
Console.WriteLine("Export complete!")Imports IronXL
Imports System.Data
Imports System.Data.SqlClient
' Define connection string and SQL query
Dim connectionString As String = "Server=localhost;Database=SalesDB;Integrated Security=True"
Dim query As String = "SELECT CustomerID, CompanyName, ContactName, Country FROM Customers"
' Create Dataset and fill from database
Dim ds As New DataSet()
Using connection As New SqlConnection(connectionString)
Dim adapter As New SqlDataAdapter(query, connection)
adapter.Fill(ds, "Customers")
End Using
' Access the DataTable exported from the database
Dim dt As DataTable = ds.Tables("Customers")
' Create Excel file and export the DataTable
Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
Dim worksheet As WorkSheet = workbook.CreateWorkSheet("CustomerData")
' Write column headers with formatting
For col As Integer = 0 To dt.Columns.Count - 1
worksheet.SetCellValue(0, col, dt.Columns(col).ColumnName)
worksheet.Rows(0).Style.Font.Bold = True
Next
' Execute data transfer to worksheet cells
For row As Integer = 0 To dt.Rows.Count - 1
For col As Integer = 0 To dt.Columns.Count - 1
Dim cellValue As Object = dt.Rows(row)(col)
worksheet.SetCellValue(row + 1, col, If(cellValue Is DBNull.Value, "", cellValue.ToString()))
Next
Next
' Save output file
workbook.SaveAs("CustomerExport.xlsx")
Console.WriteLine("Export complete!")SqlDataAdapter執行命令並從 SQL 資料庫檢索數據,將其儲存在資料集的 Tables 集合中。 程式碼遍歷從查詢匯出的 DataTable,並使用條件檢查處理空值。 列標題採用粗體字體可以提高可讀性。
對於 XML 資料來源,IronXL 還支援讀取和寫入基於 XML 的電子表格格式,為不同的資料交換場景提供了靈活性。
結論
透過 IronXL 簡潔的 API,在 VB.NET 中將資料集匯出到 Excel 的任務變得非常簡單。 無論是處理單一 DataTable 物件或多個資料表,此程式庫都能處理 .NET Framework 和 .NET Core 專案中的檔案格式轉換和工作表管理。
這些範例示範如何在不依賴 Microsoft Office Interop 的情況下,從記憶體資料結構和資料庫查詢建立 Excel 檔案。 IronXL 的方法消除了對 COM 的依賴,同時提供了字體樣式和多工作表支援。
準備好在VB.NET應用程式中實作資料集到Excel的導出功能了嗎? 購買許可證或探索更多DataTable 教程,以擴展您的 Excel 自動化功能。
常見問題解答
如何在VB.NET中將資料集匯出到Excel?
您可以使用 IronXL 庫在 VB.NET 中將資料集匯出到 Excel,該庫提供了一個直覺的 API,無需 Microsoft Office 即可建立、編輯和操作 Excel 檔案。
使用 IronXL 需要安裝 Microsoft Office 嗎?
不,IronXL 不需要在使用者電腦上安裝 Microsoft Office 即可匯出或處理 Excel 檔案。
我可以使用 IronXL 匯出哪些類型的資料?
IronXL 可讓您有效率地將結構化資料(例如資料表和資料集)匯出至 Excel 檔案。
IronXL 能否處理 Excel 檔案的建立和編輯?
是的,IronXL 提供了在 VB.NET 中以程式設計方式建立、編輯和操作 Excel 檔案的功能。
IronXL 是否適用於桌面應用程式?
是的,IronXL 非常適合桌面應用程序,特別是那些使用資料網格或系統資料庫將資料匯出到 Excel 的應用程式。
使用 IronXL 匯出資料集有哪些好處?
IronXL 提供了一個 API,無需 Microsoft Office 等外部依賴項即可處理 Excel 檔案的建立和操作,從而簡化了資料集的匯出過程。
IronXL 可以在伺服器環境下使用嗎?
是的,IronXL 可以部署在伺服器環境中,用於自動產生 Excel 檔案和匯出資料。
IronXL是否支援大型資料集?
IronXL 旨在高效處理匯出至 Excel 時的大型資料集,從而確保效能和可靠性。
是否可以使用 IronXL 修改現有的 Excel 檔案?
是的,IronXL 允許您開啟和操作現有的 Excel 文件,並提供資料編輯和格式化等功能。
IronXL 使用的是什麼程式語言?
IronXL 支援 VB.NET,使開發人員能夠將 Excel 功能無縫整合到他們的 VB.NET 應用程式中。






